views:

25

answers:

1

I asked this question over in the actual tutorial, but not sure I'll get an answer anytime soon as it's almost 2 months old... so I'll take a gander here...

Tutorial is here: Build a WordPress Plugin to Add Author Biographies to your Posts

To sum up the tutorial and what the problem is, The tutorial adds an Author Bio on to the end of the content like so (the short version):

function x($content) {
     return $content . "Author Bio";
}

add_action('the_content','x');

The Problem:

When someone uses:

$z = apply_filters('the_content', 'some content here');
echo $z;

The Author Bio will end up applied to $z and if $z is echoed out in the middle of some page… the Author Bio would be in the middle of some page… correct? (it's correct because I've tested it...)

Is there a better way to apply something to the end/under/below the_content hook? other than add_action(‘the_content’, ‘some_function’) because this to me seems evil...

or is apply_filters(‘the_content’, ‘some content here’) not the norm or something developers shouldn't be using inside their WordPress templates…? (which seems pretty much the norm, at least upon Google-ing formatting "the_content" outside the loop)...

+2  A: 

Using apply_filters('the_content','some content here'), while it may not be 'the norm' (I don't know. I haven't seen it before, but if I needed formatted text, that's what I'd do), is a perfectly valid use of filters to get some text formatted like the content. Unfortunately, there's no better way to append something to content from a plugin. That is just the way these things work.

However, there's a (less than optimal) way of circumventing this. As part of the setup/install process for your plugin, have the user insert a custom function call, action, or filter into their theme. I know several plugins that do this, so it's not all that uncommon. Something like this:

do_action('my_super_awesome_bio_hook');

Would allow you to hook in without worrying about adding a bio to unexpected (and unintended) content. Even better would be inserting a filter:

echo apply_filters('my_super_awesome_bio_filter_hook','');

That would allow your plugin to modify the bio, but also allow the one using the plugin to override it if necessary (like on pages where they're just using excerpts, like search results, etc.).

Hope this helped.

Also, one minor addendum: you should be using add_filter, not add_action to append the Author Bio. add_action still works, but it's a filter you want to be using.

John P Bloch
Thanks John for the reply and the added tips... Can I assume that "apply_filters('the_content','some content here')" is not a common practice when developing themes/plug-ins for Wordpress?seems that anybody doing so would open up a can of worms to any other plugin/theme element that would add an action/filter to "the_content"...Would be nice if apply_filters had an option to apply only the default filters of whatever hook...
luckykind
Yeah, it's not a common practice for the very reason you said. You don't always know what `the_content` will end up calling. A good example is Simple Facebook Connect. It will sometimes add a 'Like' button to the content. If you want to be able to apply the_content's filters to text without firing the action, you could always look through wp-includes/default-filters.php to see what functions it goes through and then just call the functions instead of using the filters.
John P Bloch
Thanks again John for your help on this...
luckykind