Solved

Help with function to place ad inside wordpress content

Posted on 2014-02-15
5
480 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 110

Accepted Solution

by:
Ray Paseur earned 500 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 110

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

756 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