Search code examples
wordpressadd-filter

add_filter priorities not working in correct order (Wordpress)


My Wordpress filters seem to be executing in the wrong order.

I've added filters in this way:

add_filter( 'the_content', 'display_a_single_property', 10, 1 );
add_filter( 'the_title', 'change_the_page_title', 11, 2 );
add_filter( 'wp_title', 'change_the_meta_title', 100, 1 );

Strangely the order of callback execution is:

  • change_the_meta_title()
  • display_a_single_property()
  • change_the_page_title()

According to the documentation for add_filter() the lower the priority numeric, the earlier the execution. However this isn't happening here. Could it be a theme or plugin issue? I'm using Yoast SEO plugin and Colormag theme.


Solution

  • Priorities only order callbacks within each action hook / filter. They don't order the hooks themselves (which always run in the same order).

    For example, 'wp_title' always runs before 'the_content', which in turn always runs before 'the_title'.

    The only time priorities come into effect is if you have multiple callbacks added to the same hook (for example, two different callbacks defined for 'the_content').