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.

===I HAVE SOMETHING THAT WORKS NOW, JUST NOT CORRECTLY. PLEASE READ THE WHOLE POST, I AM GOING TO LEAVE IT ALL UP HERE BECAUSE OTHERS MAY BE ABLE TO BENEFIT FROM ME BUMBLING THROUGH THIS PROCESS!!

I am using the Advanced Custom Fields plugin v3.1.5 (current as of this writing) and I have a problem that I just can't seem to solve.

I have created a "field group" and it renders correctly on the post edit screen. The meta box is specifically for the author to enter affiliate links. I have included a true/false field at the beginning of the box, so if it is checked (returns true), then the meta box will display on the front end of the site. This actually works perfectly. I added my code:

<?php if(get_field('affiliate_checkbox')){ ?>
<div class="weedub_meta_box">

    <div class="weedub_meta_title">
        <span>Weedub Product Recommendations</span>
    </div>
        <div class="weedub_meta_item">
            <div class="weedub_meta_label">
                <span><?php the_sub_field('label_affiliate'); ?></span>
            </div>
            <div class="weedub_meta_value">
                <a href="<?php the_sub_field('link_affiliate'); ?>" target="_blank" alt="reference link" title="reference link"><?php the_sub_field('text_for_link_affiliate'); ?></a>
            </div>
        </div>
</div>

This was added just after

<?php the_content(); ?>

and therein lays the problem.

I have a plugin that appends a "sponsors box" to the end of the_content and that sponsor box displays BEFORE my affiliate meta box. I understand why this happens (generally) but I am absolutely CLUELESS as to how to get my code to be appended to the content instead of writing it in AFTER the content.

I found the WordPress codex page that talks about the "the_content filter" but I just can't get it to work. I honestly don't even know how to start! Can I write this filter into my functions.php file and have it APPEND my code to the end of the_content and get it to display BEFORE the plugin's "sponsor box"????

I REALLY need help with this, I have thoroughly exhausted 3 full days working on this to no avail...

Any and all help is appreciated, as I'm no php wizard, and I'm at my wits end -Aaron

===================EDIT===========================

I've got something working now, but it's breaking some other filters on the page... BUT ... it IS working, lol :

function weedub_affiliate_filter($content) {

print $content; if (is_single()) { ?>
<div class="weedub_meta_box">

    <div class="weedub_meta_title">
        <span>Weedub Product Recommendations</span>
    </div>
        <div class="weedub_meta_item">
            <div class="weedub_meta_label">
                <span><?php the_sub_field('label_affiliate'); ?></span>
            </div>
            <div class="weedub_meta_value">
                <a href="<?php the_sub_field('link_affiliate'); ?>" target="_blank" alt="reference link" title="reference link"><?php the_sub_field('text_for_link_affiliate'); ?></a>
            </div>
        </div>
</div>

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

I know that it isn't right according to WordPress, I have stumbled on a post written by Ian Stewart about 3.5 years ago: Here is the link It taught me how to make it work the incorrect way!! There are 2 comments below the post that attempt to say how to do it the right way, but the commenters assume you know what you are doing, lol.

=============EDIT=====================

Ok... I'm not getting any errors any more, which is phenomenal! but, scroll down towards the end of the page here: Just past END OF THE_CONTENT you will see that there is no data from the fields... I am pretty sure that it has something to do with this line: if (is_single() && get_field('affiliate_checkbox') && get_field('affiliate_label')) {

but.. I'm not sure! Tim, you are awesome, we are getting REALLY close!

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Congratulations on (nearly) solving your problem already :)

There are 2 comments below the post that attempt to say how to do it the right way

They are proposing to do it this way:

function weedub_affiliate_filter($content) {
    $string_to_add = '';
    if (is_single()) {
        $string_to_add = $string_to_add . 'the string you wanted to add';
    }
    $content = $content . $string_to_add;
    return $content;
}

add_filter( 'the_content', 'weedub_affiliate_filter');

The difference to your current solution is that in this case the filter only filters the content. It does this by appending the two strings $content (a global variable) and $string_to_add.

In your solution, it prints it to the screen and then prints something else. The solution above allows for other filters to filter the content as well before printing it to the screen (which your current version does not, which is why some of your filters are broken).

// edit: syntax error, unexpected T_STRING means that something concerning the single or double quotes is not right.

As you ar mixing php and html simply escaping the single quotes (as I mentioned earlier) will not be enough.

this code should (hopefully) work for you:

http://www.pastie.org/private/kzni3uxip0ze57krvtfyjg (cannot get it to format right in this post...)

share|improve this answer
    
thankyou so much for taking the time to answer my question, and I hope you don't get frustrated with me when I ask "could you take the time to write out the full code example for me"? I am REALLY trying my best to learn this, and what you said makes sense, completely, but I just tried it, and I am messing stuff up!! I will learn a lot if you could do that for me, I truly would. –  Klyde Mar 31 '12 at 8:38
    
I replaced 'the string you want to add' with my code from the top of my post, but it caused an error. I really wish this stuff came a little easier to me! - And thank you for the vote of confidence by the way, I haven't gotten much praise from my site in the past couple days!!! –  Klyde Mar 31 '12 at 8:46
    
@Klyde: No problem :) What kind of error did you get? Without the error code I'm just guessing, but if you just copied your html code in place of the string you want to add you placed singlequotes inside a single-quoted string, which php does not like. It would be easiest to escape them by adding a backslash in front of them (only in front of the single quotes, not the double quotes). –  tim Mar 31 '12 at 8:56
    
Wow, Tim, I really didn't expect to get this level of help... I truly love the WordPress community... I updated the post to show exactly whatI wrote, and the error I got. Thanks again :) –  Klyde Mar 31 '12 at 8:58
    
@Klyde updated my answer, hope it works now. –  tim Mar 31 '12 at 9:18
show 5 more comments

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.