When you’re customizing your WordPress site, you may, at some point, need to hook the output of some custom code to appear after your post’s content.
Thankfully, you can easily create a WordPress custom function that does exactly what you need. Let’s explore how.
Key Takeaways
- You can use a custom function on your theme’s functions.php file to append anything you want after your post’s content.
How to Use a Custom Function in WordPress to Hook an Output After Your Content?
If you want to append the output of custom code after your content, you could create a custom function and add it to your theme’s functions.php file. This file allows you to add custom code snippets that modify how your theme looks and behaves.
Here’s the basic structure of the PHP code you need to add:
if ( ! function_exists( 'add_goodbye_text_to_posts' ) ) :
function add_goodbye_text_to_posts ($content) {
if ( is_single() ) {
$goodbye_text = '<p>Thanks for making it to the end of the post. Have a great day.</p>';
$content = $content . $goodbye_text;
}
return $content;
}
endif;
add_filter('the_content', 'add_goodbye_text_to_posts');
What goes inside $extra_content
can vary greatly. It can be a string with no HTML tags, an HTML paragraph tag, an image, and much more.
Let’s provide an example of how to use it to add a goodbye paragraph to your blog posts.
Example of How to Use a Custom Function to Hook Output After Content
Let’s create a custom function that adds a paragraph at the end of your blog post’s content. Input the following code into your theme’s functions.php file. You can find this file by navigating to wp-content/themes/your-current-theme/functions.php.
Additionally, you could use plugins like Code Snippets or WPCode to insert custom code into your site.
Here’s the function:
if ( ! function_exists( 'add_goodbye_text_to_posts' ) ) :
function add_goodbye_text_to_posts ($content) {
if ( is_single() ) {
$goodbye_text = '<p>Thanks for making it to the end of the post. Have a great day.</p>';
$content = $content . $goodbye_text;
}
return $content;
}
endif;
add_filter('the_content', 'add_goodbye_text_to_posts');
After adding this function and saving the changes, go to the bottom of any of your blog posts, and you should see the paragraph we just added.
If you want to make the text stand out, you can even give it some styles like this:
if ( ! function_exists( 'add_goodbye_text_to_posts' ) ) :
function add_goodbye_text_to_posts ($content) {
if ( is_single() ) {
$goodbye_text = '<div style="background-color: #333333; color: #ffffff; font-size: 22px; padding: 20px; border-radius: 8px; line-height: 1.6; text-align: center;" class="goodbye-text">Thanks for making it to the end of the post. Have a great day.</div>';
$content = $content . $goodbye_text;
}
return $content;
}
endif;
add_filter('the_content', 'add_goodbye_text_to_posts');
It should look like this:
It’s Easy to Add Custom Output After Content
As you can see, learning to hook output after your WordPress content requires only basic PHP knowledge.
You can use the template function we provided to create your own functions and display the content you need for your site.
If you found this post useful, read our blog and developer resources for more insights and guides!
Related Articles
How to... / 5 min read
How to... / 5 min read
How to Search for Slugs in a WordPress Website (5 Methods)
Whether you’re optimizing your website for SEO, troubleshooting errors, or reorganizing content, knowing how to search for slugs in a WordPress website can save you a lot of time and…
Read MoreHow to... / 11 min read
How to... / 11 min read
Can You Run Multiple Blogs on WordPress?
Yes, it is possible for you to run multiple blogs on the same WordPress site at the same time. However, WordPress doesn't support multiple individual blogs by default, so you…
Read MoreHow to... / 5 min read
How to... / 5 min read
How to Test if Your Current WordPress Page Is a Custom Post Type?
If you're customizing your site and performing specific actions for certain content, it may be useful to test if your current WordPress page is a custom post type. This way,…
Read MoreWordPress 101 / 6 min read
WordPress 101 / 6 min read
Storyblok or WordPress to Build Your Site? Our Experience as an Agency
With new CMSs popping up every year, you may wonder whether your site would benefit from the newer, up-and-coming Storyblok or WordPress, the CMS veteran that powers nearly half the…
Read MoreHow to... / 5 min read
How to... / 5 min read
How to Add HTML to the Body of a WordPress Post or Page
So, you want to add your own custom HTML to the body of your WordPress posts and pages, huh? That's great. Adding custom HTML code is an excellent way to…
Read More