Sometimes you may want to add information to every posts but don’t want to open every single post to add that line.
In this article, I will show you how to implement three use cases simply by using wordpress add_filter and the_content hook. You may add anything at the end, or at the beginning, of each post:
- Case 1: Add a simple line of text
- Case 2: Add a button of PayPal donate
Table of contents
Add a simple line of text at the end of every post
Go to functions.php in your theme folder and add in the code:
And then go to check your posts. You will see this line has been added to the end.
Similarly, you can add line at the beginning of each post, just change this line:
Add a PayPal donate button at the end of every post
First, a donate button in PayPal need to be setup.
Follow the instructions here to register a donate PayPal button and finally you will have a code snippet or an URL to be embedded anywhere. In this case, I will use the shareable URL.
Then, using the same the_content hook and add_filter, add the button at the end (or beginning) of every post:
Go to functions.php in your theme folder and add:
Let’s style the button a bit.
As a demo, please look at the end of this post.
Limit the content to show in only some pages
Sometimes you might want to show different messages based on the page where users are. Simply add a condition to the function in functions.php using WordPress’s built in such as is_page(), is_single(), is_singular() or is_front_page().
For example, I limit the donate button to only show in this post.