Easy way to add adsense inside wordpress posts without plugin, add adsense before post content or after post content or between post content, make google adsense shortcode easily.
How To Add AdSense Inside Posts
Using the content filter, we will add adsense to wordpress post easily, add adsense before post content or after post content or between post content (using shortcode), using the content filter we can do more, for example we can add content after post or removing empty paragraphs.
Make Google AdSense Function
Copy function code and paste it in your functions.php file, function code:
function my_adsense_code(){ ob_start(); ?> here enter your google adsense code <?php return ob_get_clean(); }
Now replace “here enter your google adsense code” to your google adsense code.
Add AdSense Before Post Content
Using this function, will be display google adsense before post content:
function add_adsense_before_post($content){ if( is_single() ){ $adsense = my_adsense_code(); $str = str_replace( array("\r", "\n", "<br>"), '', $adsense ); $code = '<p>'.$str.'</p>'; return $code.$content; }else{ return $content; } } add_filter('the_content', 'add_adsense_before_post', 0);
Add AdSense After Post Content
Using this function, will be display google adsense after post content:
function add_adsense_after_post($content){ if( is_single() ){ $adsense = my_adsense_code(); $str = str_replace( array("\r", "\n", "<br>"), '', $adsense ); $code = '<p>'.$str.'</p>'; return $content.$code; }else{ return $content; } } add_filter('the_content', 'add_adsense_after_post', 0);
Add AdSense Between Post Content
We will make google adsense shortcode to display adsense between post content:
function my_adsense_shortcode(){ if( is_single() ){ return '<p>'.my_adsense_code().'</p>'; } } add_shortcode('my_adsense', 'my_adsense_shortcode'); // your shortcode is [my_adsense]
Now use this shortcode [my_adsense] to display adsense between post content.
Note
You can add adsense before post content and after post content and between post content, both.