Add after post content in WordPress manually

You may want to add some content manually after the WordPress post content. It can be your Google AdSense code, Banner ad, even can be just a signature or anything that you want.

In this article, I am going to tell you how you can add custom content after post content by writing your own code.

It is going to be interesting to add after post content in WordPress. The process I will show you here will use the_content hook with add_filter() function. Lots of plugins already using this hook.

Now suppose, you want to show simple text after post content. You can do this task just by writing a few lines of PHP code that is given below:

function cspd_after_post_content($content){
    if (is_single()) {  
        $content .= 'This content will appear after post content';
    return $content;
add_filter( "the_content", "cspd_after_post_content" );

You can write the code in a plugin specific file or inside the theme’s functions.php file. In both of the cases, it will work in the same way.

So what we did?

First, we have created our function and inside it, we are checking if the post is single or not. We only add our text after post content if it is a single post page. Then we have added our text and at the end of our function, we have returned the content.

At last, we use the add_filter() function and pass the the_content() hook and our function.

Now you can test it. To test your code, open a single post in your browser and see at the end of your post content if the text appears or not. If everything’s goes right, you will be able to see the text that after the post we have added with the_content action hook.

When we are in the single post page, our text is actually the part of the post content. Our text will also be included when we call the_content() function in the template file.

Before you test the code, don’t forget to activate the plugin, if you are using the code in plugin specific file. If you are using these code in your theme’s functions.php file, then the theme should be an active theme.

You will be amazed to know that with this process, you will be able to add your own social share button after each of the single post content. Lot’s of social media plugins available in the WordPress plugin repository are actually doing it with the same process.

To retrieve all the post meta, you can use a global variable $post. In the example below, you can see how we retrieve the post status using the global $post variable:

global $post;
$post_status = $post->post_status;
$content .= 'The post status is '.$post_status;

All the post meta is available inside the $post variable as the form of an array. For example, it contains post ID, author ID, post date, post title, comment status, ping status, post type, post modified date and much more. You can just print the array to see what post meta it holds.

So, we are able to successfully add after post content in WordPress with our own PHP code with the help of the the_content hook and WordPress add_filter() function.

I hope, it was an amazing experience for you.

Leave a Reply

Your email address will not be published. Required fields are marked *