9 Common WordPress Hooks you should be Using and 3 that you shouldn’t

WordPress is an excellent platform for building a website, but unless you are using all of the available hooks, you could be missing out on some great features. This blog post lists 9 common WordPress hooks that bloggers should use and 3 that they sh...

9 Common WordPress Hooks you should be Using and 3 that you shouldn’t


WordPress is an excellent platform for building a website, but unless you are using all of the available hooks, you could be missing out on some great features. This blog post lists 9 common WordPress hooks that bloggers should use and 3 that they should not use. If your site is powered by WordPress then this article will help you to fully utilize its power!

What is a WordPress hook and what does it do 

A WordPress hook is a specific code that tells WordPress to perform some type of action. There are two types: filters and actions. A WordPress filter changes the information contained within the data, whereas an action will directly perform some type of task. For example, if you want to show your posts in descending order on single post pages, then you would use the 'post_class' filter. To change some text on each page, you would use an action.

There are two types of WordPress hooks: filters and actions. Filters change the data inside a variable while actions perform some type of task. If your site is powered by WordPress then this article will help you to fully utilize its power!

9 common WordPress hooks that bloggers should use

When using WordPress, you will likely see the same coding structure in many themes. Most of these coding structures contain filters and actions.

  1. wp_title() - This action prints the HTML to display the page title in a theme. Make sure that you have <title></title> tags in your HTML code where you want the title to appear.
  2. wp_head() - This action prints the header of a WordPress site. It should be used inside '<head></head>' tags to ensure that it is output on every page of your site.
  3. wp_footer() - This action prints the footer of a WordPress site and like 'wp_head' it should be placed within HTML tags (preferably called 'footer').
  4. get_the_category() - This is one of the easiest filters to use, simply put your desired category slug within the function.
  5. get_the_excerpt() - This filter gets the first 55 words of a post and prints it as an excerpt, great for making short snippets of posts without using any extra HTML.
  6. the_content() - This action displays the entire post content on single posts and pages, use it when you want to display the full post.
  7. wp_list_pages() - This action is one of the most useful when working with WordPress and it requires no coding knowledge at all! All you have to do is add '&child_of=X' where X is a page ID number or slug. This means that you can list all your subpages as a bullet list without doing any extra coding.
  8. wp_list_categories() - If you have a lot of categories then this filter is the best way to display them in WordPress, simply add '&sort_by=name' into the code and you will get a shiny bullet list.
  9. wp_nav_menu() -Adding a navigation menu to your WordPress site is super easy, you simply add some code and then save it inside the Appearance > Menu section of your dashboard. This filter lets you create whatever type of menu that you want using HTML and CSS.

3 common WordPress hooks that bloggers should not use

  1. wp_footer - Some people like to paste a lot of code into the footer section, but you should avoid doing this because it can conflict with widgets that are already added to your site. If you want to add some extra widgets then simply get rid of the old ones!
  2. get_comment_link() - This is a built-in function, if you want to add a link within your post to the comment section then you should simply add [comment] into your post content.
  3. get_the_title() - This filter will return the entire title of a post and not just its plain text, remember that by using this filter you will have a duplicate of your title in the HTML.

Why use hooks at all?

WordPress already provides a lot of functionality out of the box, but sometimes we need to do something simple and we end up recoding half of it. It also makes your code more readable by reducing clutter and keeping logic inside functions where it belongs.

Use hooks whenever you can instead of adding custom PHP to your themes. Many plugins use hooks too, so if you learn how to use them properly then your site will be more compatible with existing plugins.

How to find the right hook for your needs

  • Use the WordPress Codex and look up the specific hook you need.
  • Search for a function name in your theme file and find where it is initiated and terminated so that you can add things in between if needed.
  • Google ' WordPress [function name] filter', this should lead to some good results which will help you to find the right hook.

The WordPress Codex is a great resource for finding new and existing hooks, but it is also useful for finding tips and tricks on how to use them properly.

Examples of different types of hooks

get_the_title() filter - filters the title of a post.

do_action('my_hookname') Action - Enqueues a Javascript file and prints some HTML to the bottom of your content area.

wp_head Action - Displays information and scripts inside the head section of your page (useful for adding analytics code).

wp_footer Action - Displays information and scripts at the bottom of your page.

WP_Query class - Modifies a WordPress database query.

add_action , add_filter - Adds an action to a specific hook (e.g. 'mytheme'). This is used to create new "hooks" to allow plugin/theme developers to tap into a particular part of the WordPress engine.

Conclusion

To conclude, if you follow these guidelines when coding within WordPress then it should be much easier for you to add advanced features and functions to your site.

You can create your own filters and hooks, but that is a topic for another blog post.

I hope that you have found this blog helpful, if you have any questions then simply drop them in the comments section below! WordPress is an excellent CMS that offers many benefits compared to using other platforms…

...but there are still some things that it cannot do.

If I've missed out on anything or made an error please let me know in the comments section. Thanks for reading!

About Author

DCreato Team

  • 71 Post
  • 6 Recent Post

Entrepreneur | Designer | SEO & SMM Expert

Latest Comments 0 Responses

Login/ Register to Comment
Back