vendredi 1 juillet 2016

How to insert author meta inside google adsense javascript in wordpress functions.php

I am trying to insert Author meta i.e Google adsense Publisher ID and Adunit slot number in functions.php for wordpress. The code am using displays this metas as text e.g xxxxxxxxxxxxxxxxxxx i.e both the ID and slot number in same line on the web page and does not show the google adsense script/ad unit.

CODE

    //RECTANGLE Adsense UNit
function get_rectangle() {
    global $post; $author_id=$post->post_author;

            if(is_single()) { 

$rec_Ad.= '<div>';
            $rec_Ad.= '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
            <!-- 300x250 -->
            <ins class="adsbygoogle"
                 style="display:inline-block;width:300px;height:250px"';

    if (get_the_author_meta('rectangle', $author_id)) { 

                 $rec_Ad.= 'data-ad-client="ca-pub-'.the_author_meta('pub-id', $author_id).'"';
                 $rec_Ad.= 'data-ad-slot="'.the_author_meta('rectangle', $author_id).'"></ins>';

                 } else {

                 $rec_Ad.= 'data-ad-client="ca-pub-xxxxxxxxxxxxxxx"';
                 $rec_Ad.= 'data-ad-slot="xxxxxxxxxxxxxxxxxx"></ins>';
            }

            $rec_Ad.= '<script>
            (adsbygoogle = window.adsbygoogle || []).push({});
            </script></div>';
   } 

    return $rec_Ad;
}

MAIN FOCUS

$rec_Ad.= 'data-ad-client="ca-pub-'.the_author_meta('pub-id', $author_id).'"';
$rec_Ad.= 'data-ad-slot="'.the_author_meta('rectangle', $author_id).'"></ins>';

The code is supposed to display in single.php page, the ad unit of author if it exists else show the default ad unit.

Aucun commentaire:

Enregistrer un commentaire