• I am looking for a way to get a url to the theme to be used with my javascrpt. I am using requirejs and need a baseUrl.

    I thought I could do something like this in my functions file:

    function init_themeRootReference(){
    	// Set up js varibale for root directory
    	$my_theme_dir = array( 'path' => get_stylesheet_directory_uri() );
    	wp_localize_script( 'my-dependent', 'themeRootReference', $my_theme_dir );
    }
    add_action( 'after_setup_theme', 'init_themeRootReference' );

    And then get that in my js file like so:
    baseUrl = themeRootReference.path

    I think the problem I am having is that the js fires before the add_action() call and so the variable in js is undefined. Is there an action in WordPress that comes sooner? Or are there other ways I can set this up?

    Thank you very much for any help with this!!!

  • The topic ‘Absolute js path to theme?’ is closed to new replies.