Friday, July 15, 2016

How to insert Adsense code after paragraph in Wordpress

To insert Adsense code after 1'st, second or other paragraph, put the code below into Wordpress function.php.

function insert_ad_block( $text ) {

    if ( is_single() ) :

        $ads_text = '<p class="wpselect_middle_content">YOUR ADSENSE CODE</p>';

        $split_by = "\n";
        $insert_after = 2; //number of paragraphs

        // make array of paragraphs
        $paragraphs = explode( $split_by, $text);

        // if array elements are less than $insert_after set the insert point at the end
        $len = count( $paragraphs );
        if (  $len < $insert_after ) $insert_after = $len;

        // insert $ads_text into the array at the specified point
        array_splice( $paragraphs, $insert_after, 0, $ads_text );

        // loop through array and build string for output
        foreach( $paragraphs as $paragraph ) {
            $new_text .= $paragraph;
        }

        return $new_text;

    endif;

    return $text;

}
add_filter('the_content', 'insert_ad_block');

How to modify Adsense responsive ad code

If you find that our responsive ad code doesn't do everything you need, you may modify your ad code to better meet the requirements of your responsive site. The following examples show you how to correctly make these modifications.
Important:
  • If you're new to CSS media queries and modifying your ad code, we recommend you start with the Exact ad unit size per screen width example.
  • If you're already familiar with CSS media queries and modifying your ad code, feel free to go straight to the Examples of advanced responsive ad code features section.
Please be assured that the examples described in this article are acceptable modifications of the AdSense ad code. You won't violate the AdSense program policies by modifying your responsive ad code in these approved ways.

Exact ad unit size per screen width example

This example shows you how to modify your responsive code to set specific ad unit sizes for three ranges of screen widths, i.e., mobile, tablet and desktop. You don't need to have any previous experience of CSS media queries or modifying AdSense ad code to follow this example.
Here's some modified responsive ad code that sets the following exact ad unit sizes per screen width:
  • For screen widths up to 500px: a 320x100 ad unit.
  • For screen widths between 500px and 799px: a 468x60 ad unit.
  • For screen widths of 800px and wider: a 728x90 ad unit.
<style>
.example_responsive_1 { width: 320px; height: 100px; }
@media(min-width: 500px) { .example_responsive_1 { width: 468px; height: 60px; } }
@media(min-width: 800px) { .example_responsive_1 { width: 728px; height: 90px; } }
</style>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- example_responsive_1 -->
<ins class="adsbygoogle example_responsive_1"
     style="display:inline-block"
     data-ad-client="ca-pub-XXXXXXX11XXX9"
     data-ad-slot="8XXXXX1"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
To adapt this sample code for your own site:
  1. Create a responsive ad unit in your AdSense account, and note down the following information from your responsive ad code:
    • Your publisher ID, for example, ca-pub-1234567891234567
    • Your ad unit's ID (data-ad-slot), for example, 1234567890.
  2. In the sample code:
    • Replace all instances of example_responsive_1 with a unique name, e.g., Home_Pagefront_page_123, etc.
      Notes:
      • Your unique name must only contain English letters (A-Z), numbers, and underscores, and the first character must be an English letter.
      • You must use a different unique name each time that you adapt this sample code.
    • Replace ca-pub-XXXXXXX11XXX9 with your own publisher ID.
    • Replace 8XXXXX1 with your own ad unit's ID.
  3. Decide on the sizes you want your ad unit to take per screen width:
    • If you're happy with the existing ad unit sizes in the sample code, then you don't need to make any additional changes.
    • If you want to set different ad unit sizes per screen width, then, in the sample code:
      • Replace 320px and 100px with the width and height of the ad unit you want to use for screen widths up to 500px.
      • Replace 468px and 60px with the width and height of the ad unit you want to use for screen widths between 500px and 799px.
      • Replace 728px and 90px with the width and height of the ad unit you want to use for screen widths of 800px and wider.
  4. Copy and paste your modified ad code into the HTML source code of the page where you'd like the ads to appear.
    Once you’ve placed your ad code, we recommend that you test your ads on different devices and screens to make sure that the responsive behavior is working correctly.