Custom filter for the_content doesn’t work correctly

=====THIS POST HAS BEEN UPDATED===== SCROLL TO THE BOTTOM TO READ THE UPDATE!

I asked a question yesterday that a gentleman named Tim was nice enough to take a stab at. He helped me through writing a filter that will enable me to append the_content with a custom meta box I created using Advanced Custom Fields.

Read More

Tom got me through almost the entire process, but unfortunately wasn’t able to help me complete it. I am no pro programmer by any stretch, but I have (for the last few hours) tried to decipher what his code means and why it might be failing to pull in the text from the back end… And I keep coming up short.

Here is the link to my previous post : It takes you from start ’till now

and here is the code I have right now:

function weedub_affiliate_filter($content) {
$string_to_add = '';
// only add on single posts with aff checkbox and label
if (is_single() && get_field('affiliate_checkbox') && get_field('affiliate_label')) {
    $string_to_add = $string_to_add . '
<div class="weedub_meta_box">
    <div class="weedub_meta_title">
        <span>Weedub Product Recommendations</span>
    </div>';
    while (the_repeater_field('affiliate_label')) {
        // list affiliates
        $string_to_add = $string_to_add . '
        <div class="weedub_meta_item">
            <div class="weedub_meta_label">
                <span>' . get_sub_field('label_affiliate') . '</span>
            </div>
            <div class="weedub_meta_value">
                <a href="' . get_sub_field('link_affiliate') . '" target="_blank" alt="reference link" title="reference link">
                                    ' . get_sub_field('text_for_link_affiliate') . '</a>
            </div>
        </div>';
    }
    $string_to_add = $string_to_add . '</div>';
}
$content = $content . $string_to_add;
return $content;

}

add_filter('the_content', 'weedub_affiliate_filter', 9);

This snippet is ALMOST there, it is displaying in the right place, and it is recognizing that I have entered 3 rows of fields on the backend, but it is not printing the text from the fields. example (scroll down the page till you see where I have written END OF THE_CONTENT in heading case) :
Look for the “Weedub Product Recommendations” box

Any and all help is soooooo appreciated!
THANKS
-Aaron

=====UPDATE====

So, I found out that the code DOES actually print the values, but it prints them BEFORE the_content, and the markup AFTER the_content!!

look just before the content actually starts (after the featured image) and you will see the string of values… then scroll down to the end of the_content and you will see the markup for the meta box, (titled “weedub product recommendations”) here is the link again: link to single post

I can’t figure out where I am going wrong here!!

Related posts

Leave a Reply

1 comment

  1. I don’t exactly know what happened, but I got it working! All cleaned up and ready to use… Here you go guys:

     /**
    * 
    * This is the filter that adds the affiliate box to the end of the article
    * 
    */
    add_filter('the_content', 'weedub_affiliate_filter', 9);
    function weedub_affiliate_filter($content) 
        {
            $string_to_add = '';
            // only add on single posts with aff checkbox and label
            if (is_single() && get_field('affiliate_checkbox') && get_field('affiliate_label')) 
                {
                    $string_to_add .= '<div class="weedub_meta_box"><div class="weedub_meta_title"><span>Weedub Product Recommendations</span></div>';
                    while (the_repeater_field('affiliate_label')) 
                        {   
                            // list affiliates
                            $string_to_add .= '<div class="weedub_meta_item"><div class="weedub_meta_label"><span>' . get_sub_field('label_affiliate') . '</span></div><div class="weedub_meta_value"><a href="' . get_sub_field('link_affiliate') . '" target="_blank" alt="reference link" title="reference link">' . get_sub_field('text_for_link_affiliate') . '</a></div></div>';
                        }
                    $string_to_add .= '</div>';
                }
            $content .= $string_to_add;
    
            return $content;
        }
    

    Let me know if anyone out there can clean this up or optimize it, cause I’m sure it could be tweaked further.

    Thanks to @tim for the majority of this code.