How to Insert Code after More Tag in WordPress Post Page

How to Insert Code after More Tag in WordPress Post Page

AD

This is the inaugural tutorial in our Micro Tutorials category. This category will contain small-sized design and development related bits; snippets and chunks of codes to improve or add new functionality in your web systems, as well as some short tips and/or tricks for graphic design lovers. Main focus will be on WordPress, but of course, this would NOT be the only topic here.

This Simple tutorial will show you how to add/insert code or advertisement etc. right after the <more> tag, but within the post page. For doing this, all we need is to add some piece of code in your theme’s Functions.php

Before we start, use Firebug or view source code of  the post page and see if you can find the more code after the area you inserted within the post editor.

It looks like this in WordPress:

<span id="more-xxx'"></span> //xxx represent the post ID

Lets Begin!

Always remember a golden rule while editing your code: MAKE A BACKUP OF THE FILE YOU ARE EDITING!

Go to Your WordPress Admin ->Appearance -> Editor -> (Theme Functions) Functions.php

Add this code in it:

add_filter('the_content', 'adds_block');

function adds_block($content){
 if (is_single())
 global $post;
$thePostID = $post->ID; // getting the post ID
$new_insert = '<span id="more-' .$thePostID.'"></span>' ;
 return str_replace($new_insert, my_ad(), $content);
 }
function my_ad(){
 return ' Your Ad Code '; // What you want to display in place of the more tag. can be a custom code, advertisement etc.
}

This code will replace the <span id=”more-xxx”></span> with your ad added above in my_ad() Function.

Let me know in your comments that what you think about this new category and how can we improve it.

Join the 98,750+ Subscribers

Subscribe and grab Free Ebook! Photoshop Crash Course!

We promise not to spam!

Further Related Readings

Ayaz Malik

Artist by soul, programmer by profession. I am a designer, developer and Publisher. Running a few blogs and publishing a lot of interesting stuff. you can find me here on twitter
All Articles by

Copyrights © 2014 - Designzzz. All Rights Reserved