1

I figured this would have been a question already, but I haven't come across it.

I'm wondering what the correct hook for checking theme support is in a theme and also a plugin.

function my_theme_or_plugin_add_theme_support_feature()
    if (current_theme_supports( $feature )) {
        add_action( $hook, $function_to_add, $priority, $accepted_args );            
        add_filter( $tag, $function_to_add, $priority, $accepted_args );
    }
}
add_action( $hook, 'my_theme_or_plugin_add_theme_support_feature');

So in the above example when/where should the my_theme_or_plugin_add_theme_support_feature check, run in themes and plugins and is it necessary? Is it the same for both?

Examples:

add_action( 'wp_loaded', 'my_theme_or_plugin_add_theme_support_feature');

add_action( 'init', 'my_theme_or_plugin_add_theme_support_feature');

add_action( 'after_setup_theme', 'my_theme_or_plugin_add_theme_support_feature');

Update:

Actually I just found this question, which is very similar.

Also, here's a similar question I asked here which I was able to figure out using add_theme_support in a plugin... however, it doesn't work 100% of the time.

1 Answer 1

2

It really just depends on what you need to do here. The general method is to hook all your theme support features to after_setup_theme for some obvious reasons.

after_setup_theme is the first action hook available to themes to add any type of support the theme. Certain features has to be registered here and cannot be registered later. One feature that jumps to mind is support for post formats. post_format is a build-in taxonomy which is registered on init. If we skip the following actions:

  • auth_cookie_malformed

  • auth_cookie_valid

  • set_current_user

init will be the next hook after after_setup_theme to do plugin and theme related stuff on. As post_format are registered on init, adding theme support here will be too late with the wrong priority. So if you need to check for theme support for post formats, you need to do that on after_setup_theme with a very low priority. I tend to use the PHP_INT_MAX constant value as priority or PHP_INT_MAX + 1 . You can also maybe try set_current_user which runs after after_setup_theme and just before init

Generally, all build-in theme support features are executed on after_theme_support to keep consistancy in your code, but you should not rely on that at all. As @toscho described in his answer here, wp_loaded is an excellent hook to use to check if a certain feature is already supported or not (there are exceptions, see below note). By the time wp_loaded fires, all theme support features should have been added, if not, then that theme is doing something very wrong and you really cannot compromise your code to adjust for a bad theme. All hooks after wp_loaded are concerned with content output and manipulating the ouput which will be send to browser.

IMPORTANT NOTE

As I already stated, some features like post format support cannot be added on init or any hook after that as it is too late, so wp_loaded in this case would be way too late to add theme support for post formats. As I already stated you would need to hook your function as late as possible to after_setup_theme

CONCLUSION

There is now real correct hook to use here. The hook would depend on the support feature. In general it would be safe to use wp_loaded to check for a specific custom support feature and in my opinion this would be the latest hook which you should use to add any type of theme support feature. For the build-in support features, after_setup_theme with a very low priority (or even set_current_user) should suffice as init is too late to add features like post formats

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.