When I started my first WordPress Theme I didn’t know what WordPress hooks meant at all. I was reading tutorials and thought it would be very usefull to better manage all hooked functions. If you’re into WordPress development, you can’t ignore hooks for long before you have to delve into them head on. So I thought it might be usefull to write up a few best practises in regards to this topic.
What are WordPress hooks?
WordPress hooks trigger functions and allow users to improve areas in their WordPress theme or plugin. That means you can use the code to modify your website in a way you like without having to alter the original files.
For instance, you want to choose a particular WordPress template file, or you generate the content for each post and you want to extend it by something, or you add social bookmarking links to the end of the content. What ever you want to do… use WordPress hooks.
WordPress Hooks are divided into “Filter” and “Action” hooks, the latter allow you to put in custom code at various locations and the first enables you to manipulate various pieces of your content. Convinced?
Action Hooks
Action hooks are designated locations in the WordPress core where it is possible for third parties to insert additional code to alter code to let you add additional functions you may desire. The most well known and most used Action hook is called wp_head. This one in particular is
to inject additional CSS stylesheets, SEO code or anything else you require. This code will sit right between the and tags of the WordPress theme XHTML structure. That enabled you to manage javascript and css from one location in all of your WordPress themes. All you need to do is to create a certain mindset for your development. Let’s get our hands on by adding Open Graph meta to your theme.
function bs_facebook_open_graph() { if (is_single()) { global $post; $image = get_post_meta($post->ID, 'thesis_post_image', $single = true); if (!$image) $image = 'ENTER URL TO DEFAULT IMAGE HERE'; ?>