?
Solved

Help with function to place ad inside wordpress content

Posted on 2014-02-15
5
Medium Priority
?
488 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 39862549
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
ID: 39862835
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
ID: 39862870
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 111

Expert Comment

by:Ray Paseur
ID: 39862885
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
ID: 39862925
Thanks Ray - you're the best!

Chris
0

Featured Post

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

752 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