12 Powerful WordPress Hooks Every Developer Should Know

WordPress Hooks are the real power and the magic of WordPress. They allow us to customize a WordPress site according to our needs. Of course, without modifying the WordPress core files. This can be done by creating custom themes and plugins.

There are two major types of hooks:

  • Action Hooks: they add new functionality (using add_action())
  • Filter Hooks: they manipulate the post content (using add_filter())

The following examples use the array callable syntax for the callback function. In this case, the callback function is a method of the plugin PHP Class. So, there is no need to use unique names for your callbacks (as in the traditional syntax).

Regarding plugins, add_action() or add_filter() are called in the __construct() of the plugin class.

References

The Proliferation of Hooks Over Time (Source: Adam Brown)

init

Official docs: “Fires after WordPress has finished loading but before any headers are sent”.

add_action( 'init', array( $this, 'my_callback' ) );

Commonly used functions in the callback:

  • load_plugin_textdomain
  • register_post_type
  • register_meta
  • register_block_type

See https://developer.wordpress.org/reference/hooks/init/

admin_init

Official docs: “Fires as an admin screen or script is being initialized”.

add_action( 'admin_init', array( $this, 'my_callback' ) );

Commonly used functions in the callback:

  • add_settings_section
  • add_settings_field
  • register_setting

See https://developer.wordpress.org/reference/hooks/admin_init/

admin_menu

Official docs: “Fires before the administration menu loads in the admin”.

add_action( 'admin_menu', array( $this, 'my_callback' ) );

Commonly used functions in the callback:

  • add_options_page

See https://developer.wordpress.org/reference/hooks/admin_menu/

wp_enqueue_scripts

Official docs: “Fires when scripts and styles are enqueued. It is the proper hook to use when enqueuing scripts and styles that are meant to appear on the front end“.

add_action( 'wp_enqueue_scripts', array( $this, 'my_callback' ) );

Commonly used functions in the callback:

  • wp_enqueue_script
  • wp_enqueue_style

See https://developer.wordpress.org/reference/hooks/wp_enqueue_scripts/

NOTE: The corresponding hook for the admin pages is the admin_enqueue_scripts. Official docs: “admin_enqueue_scripts is the proper hook to use when enqueuing scripts and styles that are meant to be used in the administration panel“.

login_enqueue_scripts

Official docs: “Enqueue scripts and styles for the login page”.

add_action( 'login_enqueue_scripts', array( $this, 'my_callback' ) );

Commonly used functions in the callback:

  • wp_enqueue_script
  • wp_enqueue_style

See https://developer.wordpress.org/reference/hooks/login_enqueue_scripts/

wp_head

Official docs: “Prints scripts or data in the head tag on the front end”.

add_action( 'wp_head', array( $this, 'my_callback' ) );

Commonly used functions in the callback:

Usually, the callback returns code for CSS, Javascript, meta tags, etc to be included in the head section (e.g. Google Adsense code).

See https://developer.wordpress.org/reference/hooks/wp_head/

wp_body_open

Official docs: “Triggered after the opening body tag”.

add_action( 'wp_body_open', array( $this, 'my_callback' ) );

Commonly used functions in the callback:

Usually, the callback returns Javascript code to be included just after the opening body tag (e.g. Google Tag Manager code or Facebook SDK for JavaScript, etc).

See https://developer.wordpress.org/reference/hooks/wp_body_open/

wp_footer

Official docs: “Prints scripts or data before the closing body tag on the front end”.

add_action( 'wp_footer', array( $this, 'my_callback' ) );

Commonly used functions in the callback:

Usually, the callback returns Javascript code to be included just before the closing body tag.

See https://developer.wordpress.org/reference/hooks/wp_footer/

after_setup_theme

Official docs: “Fires after the theme is loaded”.

add_action( 'after_setup_theme', array( $this, 'my_callback' ) );

Commonly used functions in the callback:

  • add_theme_support
  • add_image_size

See https://developer.wordpress.org/reference/hooks/after_setup_theme/

widgets_init

Official docs: “Fires after all default WordPress widgets have been registered”.

add_action( 'widgets_init', array( $this, 'my_callback' ) );

Commonly used functions in the callback:

  • register_sidebar

See https://developer.wordpress.org/reference/hooks/widgets_init/

the_content

Official docs: “Filters the post content”.

add_filter( 'the_content', array( $this, 'my_callback' ) );

Commonly used functions in the callback:

Any PHP code to manipulate the post content.

See https://developer.wordpress.org/reference/hooks/the_content/

rest_api_init

Official docs: “Fires when preparing to serve a REST API request”.

add_action( 'rest_api_init', array( $this, 'my_callback' ) );

Commonly used functions in the callback:

  • register_rest_route
  • register_rest_field

See https://developer.wordpress.org/reference/hooks/rest_api_init/

Your comments are welcome!

Are you a website owner?

Enhance your website with custom features, optimizations, and seamless e-commerce integration. Let's discuss your project and achieve your online business goals.

Christos Pontikis