• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 491
  • Last Modified:

Help with function to place ad inside wordpress content

I've got a function that is designed to place an ad inside of wordpress content.

I can set it to place the ad between paragraphs after a certain number of characters. I've been wanting this ability for years!!! :)

Opps, one problem. The character count includes characters inside of tags.

All my posts start with an image, which includes alt and title values of variable lengths.

So if I have this function to put ads at the first paragraph tag after 400 characters.... but my img tag takes up 200 characters, the visual affect is that the ad shows at the first paragraph tag after 200.

So.... this may be tricky, I'm not sure - but I need this function to show the ad at the first paragraph after 400 characters not counting the characters that are inside of an img tag.

Sometimes I have additional images after where the ad will be placed (never more than one before), so any solution would have to not affect them.

Here's the function. I have minimal experience with functions and arrays, so I haven't a clue how to fix this!

----------------------------------


add_filter('the_content', 'inject_ad_text_after_n_chars');

function inject_ad_text_after_n_chars($content) {

global $myAd; //my ad

$after_character = 440;

$before_content = substr($content, 0, $after_character);
$after_content = substr($content, $after_character);
$after_content = explode('</p>', $after_content);
array_splice($after_content, 1, 0, $myAd);
$after_content = implode('</p>', $after_content);
return $before_content . $after_content;

}
0
St_Aug_Beach_Bum
Asked:
St_Aug_Beach_Bum
  • 3
  • 2
1 Solution
 
Ray PaseurCommented:
I can't test this, so please try it with care.  The theory is to locate the end of the first HTML tag in the content, then add the $after_character offset.  

While that may work for positioning the ad relative to the end of the image tag, it looks to me like the resulting content string will create invalid HTML.  I believe it will inject an extra </p> tag into the document.  This is an artifact of the original function.  If it doesn't bother you, don't worry about it.  If it does, just prepend your $myAd variable with a <p> tag and make sure there is no corresponding </p> tag at the end.

Let me know how it goes, thanks.

<?php // RAY_temp_st_aug.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28365961.html
// REF http://www.php.net/manual/en/function.strpos.php

function inject_ad_text_after_n_chars($content, $after_character=440) {

    global $myAd;

    // FIND THE END OF THE FIRST HTML TAG
    $end_position = strpos($content, '>');

    // IF NO HTML TAGS JUST APPEND THE ADVERT AND RETURN
    if ($end_position === FALSE) return $content . $myAd;

    // ADD THE END OF THE HTML TAG TO THE POSITION
    $after_character = $after_character + $end_position;

    // REST OF THE FUNCTION REMAINS UNCHANGED
    $before_content = substr($content, 0, $after_character);
    $after_content = substr($content, $after_character);
    $after_content = explode('</p>', $after_content);
    array_splice($after_content, 1, 0, $myAd);
    $after_content = implode('</p>', $after_content);
    return $before_content . $after_content;
}

Open in new window

0
 
St_Aug_Beach_BumAuthor Commented:
Hi Ray,

Thank you very much! Works great :)

I had not noticed that extra </p>.

As suggested, I appended a <p> before the ad.

That caused a line break I didn't want though, so I changed that to a slightly odd: <p style="display:inline;">

hmm, works through :)

Thanks again,

Chris
0
 
St_Aug_Beach_BumAuthor Commented:
Ah, better yet -

I had my ad surrounded by a div, and when I prepended the <p>, even inline, the spacing just wasn't quite right.

So I just changed the <div to <p and left the divs styling in place (and removed the trailing </div> of course).

Seems to be working great, still testing though!
0
 
Ray PaseurCommented:
Thanks for the points and thanks for using EE.

I think the extra trailing </p> comes from this construct:

$after_content = explode('</p>', $after_content); // eliminates the </p>
array_splice($after_content, 1, 0, $myAd);  // inserts a new element
$after_content = implode('</p>', $after_content); // rejoins all of the elements, adding a </p> between them

I looked at this a little bit more, and I think your approach of changing <div to <p with styling is probably the best solution.
0
 
St_Aug_Beach_BumAuthor Commented:
Thanks Ray - you're the best!

Chris
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now