In pages and posts you may want to insert, add custom content after post or before in WordPress. Quite simply, itsmeit will illustrate the easiest way you can do this without crashing your site.
Code to insert, Add custom Content After Post or Before in WordPress
Before we do that, let’s learn about Hooks in WordPress, Hooks are a way for one piece of code to interact or modify another piece of code at specific, predefined points. They form the foundation for how plugins and themes interact with WordPress Core, but they are also widely used by Core itself. ( refer ).
To be able to insert or customize any content, any text that you want it to display, add it to the beginning of the article or the end of the article you will need to use the the_content
. This function has the function of displaying the post content and its function is as follows:
Look at the function the_content
you will see it echo $content
, the content here means the article on your website. So to add custom content after post or before in WordPress, you must have figured it out.
In WordPress, there will be two types of hooks: Actions, and Filters, and the way the_content function works is as follows:
So to use the_content function we will use add_filter, this function has a callback function, that calls back to another hook, specifically add_filter
will callback to the_content
. Now this will be how itsmeit will guide you to do it.
How to insert content before WordPress post
The code to add custom content before the post/page, just concatenate it before the $content variable and return the $fullContent variable as follows:
The simple understanding of the above code means that by concatenating the string for it before the $content variable, it will come first.
How to customize the content after WordPress posts
Similar to how to insert content before post/page, the code to add custom content after post /page you just need to concatenate it after the $content variable and return the $fullContent variable as follows:
Add, Insert content both before and after posts in WordPress
You can also have a hybrid scenario where you want to display the content before and after the_content
and this can be achieved with the following code:
Similarly, we can simply understand from the above code that if we concatenate the string after the $content variable, it will come after.
So with the methods of add, insert, or custom content at the end or the beginning of the article in WordPress like itsmeit instructed, you can insert the code into your website by writing a plugin or simply inserting it. in the class function.php
of the theme you are using. If you have any questions, leave a comment below!
– Advertising –