Skip to content
hostcomplex

WordPress Hooks

Maarten Soetens 2 min read

What WordPress Hooks Are and Why They Exist

WordPress hooks actions filters is the system plugins and themes use to extend WordPress. WordPress hooks are named points in the core code where other code can run or change data. They are the recommended way for plugins and themes to extend WordPress without editing core files. There are two kinds: actions and filters.

Hooks keep customizations separate from the core. A plugin that adds a field to the post editor, sends an email when a post is published or changes how dates are shown does so by attaching to a hook. The change survives core updates and stops when the plugin is deactivated. The plugin side of this is explained in the plugins article.

Actions: Running Code at a Specific Moment

An action lets code run when WordPress reaches a particular event, such as loading a page, saving a post or rendering the footer. An action does not return a value. It simply performs extra work at that moment.

A plugin might attach a function to the event fired when a post is published, so that the function notifies an editor. An illustrative line looks like this: add_action('publish_post', 'notify_editor'); WordPress then continues once the function has finished.

Filters: Changing Data Before It Is Used

A filter passes a value into a function, which modifies it and returns the result. Filters change content, titles, excerpts, links and many other values before WordPress displays or saves them. A filter must return a value, because WordPress uses whatever the function returns.

For example, a filter can change the text shown after an excerpt, and the theme displays the new text without any template change. An illustrative line is add_filter('excerpt_more', 'custom_excerpt_end');

Hooks also have a priority number that sets the order in which attached functions run. When two plugins change the same value, the order decides the result, which is one reason conflicts happen.

Frequently asked questions

Are hooks the same as shortcodes?

No. A shortcode is a placeholder written into content, such as a bracketed tag that a plugin replaces with output when the page is displayed. A hook is a point in the code where functions attach. Content editors use shortcodes, as described in the shortcodes article, while developers use hooks, and many plugins use both.

Do I need to know PHP to use WordPress hooks?

To write hooks, yes. They are called from PHP code, usually placed in a child theme or a site-specific plugin rather than in core files. To use hooks that are already built into a plugin, you need no coding at all, because the plugin’s settings handle them.

  • Two-Factor Authentication

    Two factor authentication hosting adds a second check to control panel, domain and CMS logins. Learn which methods are strongest and why 2FA helps.

  • What Is an A Record?

    What is an A record? Learn how this DNS record maps a hostname to an IPv4 address, why domains use several A records and the root and www pattern.

  • Billing and Automation Platforms

    What is WHMCS? Learn how this commercial billing and automation platform helps hosts sell plans, collect payments and create accounts automatically.