Call php function inside of echo function

14 replies
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
#call #echo #function #inside #php
  • Profile picture of the author mywebwork
    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
    {{ DiscussionBoard.errors[1560036].message }}
    • Profile picture of the author PlumGreekMob
      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


      Originally Posted by mywebwork View Post

      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
      Signature
      CPAlead Forum Learn how to make money like the Big Dogs.
      {{ DiscussionBoard.errors[1560219].message }}
  • Profile picture of the author mywebwork
    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
    {{ DiscussionBoard.errors[1560598].message }}
    • Profile picture of the author PlumGreekMob
      same effect as before, this unfortunately may be something that doesn't have a fix.


      Originally Posted by mywebwork View Post

      How about doing it this way then:

      = get_adsense_code();

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


      Hope that does the trick!

      Bill
      Signature
      CPAlead Forum Learn how to make money like the Big Dogs.
      {{ DiscussionBoard.errors[1560634].message }}
  • Profile picture of the author mywebwork
    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
    {{ DiscussionBoard.errors[1560651].message }}
  • {{ DiscussionBoard.errors[1560654].message }}
  • Profile picture of the author mywebwork
    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?
    {{ DiscussionBoard.errors[1560685].message }}
    • Profile picture of the author lisag
      Originally Posted by mywebwork View Post

      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?
      I took today off to catch up on WF and some of my other hangouts.
      Signature

      -- Lisa G

      {{ DiscussionBoard.errors[1560693].message }}
      • Profile picture of the author mywebwork
        Originally Posted by lisag View Post

        I took today off to catch up on WF and some of my other hangouts.
        Well I'm impressed - both with your productivity and for managing to take a day off!

        Bill
        {{ DiscussionBoard.errors[1560722].message }}
        • Profile picture of the author lisag
          Originally Posted by mywebwork View Post

          Well I'm impressed - both with your productivity and for managing to take a day off!

          Bill
          Days off are gifts I give myself to reboot my brain and to find and share ideas and assistance.
          Signature

          -- Lisa G

          {{ DiscussionBoard.errors[1562671].message }}
    • Profile picture of the author PlumGreekMob
      Originally Posted by lisag View Post

      Thank you for the idea, but i still think it will still have the same output with the same problem.

      Originally Posted by mywebwork View Post


      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?
      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 = <?php get_adsense_code(); ?>;
      /* 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!!
      Signature
      CPAlead Forum Learn how to make money like the Big Dogs.
      {{ DiscussionBoard.errors[1560825].message }}
  • Profile picture of the author PlumGreekMob
    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!!!!
    Signature
    CPAlead Forum Learn how to make money like the Big Dogs.
    {{ DiscussionBoard.errors[1562740].message }}
  • Profile picture of the author mywebwork
    Glad to here that you got it working.

    All the best for a safe and successful New Year!

    Bill
    {{ DiscussionBoard.errors[1562782].message }}
    • Profile picture of the author PlumGreekMob
      Thank you very much.
      If you guys want to check out my project to can here--> BlogStick.com - Add Your Stick To The Tree.
      I would love to get suggestions on what i could do better, or add!
      Keep in mind its still under construction and not launched yet.


      Originally Posted by mywebwork View Post

      Glad to here that you got it working.

      All the best for a safe and successful New Year!

      Bill
      Signature
      CPAlead Forum Learn how to make money like the Big Dogs.
      {{ DiscussionBoard.errors[1563246].message }}

Trending Topics