0

I've been trying to add some of the Items to the middle of WordPress content using add_filter. The code is successfully added Ads code to the top of WordPress content, but not the meddle.

Here is code I used:

add_filter('the_content', 'bbtre_amazon_content_filter');
function bbtre_amazon_content_filter($content){

if(is_singular('post')){
    $count = explode('</p>', $content);
    foreach($count as $i => $paragraph){
        $count = $i+1;
        }

    $content = explode('</p>', $content);

    foreach( $content as $index => $paragraph){
        if(trim($paragraph)){
            $content["{$index}"] .= '</p>';
        }

        if($index == intval($count/2)){
            $ads = bbtre_the_amazon_items();
            $content["{$index}"] .= $ads;
        }
    }
}

$content = (is_array($content)) ? implode($content) : $content;

return $content;
}

inside the bbtre_the_amazon_items();

function bbtre_the_amazon_items(){
include(get_stylesheet_directory() .'/inc/reshare/templates/ads/amazon.items.php');
}

inside the template page:

<div class="amazon-item-wrap"><?php echo bbtre_get_amazon_items_list(); ?></div>

Anyone please kindly help me to solve the problem, please.

Note: the above code works fine when I use

$content["{$index}"] .= '<p>Hello World</p>';

instead of:

$content["{$index}"] .= $ads;
1

You should use add_filter() instead of apply_filters(). apply_filters() is used to apply the filter that's been registered with add_filter(). Please check the following code for the solution -

function bbtre_amazon_content_filter( $content ) {
    if ( ! is_single() ) {
        return $content; // Return early if not single post
    }

    // Paragraphs array without the closing tag
    $paragraphs       = explode( '</p>', $content );
    // Number of paragraphs in $paragraphs
    $paragraphs_count = count( $paragraphs );
    // Middle index of $paragraphs
    $middle_index     = absint( floor( $paragraphs_count / 2 ) );
    // New content string
    $new_content      = '';

    for ( $i = 0; $i < $paragraphs_count; $i++ ) {
        if ( $i === $middle_index ) {
            // Add custom content in the middle of post contents
            $new_content .= bbtre_the_amazon_items();
        }
        // Append the missing closing p tag
        $new_content .= $paragraphs[ $i ] . '</p>';
    }
    return $new_content;
}
add_filter( 'the_content', 'bbtre_amazon_content_filter' );
  • 1
    additional to using add_filter() make sure to rewrite the functionbbtre_the_amazon_items() to return the result instead of echoing it. possibly by using object buffering. – Michael Feb 18 '18 at 7:41
  • I'm sorry. I did not edit my code here. Actually, I used add_filter, but it did not work. – Kimsea Sok Feb 18 '18 at 9:14
  • @obiPlabon ! Thanks so much for your answer. I found the solution. I need to return the out put using ob_get_clean(); – Kimsea Sok Feb 18 '18 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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