Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Basically, I want to add a function that executes half way through the_content();

I want to add a message/ad block whatever in the middle of all of my content without having to go in to each individual page and add the block.

Is this even possible?

share|improve this question
    
You could count the str length, then output the first part, your ad and then the last part. Btw: It's "Ad", not "Add" :) –  kaiser Dec 20 '11 at 23:37
add comment

2 Answers

Use this function ad_content() in replace of the_content() in your template.

function ad_content() {
$content = apply_filters ( 'the_content', get_the_content () );
$content = explode ( "</p>", $content );
$half_way = ( count($content) / 2);
    for ( $i = 0; $i < count( $content ); $i ++ ) {
        if ( $i == $half_way ) {
            echo 'YOUR_AD_CODE';

        }
        echo $content[$i] . "</p>";
    }
}
share|improve this answer
    
Function the_content() does not only apply the filters. It also applies str_replace(']]>', ']]&gt;', $content). Using a replacement function implies to always check the source of the_content() after a new version of WP is published. –  jot Dec 21 '11 at 15:39
    
Thanks for the code - this would replace the entire the_content function? Not sure if I want to go down that road, in case of future changes. Thoughts? –  Tom Dec 23 '11 at 1:28
add comment

Of course it's possible, you'll just want to write a function that finds the middle of the content then ads your block. Something like this

function wpse_37015(){

//get the content   
$content= get_the_content(); //get the content as a variable

// find the middle
$content_length= STRLEN($content);
$halfway_mark = ($content_length/ 2);

// clip off the first half
$firsthalf = SUBSTR($content, 0, $halfway_mark);

// find the last '<br>' in the first half
$end_mark = STRRPOS($firsthalf, '<br>');

// add in the adblob at the position found above

return SUBSTR($content, 0, $end_mark) . "<p> YOUR AD HERE p>" . SUBSTR($content, $end_mark); 
}

add_filter('the_content', 'wpse_37015');

This is totally untested and may need to be reworked I got the code to find the middle from here.

Edit: As OneTrickPoney pointed out we need to check for HTML tags one method may be to use php's strip_tags() function . and add something like this after the first content call:

$content = strip_tags($content) //remove html tags

the problem will be adding the tags back at the end.

share|improve this answer
    
Yes, you need to find out if you're within a HTML tag, and move the position outside of it. –  onetrickpony Dec 21 '11 at 0:06
    
This looks awesome - thanks. One Trick Pony - any idea how I would go about doing that? The content is using <p> tags. –  Tom Dec 21 '11 at 3:22
    
Been playing with this a bit - it doesn't seem to be inserting into the middle of the text. "YOUR AD HERE" is the first paragraph within the content. Any ideas? Thanks guys. –  Tom Dec 23 '11 at 1:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.