In your WordPress site pages and posts, you may want to add some content after the page content or the post content. Such content may be required across all pages or posts or both or custom posts types. In such a case you need to make use of a filter that adds content after or before the_content function in WordPress theme or plugin’s page template. Luckily, today, I will illustrate the simplest way how you can get this done without breaking your site.
Example of Content to Add after Post Content
A good example is when you want to share with your visitors a quick note about affiliate disclosure or comment policy.
Instead of typing this content every time you publish the post or page, you can get it done automatically using a filter.
You will not only save time but prevent typos every time you have to add this kind of content. If you are not a WordPress developer you can easily add this snippet to your functions.php file and replace the content with your respective content. You don’t have to worry about it in the future.
Adding Content after Page or Post Content in WordPress
When creating WordPress themes or customizing WordPress themes, you may be required to add content to the end of the_content. It is particularly common when you are using custom Meta fields or custom post types with specific details that should be added to the end of each post.
You can possibly be using get_post_meta , get_user_meta or get_option to get the data stored for the specific post or user before it is added after the_content function.
There are several situations you may be required to add content after the_content. I want to make it easy for you by demonstrating step by step on how to add content after the_content.
Filter Hook after the_content to Add New Content
Obviously to add any content on a WordPress page you need to understand how the filter hooks work. Ideally, WordPress has two types of hooks the action hooks and the filters as clearly explained in this post – WordPress do_action hooks and apply filters, or you should at least have an idea how to edit functions.php file in your theme
So we require a filter that will filter the content on our page and then add the new content before we can return the output.
Add Content after the_content in WordPress
First we need to create the filter as follows:
add_filter('the_content', 'add_content_after');
The callback function should now contain the code that we want to add after the_content we should also pass $content parameter in the callback function so that we filter and return:
function add_content_after($content) { $after_content = “Example of our comment policy”; $fullcontent = $content . $after_content; return $fullcontent; }
So, in this case, we are adding the $after_content and it is displayed as shown in the image below:
You can replace the value of $after_content in the code with your message and add the complete snippet to the functions.php and the content will be added after the content. The combined code snippet is as follows:
add_filter('the_content', 'add_content_after'); function add_content_after($content) { $after_content = “Example of our comment policy after post content”; $fullcontent = $content . $after_content; return $fullcontent; }
Add Content Before the_content in WordPress Page or Post
In some circumstances, you may want to add the content before the_content. For example, when I was recently building a pet website, I needed to list the details of the dog breeds before the content can be displayed as shown in the image below:
Although in this project I used a different approach, if you needed to display the content before the_content, you would still use the filter shared above and the only difference is when you are joining the $fullcontent variable.
In this case, you would start with the content you want to display first as shown below:
add_filter('the_content', 'add_content_before'); function add_content_before($content) { $before_content = “Example of our comment policy before post content”; $fullcontent = $before_content . $content ; return $fullcontent; }
The output should be displayed as highlighted in the image below:
Add Content Before and After the_content in WordPress Page or Post
You can also have a combined scenario where you want to display content before and after the_content and this can be achieved with the following code:
add_filter('the_content', ' add_content_before_and_after); function add_content_before_and_after($content) { $before_content = “Example of our comment policy before and after post content”; $after_content = “Example of our comment policy before and after post content”; $fullcontent = $before_content . $content . $after_content; return $fullcontent; }
The output should be displayed as highlighted in the image below:
Add Content before and after the_content and in Custom Post Types
If you prefer to add only this content for a specific custom post type, you can add some logic that determines if we are viewing the custom post type and if so we add the content else we return the default the_content.
In this case, if your custom post type is named ‘books’ the code should be as follows:
//4 Steps of Adding the new content to specific custom post type page template add_filter('the_content', 'add_new_content_after_books_custom_post_type'); function add_new_content_after_books_custom_post_type($content) { //1) Check if we are on the single post and the post type is equal to books i.e the slug of the custom post type if (is_singular("books")){ //2) Add the new content to a variable $new_books_content = "This is the new book content to be added after the Books custom post type" $aftercontent = $new_books_content; $fullcontent = $content . $aftercontent; //3) Return the first time for the custom post type if the conditional is true return $fullcontent; }else{ //4) If this is any other page or post not the books cpt return the default the_content without the additional content return $content; } }
Joe is an experienced full-stack web developer with a decade of industry experience in the LAMP & MERN stacks, WordPress, WooCommerce, and JavaScript – (diverse portfolio). He has a passion for creating elegant and user-friendly solutions and thrives in collaborative environments. In his spare time, he enjoys exploring new tech trends, tinkering with new tools, and contributing to open-source projects. You can hire me here for your next project.
Wrapping up
As we have outlined in this post, adding content after the_content or before should not be an uphill task, you can quickly copy the code snippet to your theme functions.php file and it should work.
As a professional WordPress developer, it may be helpful to learn more about the WordPress action hooks and in particular the filter hooks. I hope you find this tutorial useful and will share it with your fellow developers and friends.
Comments are closed.