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
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:
add_filter ('the_content', 'ak_add_message_content_after');
function ak_add_message_content_after($content) {
if ( !is_singular('post') ) return $content;
$message = 'This will be added at the end of each article.';
return $content . $message;
}
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:
return $content . $message;
into this:
return $message . $content;
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.
Creating a donate button in PayPal and getting 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:
add_filter ('the_content', 'ak_add_paypal_donate_btn_content_after');
function ak_add_paypal_donate_btn_content_after($content) {
if ( !is_singular('post') ) return $content;
$message = '<a href='{paypal-donate-link}' target='_blank'><button id='paypal-donate-btn'>Donate this blog via PayPal</button></a>';
return $content . $message;
}
Let’s put in some css:
#paypal-donate-btn {
background-color: #ffc439;
border-radius: 40px;
padding: 1rem 2rem;
box-shadow: 0px 4px 15px rgba(0,0,0,0.15);
border-bottom: 0;
margin: 1rem 0;
}
#paypal-donate-btn:hover {
background-color: #ffb506;
box-shadow: 0px 2px 2px rgba(0,0,0,0.1);
}
We’re done!