This article will describe in detail adding text or any other content at the bottom of all posts programatically at run time without any changes to the wordpress database.The article will discuss 7 different requirements all done without a plugin through theme’s functions.php file as follows :-
- Adding simple text or any content as Ads to bottom of all posts and pages (including custom post types).
- Adding to all posts and pages only (excuding custom post types).
- Adding to all posts only (excuding pages and other custom post types).
- Adding at bottom of specific post or page by its ID
- Adding A signature at bottom of every post dynamically on the fly
- Add text or content at bottom of only last page of a post with multiple pages OR
- Add text or content at bottom of all pages of the post with multiple pages.
- Replace string in the content of single post programatically.
1. Adding simple text or any content as Ads to bottom of all posts and pages (including custom post types).
To add simple text or any content (like Ads code) to the bottom of every post you will need to add the following code to your theme’s functions.php file. It is recommended that you create the functions.php file in your child theme.The code to be added is as under.Please read the comments carefully
function new_content($content) { $signature = "<br><br>That is all.<br><br>If you liked this article please do leave a reply and share it with friends.<br><br>Thanks.<br><br>"; //you may comment above $signature line and add Adsense or affiliate ads code below as $signature = "<br><br>"."YOUR AD CODE COMES HERE, REPLACE THIS STRING"."<br>" ; if ( is_singular() || in_the_loop() || is_main_query()) { // This is a single post $content = $content.$signature; } return $content; } add_filter('the_content','new_content');
2. Adding to all posts and pages only (excuding custom post types).
To add to the bottom of all posts and pages only, you will have to replace line 9 of code in method 1 with code as below
if ( (is_singular('page') || is_singular('post')) && in_the_loop() && is_main_query() ) {
3. Adding to all posts only.
To add at the bottom of all posts only, you will have to have to replace line 9 of code in method 1 with code as below
if ( is_singular('post') && in_the_loop() && is_main_query() ) {
4. Adding at bottom of specific post or page by its ID
To get the post ID, go to your post editor in WordPress admin area by Selecting Posts -> All Posts. Hover the mouse over the title of the Post you want to replace the text string for. Observe the link that appears on the bottom left, above the task bar. See the portion of the link containing
/post.php?post=1234&…. Here the number after post= is the ID of the post.
To get the page ID, go to your post editor in WordPress admin area by Selecting Posts -> All Pages.Rest action is same as that for getting the post ID.Pages are also like posts having a post ID in wordpress.
Having found the ID ,to add at the end of the post or page ad code as under
add_filter('the_content', 'filter_post_1254'); // 1254 is ID of the post or page you have found function filter_post_1254($content){ if ( is_singular() && in_the_loop() && is_main_query() ) { global $post; if ($post->ID == 1254){ // 1254 is post/page ID you have found $signature = "<br><br>That is all.<br><br>If you liked this article please do leave a reply and share it with friends.<br><br>Thanks.<br><br>"; //you may comment above $signature line and add Adsense or affiliate ads code below as $signature = "<br><br>"."YOUR AD CODE COMES HERE, REPLACE THIS STRING"."<br>" ; $content .= $signature ; } } return $content; }
5. Adding A signature at bottom of every post article dynamically on the fly
It is assumed that you have created a signature image containing your name already.If you would like to create one you can use a photo editing software or create free online here.
Upload the image to the Media library in wordpress admin.Get the url to the image by going to Admin->Media->library->Click image name>get File URL on the right side of the page .Copy the URL and modify it from /wp-content … onwards to look like
/wp-content/uploads/my_signature_image.png
The first forward slash is important.The path will be different for your image as shown in the file URL.
Now to add the signature without using a plugin to the bottom of your single post add the following code.
Replace the contents of src within single quotes with your image URL
function add_signature($content) { $signature = "<br><img style='width:200px;height:120px;float:left;' src='/wp-content/uploads/signature.png' /><br>"; //you can modify the style size of image and float the image right if you desire if ( is_singular('post') && in_the_loop() && is_main_query()) { $content = $content.$signature; } return $content; } add_filter('the_content','add_signature');
The above will add signature to posts only ( not to pages or custom post types )
6. Add text or content at bottom of only last page of a post with multiple pages
It is important to note that if a post spans multiple pages using the wordpress <!–nextpage–> tag,then following code will add content to the bottom of the post only on the last page of the article and display the message as
Continue reading the remaining 6 pages of the article through page links below …. in all except the last page
This happens in line 22 (below).
function add_content_to_last_page($content) { $signature = "<br><br>That is all.<br><br>If you liked this article please do leave a reply and share it with friends.<br><br>Thanks.<br><br>"; global $numpages; // numpages returns the number of pages global $page ; // global $page returns the int number of the current page of multipage post if ( (is_singular('post') || is_singular('page')) && in_the_loop() && is_main_query() && $numpages > 1 ) { // This is a single post // and has more than one page; $current_page = $page ; if( $current_page == $numpages) { // we are on last page of multi page post //add the signature $content = $content.$signature; } else{ $content = $content ."<br>Continue reading the remaining <b><i>".($numpages- $current_page)."</i></b> pages of the article through page links below ....<br>"; } } else{ // single page post $content = $content.$signature; } return $content; } add_filter('the_content','add_content_to_last_page');
OR
7. Add text or content at bottom of all pages of the post with multiple pages.
If you do not want that and would like the content be appended to bottom of every page of a post with multiple pages ,then the following code should be used
function add_content_to_all($content) { $signature = "<br><br>That is all.<br><br>If you liked this article please do leave a reply and share it with friends.<br><br>Thanks.<br><br>"; if ( (is_singular('post') || is_singular('page')) && in_the_loop() && is_main_query()) { $content = $content.$signature; } return $content; } add_filter('the_content','add_content_to_all');
8. Replace string in the content of single post programmatically.
To change the content of the post programmatically at run tume, to do things like replacing a common URL or any other data from post using preg_replace or preg_match php constructs or simply using string replace on all posts ,you can modify and use the following code
function replace_content($content) { if ( is_singular() && in_the_loop() && is_main_query() ) { //you can use str_replace or preg_replace as below to change specific image tags ,urls etc $content = str_replace($content,"replace content with this", $content); } return $content; } add_filter('the_content','replace_content');
There is a detailed article relating to section 8 above here : How to Replace Strings in content (New Window)
That is all.
If you liked this article please do leave a reply and share it with friends.
Thanks.
Very insightful.