Skip to content
BeoHosting
BeoHosting
WordPress

Guide to WordPress Hooks: Actions and Filters

BeoHosting Team··10 min read read
Guide to WordPress Hooks: Actions and Filters

What WordPress hooks are

The WordPress hook system is the foundation that lets you extend and modify the WordPress system without directly changing the source code. Hooks are points in the WordPress code where you can attach your functions that will run at a specific moment or modify data passing through the system. Without hooks, every customization would require directly modifying WordPress core files that would be lost with every update.

There are two types of hooks - actions and filters. Actions let you run code at a specific moment in WordPress's lifecycle, while filters let you modify data before it's displayed or saved. Understanding these two concepts is key for every WordPress developer because they're the foundation for creating themes, plugins, and custom functionality.

Actions - running code at the right moment

How actions work

An action hook is a place in WordPress code where a do_action function call is placed. When WordPress reaches that point during execution, it checks whether functions are attached to that hook and runs them in order by priority. For example, the wp_head action fires inside the head tag of every page, wp_footer fires before the closing body tag, and init fires at the start of every WordPress load after all plugins are loaded.

Using add_action

The add_action function takes four parameters - hook name, callback function, priority, and number of arguments. Priority determines the execution order when multiple functions are attached to the same hook, with a default value of 10 where a lower number means earlier execution. For example, if you want to add Google Analytics code to the site header, attach the function to the wp_head hook with priority 1 to run among the first. If you want to add CSS after all plugins, use priority 99.

Most common action hooks

  • init: Fires at the start of every request after plugins load. Ideal for registering custom post types, taxonomies, and shortcodes.
  • wp_enqueue_scripts: The proper way to add CSS and JavaScript files on the frontend. Uses the wp_enqueue_style and wp_enqueue_script functions for managing dependencies and avoiding duplicates.
  • admin_init: Fires at the start of every admin page. Use for registering plugin settings, permission checks, or redirects.
  • save_post: Fires when a post is saved or updated. Ideal for saving custom meta data, validating content, or sending notifications.
  • wp_login and wp_logout: Fire on user login and logout. Use for activity logging, redirects, or clearing cache.
  • template_redirect: Fires before template loading. Ideal for custom redirects or page access checks.

Filters - data modification

How filters work

A filter hook works similar to an action hook but with a key difference - the filter receives data, modifies it, and returns the modified data. WordPress uses the apply_filters function to pass data through all attached filter functions. Every filter function must return a value because otherwise the data becomes empty. For example, the the_content filter passes post content through all attached functions before displaying, letting plugins add social share buttons, related posts, or ads within content.

Using add_filter

The add_filter function has identical syntax to add_action - hook name, callback function, priority, and number of arguments. The key difference is that the callback function must receive at least one argument representing the data to filter and must return the modified or original data. If your filter function doesn't return a value, the data will be lost which can break the site. Always test filter functions to make sure they return the correct value even for edge cases.

Most common filter hooks

  • the_content: Filters post content before display. Formatting plugins, shortcodes, and content modifications use this filter.
  • the_title: Filters post title. Use for adding prefixes, suffixes, or programmatically formatting titles.
  • wp_mail: Filters email parameters before sending. You can modify recipients, subject, content, or email headers.
  • login_redirect: Filters the URL the user is redirected to after login. Use for custom redirects based on user role.
  • excerpt_length: Filters the length of the automatically generated excerpt. The default value is 55 words.
  • upload_mimes: Filters allowed file types for upload. Use to add support for SVG or other formats.

Creating your own hooks

Custom action hooks

When developing a plugin or theme, creating your own hooks lets other developers extend your functionality without changing your code. Place do_action calls in places where you want to enable extensions - for example before and after form display, before saving data, or after processing an order. Name hooks with your plugin prefix to avoid conflicts with other plugins, for example myplugin_pre_save_data. Document available hooks with a description of when they fire and what arguments they pass.

Custom filter hooks

Use apply_filters in places where you want to enable data modification. For example, if your plugin generates a list of items, pass the items array through a filter before display. Other developers can attach a filter to add, remove, or modify items. For each filter clearly document the data type being filtered, the expected return value format, and the context in which the filter is used. This makes your plugin pleasant to extend and reduces the need for direct modifications.

Plugin development with hooks

Plugin structure

Every WordPress plugin starts with a header comment containing plugin name, description, version, author, and license. The main plugin file typically registers hooks in the class constructor or in an initialization function. Organize code into classes with methods for each functionality and use the constructor to register all hooks in one place. This makes code readable and easier to maintain because you can see all hooks in one place.

Activation and deactivation

WordPress provides special hooks register_activation_hook and register_deactivation_hook that fire on plugin activation and deactivation. Use the activation hook for creating database tables, setting default options, and scheduling cron jobs. Use the deactivation hook for cleaning up scheduled cron jobs and temporary data. For complete data removal when deleting the plugin, create an uninstall.php file that deletes options from the database, custom tables, and uploaded files.

Best practices

Always use prefixes for function and class names to avoid conflicts with other plugins. Check whether a hook exists before registering to avoid errors. Use has_action and has_filter to check whether someone is already attached to a specific hook. Remove hooks with remove_action and remove_filter when you need to modify the behavior of another plugin or theme. Priority is key when removing - you must use the same priority used when adding.

Debugging hooks

Debugging tools

The Query Monitor plugin is an indispensable tool for debugging hooks that displays all registered hooks, execution order, and time each hook consumes. The WP_DEBUG constant in wp-config.php enables displaying PHP errors and warnings that help identify hook issues. The Debug Bar plugin adds a panel in the admin bar with information about hooks, database queries, and memory usage. For advanced debugging, Xdebug with VS Code allows setting breakpoints inside hook callback functions.

Common mistakes

The most common mistake is a filter function that doesn't return a value, resulting in empty content. Another common mistake is a mismatched argument count between hook registration and the callback function. An infinite loop occurs when a filter modifies data in a way that re-triggers the same filter. Use a static variable or flag to prevent recursion. Wrong priority can cause your function not to work because a previous hook changed data in an unexpected way. On BeoHosting WordPress hosting plans we have an environment optimized for plugin development with support for debugging tools and enough PHP memory for complex hook chains.

Conclusion

The WordPress hook system is a powerful and elegant mechanism that enables endless extension of WordPress. Actions give you the ability to run code at the right moment while filters enable data modification anywhere in the system. Understanding hooks is key to the transition from user to developer and opens the door to creating professional plugins and themes. Start with the most commonly used hooks, experiment with customization, and gradually build more complex functionality. If you're just installing WordPress, see our installation guide.

BeoHosting Team

10+ years of experience — Web hosting and infrastructure specialists

  • Web Hosting
  • WordPress Hosting
  • VPS
  • Dedicated Serveri
  • Domeni
  • SSL
  • cPanel
  • LiteSpeed
  • Linux administracija
  • DNS

Last updated: