wordpressgenesis

How to add adsense code after 1st paragraph only in genesis?


I want to display adsense ad after the first paragraph only. can one please guide me with proper code.


Solution

  • You can do it the manual way with the following code. It's very efficent and very fast:

    add_filter('the_content', 'prefix_process_content', 20);
    
    function prefix_process_content($content){
    
    $needle = '</p>';
    
    $replace= '</p>your ad code here'; // do not forget the </p>
    
    // Get the position of the first occurrence of $needle
    $pos = strpos($content, $needle);
    
    // if found
    if ($pos !== false) {
        $newstring = substr_replace($content, $replace, $pos, strlen($needle));
    }
    
    return $newstring;
    
    }
    

    I did not test it, but should be working. If you need help with that code let me know.

    If you are using WordPress you can use a plugin which can do that like WP QUADS. It only adds no to very little overhead and does not increase the loading times of your site. https://wordpress.org/plugins/quick-adsense-reloaded/

    It's maintained and tested with latest WordPress version!

    Disclaimer: I am the author of the plugin