218 PART III • Using Plugins with WordPress
Now, just add something to this plugin, something from functions.php. Say that you do have a
[pull] shortcode in your theme’s functions.php that you want to keep around. Then you’d
just add it after the plugin declaration, like this:
<?php
/*
Plugin Name: Smashing Functionality, Baby!
Description: This plugin keeps my features portable.
Version: 1.0
Author: Thord Daniel Hedengren
Author URI: http://tdh.me
*/
// Pullquote shortcode
function smashing_pullquote( $atts, $content = null ) {
extract( shortcode_atts( array(
'float' => '$align',
), $atts ) );
if ($content) {
return '<blockquote class="pullquote '. $float. '">'. $content.
'</blockquote>';
}
}
add_shortcode( 'pull', 'smashing_pullquote' );
?>
Just adding the code from functions.php like that will do the trick. Now, obviously, you will
need to remove the code from functions.php because otherwise you’ll have a nasty clash
between the functionality plugin (when activated) and the theme’s functions.php. In the
preceding example, you might want to add a style sheet as well, for styling the pullquote
CSS class, but I’m refraining from that because my pull quote might look and feel different
depending on what theme I’ll be using. Whether you put the style with the plugin (queuing a
style sheet up with wp_enqueue_style()) or in your theme’s style.css is completely up to
you; I’m sticking with the functionality only here.
You can add whatever features you like this way, effectively making the features portable
across themes because all you need to do is activate the plugin. Of course, you’ll have to make
sure that your themes will actually handle the features you add. If you put the code for a
custom taxonomy in a functionality plugin, for example, that will just enable the custom
taxonomy for you. To actually output it on the posts on your site, you’ll have to make sure that
your theme does just that.
Functionality plugins work well for other things as well, not just stuff that usually goes in
functions.php. Perhaps you don’t want to use any of the plugins for Google Analytics, but
you don’t want to risk forgetting about putting your tracker code in your theme’s template
files. No problem — just add the code using the wp_head and wp_footer hooks, via your
functionality plugin. That way, you’ll keep your Google Analytics code snippets outside of
your theme files and make sure that you won’t forget to put them in, losing valuable statistics,
the next time you activate a new theme for your site. Obviously, this could just as well be a
plugin of its own, so perhaps you won’t end up with just one functionality plugin, but several.