Solved

Help with function to place ad inside wordpress content

Posted on 2014-02-15
5
462 Views
Last Modified: 2014-02-16
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
Comment
Question by:St_Aug_Beach_Bum
  • 3
  • 2
5 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
 

Author Comment

by:St_Aug_Beach_Bum
Comment Utility
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
 

Author Comment

by:St_Aug_Beach_Bum
Comment Utility
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
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
 

Author Comment

by:St_Aug_Beach_Bum
Comment Utility
Thanks Ray - you're the best!

Chris
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now