Empower your WordPress blog with functions to create a killer theme, utilising hooks to make WordPress do what you want. In my 2005 post,"Working with WordPress functions in Themes" I explained how to use the functions.php file in your theme to add custom functions. A lot of people still haven't realised the power of theme functions. Most of the popular theme frameworks rely heavily on functions and many plugins are no more than a few lines of code, utilising hooks to modify what WordPress outputs. If you are not using your theme's functions.php you are missing some great opportunities to customise WordPress.
Let's get some background out of the way first, then get into the good stuff.
So, what's a hook?
Hooks are provided by WordPress to allow your code to "hook into" the rest of WordPress, to call functions in your code at specific times, in specific places within your blog. There are two kinds of hooks - actions and filters - but unless you are developing plugins the difference is largely semantic.
Basically, action hooks look like this: do_action( "hook_name" ) while filter hooks look like this: apply_filters( "hook_name", "what_to_filter" ).
I'm not going to go into too much detail about hooks since these have been covered elsewhere. See: the WordPress Codex Plugin API. and Adam Brown's list of all hooks available in WordPress 2.7
Useful WordPress Functions
You don't need to be a PHP programmer in order to use hooks and custom functions to enhance your blog. Understanding what they are and how to use them is a good idea though. Some useful information about custom functions can be found in Chris Pearson's post "How You Can Use WordPress Functions to Run a Smarter Blog and in Matt Varone's, "Taking Advantage of Functions.php in Wordpress Themes". Matt also provides some useful functions for you to use.
Over the years, I have found myself using some filters and actions repeatedly. Most simply remove unwanted WordPress output, some are used to extend WordPress functionality and help me to build sites quicker.
In my "Removing the WordPress Generator Tag" post I gave you the actions and filters to use, as well as a function to include, to remove the generator tag from feeds, posts and comments. Security by obscurity is not much of a security enhancement but I simply hate generator tags and remove them from every blog.
If you do not use remote publishing tools, such as Windows Live Writer, use this code to remove those tags from your header. (Note: you can still use remote publishing tools if you bookmark the links in your blog header before adding these to your theme's functions.php file):
// Windows Live Writer
remove_action('wp_head', 'wlwmanifest_link');
// Really Simple Discovery
remove_action('wp_head', 'rsd_link');
To remove the automatic paragraphing that WordPress does (which often messes with code):
//disable auto p
remove_filter ('the_content', 'wpautop');
remove_filter ('comment_text', 'wpautop');
To remove the "pretty" formatting that automatically turns quotes into curly quotes:
// Remove curly quotes
remove_filter('the_content', 'wptexturize');
remove_filter('the_excerpt', 'wptexturize');
remove_filter('comment_text', 'wptexturize');
remove_filter('the_title', 'wptexturize');
All of the above go into your theme's functions.php file, after the opening <?php tag. I use them on every blog I build. Other useful functions include:
//allow HTML in user profiles
remove_filter('pre_user_description', 'wp_filter_kses');
Do you have some favourite functions to share?
If you enjoyed this post, make sure you subscribe to my RSS feed!














{ 2 comments… read them below or jump to the comment form to add your thoughts }
nice, really nice!
I am reading this article second time today, you have to be more careful with content leakers. If I will fount it again I will send you a link
Leave a Comment