Call php function inside of echo function

by 14 replies
20
I have added an adsense share function to my WPMU/BP site.

I am using the T2 method of adding an adsense unit to show on only the first post on the index page.
found here WordPress - add Adsense to your blog

These two parts are used to define and show adsense only on the first post
1st part
Code:
<?php
$postnum = 1;
$showadsense1 = 1;
?>
2nd part

Code:
<?php if ($postnum == $showadsense1) {
echo '
<script type="text/javascript"><!--
google_ad_client = <?php get_adsense_code(); ?>;
/* xxxxxxxxxxxxxx */
google_ad_slot = "xxxxxxxxx";
google_ad_width = xxx;
google_ad_height = xxx;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
';
} ?>

<?php $postnum++; ?>


Now inside of the second part you will notice
Code:
<?php get_adsense_code(); ?>
after google_ad_client =. What this is doing is showing as is when i am viewing the page source. I believe it to be because of a function inside of a function.

My question is would anyone know of a fix for this or is this something that doesnt have a fix.

Thank you
#programming #call #echo #function #inside #php
  • Couldn't you just change this:

    Code:
    echo '
    <script type="text/javascript"><!--
    google_ad_client = <?php get_adsense_code(); ?>;
    /* xxxxxxxxxxxxxx */
    google_ad_slot = "xxxxxxxxx";
    To this:

    Code:
    echo '
    <script type="text/javascript"><!--
    google_ad_client = ' . get_adsense_code() . ';
    /* xxxxxxxxxxxxxx */
    google_ad_slot = "xxxxxxxxx";
    Just a thought, maybe I'm missing something?

    Bill
    • [ 1 ] Thanks
    • [1] reply
    • Thank you very much for the reply.
      It now pulls the code correctly but when it does it puts it in a different spot then defined. ie it should be after google_ad_client = but is now above <script type="text/javascript">. It also shows the publisher # on the page. here is a userblog so you can see what it does.

      SEA SHIPS


  • How about doing it this way then:

    $adsense_code = get_adsense_code();

    echo '
    <script type="text/javascript"><!--
    google_ad_client = ' . $adsense_code . ';
    /* xxxxxxxxxxxxxx */
    google_ad_slot = "xxxxxxxxx";


    Hope that does the trick!

    Bill
    • [ 1 ] Thanks
    • [1] reply
    • same effect as before, this unfortunately may be something that doesn't have a fix.


  • Everything has a fix!

    Can you post some of the "get_adsense_code()" function? Is it possible that it is returning a non-printable or a control character along with the AdSense user ID number?

    Bill
  • Wow, you're on a roll today Lisa - I was just noticing how many questions you've answered in the last couple of days!

    Happy New Year!

    Bill

    P.S. @PlumGreekMob - why don't you try it without the echo statements and just use straight HTML for the rest of the AdSense JavaScript?
    • [ 1 ] Thanks
    • [2] replies
    • I took today off to catch up on WF and some of my other hangouts.
      • [1] reply
    • Thank you for the idea, but i still think it will still have the same output with the same problem.

      The purpose of the echo statement is to pull it for only the first post on the index page(index page usually has more than one post). If you look here SEA SHIPS Blog Archiv Hello world! (this is a single post page) you will see it working correctly becuase the adsense code is raw with this


      <script type="text/javascript"><!--
      google_ad_client = ;
      /* xxxxxxxxxxxxxx */
      google_ad_slot = "xxxxxxxxx";

      and it works correctly since there is no need for echo statements to make sure that the adsense code only shows up for the first post.


      Thanks!!
  • Found a fix for it that did not require an echo call, instead, it uses a php if slug like here

    <?php if ($adcount == 1) : ?>
    <script type="text/javascript"><!--
    google_ad_client = <?php get_adsense_code(); ?>;
    /* 336x280, TOPfirstPOST */
    google_ad_width = 336;
    google_ad_height = 280;
    //-->
    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
    <?php endif; $adcount++; ?>



    Thanks for your help everyone, Case closed!!!!
  • Glad to here that you got it working.

    All the best for a safe and successful New Year!

    Bill
    • [ 1 ] Thanks
    • [1] reply

Next Topics on Trending Feed