WordPress Performance: Hook System: Advanced Customisation

9 minute read WordPress Performance · Part 3

Master actions, filters, priorities and custom hook creation for deeply flexible plugin and theme development in WordPress.

Hooks are the backbone of extensibility in WordPress. Mastering them lets you customise without modifying core code.

Here’s how to go beyond add_action() and add_filter() basics.

1. Actions vs Filters

Actions do something:

add_action( 'init', 'register_custom_post_type' );

Filters modify and return something:

add_filter( 'the_title', 'prefix_title' );

2. Priority and Arguments

Hooks accept priority and argument count:

add_filter( 'the_content', 'add_cta', 20, 1 );
  • Lower priority runs earlier
  • Use argument count to match parameters:
add_filter( 'excerpt_more', 'custom_ellipsis', 10, 2 );

3. Anonymous Functions

For inline logic:

add_action( 'wp_footer', function() {
    echo '<!-- Tracking code -->';
});

Note: Harder to remove later.

4. Remove Hooks

Remove default behaviour:

remove_action( 'wp_head', 'wp_generator' );

Remove plugin logic conditionally:

remove_filter( 'the_content', 'some_plugin_output' );

5. Custom Hooks

Create your own in plugins or themes:

do_action( 'myplugin_before_form' );

And listen for them elsewhere:

add_action( 'myplugin_before_form', 'custom_notice' );

6. Debugging Hook Order

Use did_action() or current_filter() to introspect:

if ( did_action('init') ) { log_it('Init has run'); }
echo current_filter();

Plugins like Query Monitor can show live hook firing order.

7. Best Practices

  • Prefix hook names to avoid clashes: prefix_hook_name
  • Document expected arguments in custom hooks
  • Avoid excessive nested hooks

Real-World Application

In a modular theme refactor for a performance project:

  • Custom hooks defined around template parts
  • Filters used to modify menu logic, breadcrumbs, and excerpts
  • Action priorities adjusted for third-party plugin compatibility