Wordpress: apply_filters & add_action to the_content = EVIL? - wordpress

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)...

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.

I bumped into a similar issue with a widget I'm dev'ing. I just found this:
http://codex.wordpress.org/Function_Reference/wpautop
Which I'm now going to use instead of add_filters('the_content'). I want the WYSIWYG formatting but I don't want things append to my content because it's not traditional content anymore.

Related

Adding A Button Between post-content and Related Posts Thumbnail Plugin on WordPress

I am trying to add a button to my WordPress template. Basically, I have the post and then there is the Related Posts Thumbnails widget that appears. I want the button to go between the text of the post and the related posts thumbnail widget. The code in my 'Single Post' that contains all of this is as follows:
<div class="post-content">
<?php the_content(__('<em><strong>More:</strong> Read the rest of this entry...</em>', 'life-is-simple')); ?>
</div>
I know the Related Posts Thumbnails plugin falls within this code because it's at that place when I 'Inspect Element' on Google Chrome. I can't find how to edit the order of things within that div though. Any help would be greatly appreciated, thanks!
EDIT
Clarification: I am using the Life Is Simple WordPress theme although it has been custom editing quite a bit (mostly on the CSS side of things though).
That plugin is probably appending the output to the_content with a filter .
That means , that the output is being added to the_content automatically. it is a common behaviour.
You need to look at the plugin´s admin interface to see if you can have an alternative way (I believe there is ) which is using a template tag. in that case, you should put the template tag after the div where the the_content() is .
EDIT I :
After a small dig, - in fact there is - you need to put
<?php get_related_posts_thumbnails(); ?>
Then the plugin should know by itself not to append to the_content.
For parameter passing, or if something is not working, go read their APi or help files.
You'll probably need to edit single.php or archive.php in your theme. If nothing is occuring there that looks familiar, it's probably using a loop. In which case you might find what you are looking for either in loop.php, loop-single.php, or loop-archive.php depending on what type of page you are on and how the theme was constructed. Add your button near where you find Read the rest of this entry...
With more information (such as what theme you are using), one might be able to help more as well.

Wordpress, is there a hook for insert/edit link in post page?

I am trying to add http:// in the post link dialog box if there is not already added. I tried with filter
add_filter('pre_link_url', 'add_http_link_url');
It didn't work. Does anybody know how to do that ?
Doesn't Wordpress automatically add the "http://" there by default?
Maybe this plugin would help?
wordpress.org/extend/plugins/auto-hyperlink-urls/
EDIT
found this on http://betterwp.net/wordpress-tips/make-links-clickable/
a function named make_clickable() that can be found in wp-includes/formatting.php.
make_clickable() filters the comment_text hook with this:
add_filter( 'comment_text', 'make_clickable', 9 );
Since it is that simple, let’s try adding the same filter to our post contents and see if it works
add_filter( 'the_content', 'make_clickable', 12 );
The priority of 12 as used above simply tells WordPress to make links clickable for post contents after shortcodes are parsed (which is at priority 11). If you don’t like such behaviour, just change 12 to any number you want. You should take a look at wp-includes/default-filters.php to choose an appropriate priority for make_clickable().
Being that awesome, however, make_clickable() has a limitation, which you can clearly see in this clickable link: http://codex.wordpress.org/Function_Reference/make_clic ... _clickable.
See the full stop punctuation mark also included in the link, thus making it broken? To avoid such behaviour you must always have one space plus another character after a plain link, or in other words, never put a plain link like that at the end of a paragraph. In case you must, just make the link clickable the normal way .
Of course if you don’t like your visitors to be able to post links that way you can easily remove the filter using:
remove_filter('comment_text', 'make_clickable', 9);
hope this helps, sorry i had to remove the first link as i can only post 2 links till i get my rep up :)

WordPress functions.php: how to apply update_option()?

I'm trying to set the default Image Link URL for my WP users so that it doesn't include the url link as a default. I've done some research, and I know the function is in the wp-admin/options.php:
update_option('image_default_link_type','file');
Rather than mess with the core files, I'd like to put this into the functions.php, but never know the proper way to implement stuff like this! This is what I have so far in my functions.php:
<?php
update_option('image_default_link_type','none');
?>
This obviously doesn't work: it needs the proper setup! What is the correct way to implement this in functions.php?
Also: I'd like to know the strategy for figuring out how to implement functions like this in the future by myself? For example, I never know whether or not I'm supposed to use add_filter or do_action, and how I need to pass the parameters. I've yet to find a book or post out there that explains this very well, and can show me by example. Any good leads on this would be awesome too!
Start with the Wordpress codex. Visit the plugin API (which is really what you are doing) that explains Hooks, Actions and Filters. Then see the Action Reference which provides your list of hooks.
Here you will find the hook update_option_OPTIONNAME. Description from codex:
Runs after a WordPress option has been update by the update_option
function. Action function arguments: old option value, new option
value. You must add an action for the specific options that you want
to respond to, such as update_option_foo to respond when option "foo"
has been updated.
Adding code from asker's comment:
function inventory_linkurl_setting() {
update_option('image_default_link_type','none');
}
add_action('admin_init', 'inventory_linkurl_setting'); ?>

Wordpress discontinuity between Excerpt and its 'more-button'

I'm building a custom Wordpress theme.In my index.php page i'm looping to display the excerpts for all of my blog-posts.
this is how I break the post flow:
//At first I place <!--more--> in my post-text where I want to break it..
the_content('more');
Now the point is I'd like to place the 'more' button not right after the excerpt.I'd like to place this button in another div containing other link-buttons (tags,comments-count ecc).
The only thing I came up with is using a span around the 'more' button (with absolute positioning).
Is there any better solution?
thanks
Luca
The fact that WP injects the read more link into excerpts, rather than just giving the formatted excerpt, has always been a problem for me, too. Especially since what surrounds the link is dependent on where the author placed it: inline or on a new line, before or after an image, inside or outside inline markup. Here's what I've done in the past -- I'm not sure whether this is the best solution but it should work...
get the raw, unformatted post content: $content = $post->post_content
get the excerpt by using explode("<!--more-->", $content).
run the excerpt through the the_content filter: $excerpt = apply_filters("the_content", $content[0]);
Balance the tags... $excerpt = force_balance_tags($excerpt)
On the whole, a better solution may be to ask authors to provide a text excerpt to go with each post rather than relying on this kloodgy method.

Insert a plugin manually into wordpress page

I am working in worpress front page.
I want to add a plugin to the page at a specific location manually but adding the code to the page myself.
I basically want to include a plugin in a certain page on a certain location. So I'm create a div...
<div id="plugin-holder">
**Plugin-will-appear-here-with-this-code**
</div>
Don't anyone know how this is done please?
Thanks
If you're wanting a plugin to appear somewhere, you'll be looking for "shortcode" functionality.
This is actually surprisingly easy to code, check out the examples in the Codex under Shortcode API - ie:
function bartag_func( $atts ) {
// ... do more things here ...
return "text to replace shortcode";
}
add_shortcode( 'bartag', 'bartag_func' );
Once you've called these functions you can use [bartag] in code and it will run your function and replace the shortcode with the generated text your function returns.
If you're adding shortcode functionality to your site, it generally makes most sense to code a really simple plugin and put it in that. The reason why this works best is that, over time, it's really easy to forget and upgrade a theme by mistake (or even change to a new theme) and thus break your site by losing your custom code in your former functions.php. Surprisingly, this is pretty easy to achieve and only requires some specially formatted comments at the top of your plugin file and a little common sense in coding - there are many tutorials and "how to"s around!
Here's a useful shortcode tutorial: http://www.reallyeffective.co.uk/archives/2009/06/22/how-to-code-your-own-wordpress-shortcode-plugin-tutorial-part-1/
You should add the relevant plugin code to functions.php.
I suspect you'll want to use some conditional tags, like is_home() to pinpoint your location. But maybe not, depending on what you are trying to do,
Also, if you're trying to to insert from a pre-existing plug-in, make sure you remove the register_activation_hook or activate_pluginname action.
If your plugin supports a sidebar widget you can simply "widgitize" the div tag that you wish to insert the plugin into.. Google the term and you are gonna find many resources.

Resources