I need to know the templates directory uri at a point at which the function get_template_directory_uri() is not yet available.

The function where I need the uri is triggered by the action after_switch_theme.

So is there any other way to achieve this? Or can I make is somehow available?


It is a sign that you are "doing it wrong". you should not assume the existance or initialization of anything before the init action was fired, or even better to wait for wp_loaded

| improve this answer | |
  • I tried the same thing with wp_load instead of after_switch_theme as this would still work in my case, but get_template_directory_uri() is still unknown. – TheKidsWantDjent Aug 28 '17 at 17:43
  • well, maybe it is time to actually put your code in the question so we all will be able to see what we are talking about ;) – Mark Kaplun Aug 28 '17 at 18:34

after_switch_theme passes the old theme's name and an instance of WP_Theme class. So, you should be able to use that to construct a path for your theme's URI.

Here's an example:

add_action( 'after_switch_theme', 'my_function', 10, 2 );
function my_function( $old_theme_name, $old_theme = false ){
    if( $old_theme ){
        $uri = $old_theme->get_template_directory_uri();

This is only valid if the old theme still exists.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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