How to automatically add the same text before and after every post in WordPress using a WordPress plugin.

Table of Contents

How to automatically add the same text before or after every post in WordPress

Have you ever visited a blog and noticed that every post has the same block of text before and/or after the post content?

For example, some bloggers include a message asking the reader to subscribe to an email newsletter or share the post they are reading on social media sites.

If you have a block of text that you’d like to display before and/or after the content of each blog post, all you have to do is edit your WordPress theme’s functions.php file and add one or both of the following code snippets at the bottom. This post will tell you how to do that. 

Note: Be sure to back up the original functions.php file first just in case something goes wrong.

And now, for my semi-standard disclaimer:

Using this tip requires editing your WordPress theme’s functions.php file, a procedure that can render your blog improperly formatted, or even completely unusable if not done correctly.

Therefore, you agree not to hold me, your humble tech blogger, responsible if all the oxygen in the air turns into catsup, your chickens start laying turtle eggs or your WordPress blog stops loading and displaying correctly.

If you agree with everything I said in the previous paragraph, then make a backup of your theme’s functions.php file and follow the instructions below: 

Note: While you can make this change to the functions.php file of your blog’s parent theme (or the currently active theme is there is no child theme being used), it’s best to make it to a child theme if you’re using one. That will prevent your changes from being wiped out by a theme update.

1 – Back up your theme’s functions.php file by making a copy of it under a different name (Yes, this is the 3rd time I’ve mentioned backing it up – it’s that important!). You’ll find this file in the /wp-content/themes/xxxxxxx/ folder. Substitute the name of the folder containing your theme for the x’s.

2 – Using your favorite text editor, open the functions.php file.

Note: You can follow step 3, step 4, or both steps 3 AND 4 depending on whether you wish to display your chosen text before the post content, after the post content, or both. Note 2: You can display different text snippets in the two sections of code.

3 – If you wish to place your standard boilerplate text before the post content, add the code below to the end of the functions.php file:

add_filter(‘the_content’, ‘add_my_content’);

function add_my_content($content) {

$my_custom_text = ‘Place the text you wish to insert before your post content here. Do not delete the single quote marks.’;

if(is_single() && !is_home()) {
$content = $my_custom_text.$content;
}
return $content;
}

4 – If you wish to place your standard boilerplate text after the post content, add the code below to the end of the functions.php file:

add_filter(‘the_content’, ‘add_my_content’);
function add_my_content($content) {

$my_custom_text = ‘Place the text you wish to insert below your post content here. Do not delete the single quote marks.’; //

if(is_single() && !is_home()) {
$content .= $my_custom_text;
}
return $content;
}

5 – Save the functions.php file, then upload it to the folder mentioned above.

6 – Load a page on your blog and see if the desired boilerplate text is displayed. If not, try again, making sure that the code in the boxes matches the code you added to the functions.php file. One common error is accidentally deleting the single quite marks before and after the text snippet.

If the page formatting is messed up or if the blog doesn’t work at all, replace the functions.php file you just uploaded with the backup copy you created when you first started. Be sure to rename it back to functions.php.

That’s all there is to it. Good luck!

Bonus tip: explains how to quickly change a common block of text (or code) on every page of your WordPress blog.

Never miss a tip!   to sign up for my free Daily Tech Tips Email Newsletter!

This content was originally published here.