Tutorials

How To Add Content Before And After the_content in WordPress?

I see on the Internet that a lot of beginners webmasters have problem with adding custom content before post (the_content). You know, or not, but this is really simple to implementation with custom function. With this code you can for example insert ad after/before post content, image or anything else like signature or custom text. This code/text will be automatically added to posts and pages Today I’m going to show you, how to add content before and after post content in WordPress using a hook.

TIP: Functions will work on pages and posts only!

Add custom content before the_content

Open file functions.php and add at the end this code:

1
2
3
4
5
6
7
8
9
10
11
add_filter('the_content', 'skilledwp_custom_content_before_content'); // our hook
 
function skilledwp_custom_content_before_content($content) {
if(is_page() || is_single()) {
$before = "your code before";
$newcontent = $before . $content;
} else {
$newcontent = $content;
 
return $newcontent;
}

String $before contains code or text inserting before the content. Just change it!

Add custom content after the_content

Like previous code, first you need to edit functions.php and add new function

1
2
3
4
5
6
7
8
9
10
11
12
add_filter('the_content', 'skilledwp_custom_content_after_content'); // our hook
 
function skilledwp_custom_content_after_content($content) {
if(is_page() || is_single()) {
 
$after = "your code after";
$newcontent = $content . $after;
} else {
$newcontent = $content;
 
return $newcontent;
}

String $after contains code or text inserting after the content.

And that’s all! I hope you will have no troubles with adding content before or after post content in WordPress. If you have any questions, just ask in comments below!

Leave a Comment

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