0

I'm looking to display a piece of javascript after the 6th paragraph of every post on my wordpress blog. So far, I can only get the function to work when I'm using a fixed variable:

add_filter( 'the_content', 'prefix_insert_post_ads' );

function prefix_insert_post_ads( $content ) { 
    $ad_code = 'fixed variable such as <div>box</div>';

    if (is_single()) {
        return prefix_insert_after_paragraph( $ad_code, 6, $content );
    }

    return $content;
}

function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
    $closing_p = '</p>';
    $paragraphs = explode( $closing_p, $content );
    foreach ($paragraphs as $index => $paragraph) {

        if ( trim( $paragraph ) ) {
            $paragraphs[$index] .= $closing_p;
        }

        if ( $paragraph_id == $index + 1 ) {
            $paragraphs[$index] .= $insertion;
        }
    }

    return implode( '', $paragraphs );
}

The javascript I'm trying to add:

function ad_unit() ?>
    <script type="text/javascript">
        ad = document.getElementById('marker');
        if (ad.getBoundingClientRect().width) {
        adWidth = ad.getBoundingClientRect().width;  
        } else {
            adWidth = ad.offsetWidth; // for old IE 
        }
        /* Choose the right ID */
        if ( adWidth >= 600 )
          aId = ["test1"];  
        else if ( adWidth >= 468 )
          aId = ["test2"]; 
        else
          aId = ["test3"]; 
        document.write (
            '<div id="' + akId[0] + '"></div>'
        );
    </script>
<?php

I've tried to declare $placeholder = ad_unit(); but it keep displaying the unit at the top of the content instead of after the 6th paragraph. Somehow the prefix_insert_after paragraph function doesn't work after I add the javascript function. Any ideas?

0

2 Answers 2

1

Try it with ob_start() and ob_get_clean()

function ad_unit() 
{ 
    ob_start();

    ?>
    <script type="text/javascript">
      ad = document.getElementById('marker');
      if (ad.getBoundingClientRect().width) {
        adWidth = ad.getBoundingClientRect().width;  
      } else {
        adWidth = ad.offsetWidth; // for old IE 
      }
      /* Choose the right ID */
      if ( adWidth >= 600 )
        aId = ["test1"];  
      else if ( adWidth >= 468 )
        aId = ["test2"]; 
      else
        aId = ["test3"]; 
      document.write ('<div id="' + akId[0] + '"></div>');
    </script>
    <?php

    return ob_get_clean();
}

So what happens is that with ob_start() you enable output buffering. You output the HTML but buffer it, then with ob_get_clean() you get the buffered output from the output buffer.

This will be cleaner without a function (and definitely work, I don't remember what plain HTML does when put in a PHP function):

// Somewhere above the rest of your application
ob_start(); ?>

/// YOUR HTML/javascript

<?php
$advertisement = ob_get_clean();
Sign up to request clarification or add additional context in comments.

3 Comments

output buffers are really great, you can capture what would get sent to the browser w/o it, instead save the buffer into a variable
WOOT. Thanks man! That did the trick. I wasn't aware how useful ob_start(); and ob_get _clean(); are. I read a little bit on PHP.net but damn it's really confusing. By the way, is this a better way of calling hardcoded html/js then simply closing the php tags to include html/js?
Sometimes stuff you put in functions are not supposed to be shown where you call the function. Using the output buffer guarantees this. Even if you are absolutely 100% sure that the function will only be called where the output is supposed to be, use the output buffer. Plans change. This might be an interesting read: web.archive.org/web/20101216035343/http://dev-tips.com/featured/…
0

You need opening and closing brackets for the php function

    <?php
    function ad_unit() { ?>
    <script type="text/javascript">
        ad = document.getElementById('marker');
        if (ad.getBoundingClientRect().width) {
        adWidth = ad.getBoundingClientRect().width;  
        } else {
            adWidth = ad.offsetWidth; // for old IE 
        }
        /* Choose the right ID */
        if ( adWidth >= 600 )
          aId = ["test1"];  
        else if ( adWidth >= 468 )
          aId = ["test2"]; 
        else
          aId = ["test3"]; 
        document.write (
            '<div id="' + akId[0] + '"></div>'
        );
    </script>
    <?php }

1 Comment

In the code you posted you were missing open and closing brackets, so unless the function ad_unit goes on beyond the line:<?php and you have a closing bracket for it after that line than you are missing the closing bracket as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.