template_redirect doesn't work if is in theme
-
In the moment I’m facing some strange behaviour when I try including javascript via wp_enqueue_script.
Ok … Since ages I include javascript like this:function my_scripts_init(){ wp_register_script('myscript1', get_stylesheet_directory_uri() . '/js/myscript1.js', array('jquery')); wp_enqueue_script('myscript1', true); wp_register_script('myscript2', get_stylesheet_directory_uri() . '/js/myscript2.js', array('jquery')); wp_enqueue_script('myscript2', true); wp_register_script('myscript3', get_stylesheet_directory_uri() . '/js/myscript3.js', array('jquery')); wp_enqueue_script('myscript3', true); if ( is_page( '22' ) ){ wp_register_script('googleMaps', 'http://maps.google.com/maps/api/js?sensor=false&language=de'); wp_register_script('my-map', get_stylesheet_directory_uri() . '/js/my-map.js', array('jquery')); wp_enqueue_script('googleMaps', true); wp_enqueue_script('my-map', true); } } add_action( 'template_redirect', 'my_scripts_init' );Works perfect … but not this time. It works if I remove
<?php wp_footer(); ?>from my theme (I know this because I forgot to include wp_footer() at first and everything was fine … except one plugin broke ;)), but if I don’t no script is included.To make it more weird if I split the including to several functions like:
function flexslider_init(){ wp_register_script('flexslider', get_stylesheet_directory_uri() . '/js/jquery.flexslider-min.js', array('jquery')); wp_register_script('my-slideshow', get_stylesheet_directory_uri() . '/js/my-slideshow.js', array('flexslider')); wp_enqueue_script('flexslider', true); wp_enqueue_script('my-slideshow', true); } add_action( 'template_redirect', 'flexslider_init' ); function map_init() { if ( is_page( '22' ) ){ wp_register_script('googleMaps', 'http://maps.google.com/maps/api/js?sensor=false&language=de'); wp_register_script('my-map', get_stylesheet_directory_uri() . '/js/my-map.js', array('jquery')); wp_enqueue_script('googleMaps', true); wp_enqueue_script('my-map', true); } } add_action( 'template_redirect', 'map_init' );then some parts are included and others not. For example jquery.flexslider-min.js is included but not my-slideshow.js and so on (there’s no consistent pattern which part will be included and which not).
Any ideas what could be wrong?
I tried to deactivate all plugins step by step, but it doesn’t help.
P.S. I would like to point to the fact that everything works fine if <?php wp_footer(); ?> is removed from my theme. So the path to the scripts should be correct and all scripts should be fine and the way I use wp_register_script() and wp_enqueue_script() should be ok.
P.P.S. I tried to use
add_action( 'wp_footer', 'my_scripts_init' );insteadadd_action( 'template_redirect', 'my_scripts_init' );.
This seems to work … in a way. The scripts are included but sometimes (to make it more weird) it ignores to include scripts that should only be in some pages. That means I use for exampleif (is_page(array('10', '16'))){ wp_register_script('jquery.tabs', get_stylesheet_directory_uri() . '/js/jquery.tabs.min.js', array('jquery') ); wp_register_script('my-tabs', get_stylesheet_directory_uri() . '/js/my-tabs.js'); wp_enqueue_script('jquery.tabs', true); wp_enqueue_script('my-tabs', true); }and it’s included in page 10 but not page 16.
Also if I do it this way (via the wp_footer hook) wp-minify seems to be incapable to grasp these scripts.
Strange. I am totally baffled.
The topic ‘template_redirect doesn't work if is in theme’ is closed to new replies.