Reputation: 127
I am trying to force WordPress to load some JS in the wp_footer() area of my theme. So far it loads them on both the footer and the head tag. Any ideas as to why?
function move_scripts() {
if (!is_admin()) {
wp_deregister_script('jquery');
wp_register_script('jquery','https://code.jquery.com/jquery-1.8.3.min.js', false, '1.8.3');
wp_register_script('migrate','https://code.jquery.com/jquery-migrate-1.4.1.min.js', false, '1.4.1');
wp_enqueue_script('jquery');
wp_enqueue_script('migrate');
wp_enqueue_script('gravity','/wp-content/plugins/gravityforms/js/gravityforms.min.js','','',true);
wp_enqueue_script('conditional','/wp-content/plugins/gravityforms/js/conditional_logic.min.js','','',true);
wp_enqueue_script('masked','/wpcontent/plugins/gravityforms/js/jquery.maskedinput.min.js','','',true);
}
}
add_action('wp_enqueue_scripts', 'move_scripts');
Thanks
Upvotes: 1
Views: 1675
Reputation: 5211
/************************************************************************************************************/
/* Move js to footer */
/************************************************************************************************************/
function remove_head_scripts() {
remove_action('wp_head', 'wp_print_scripts');
remove_action('wp_head', 'wp_print_head_scripts', 9);
remove_action('wp_head', 'wp_enqueue_scripts', 1);
add_action('wp_footer', 'wp_print_scripts', 5);
add_action('wp_footer', 'wp_enqueue_scripts', 5);
add_action('wp_footer', 'wp_print_head_scripts', 5);
}
add_action( 'wp_enqueue_scripts', 'remove_head_scripts' );
Upvotes: 2
Reputation: 58
if you're using the Gravity Forms plugin (and it's activated), some of these scripts might already be loaded by the plugin itself. Usually with plugins you don't need to add them to the functions.php of your theme.
If your move_scripts() works properly (looks to me like it should) you would then include another copy to the footer. That would explain your issue.
You can either try to deactivate the plugin (in WP Admin), and see if scripts are now only loaded in the footer, or you can comment your add_action line, and see if scripts are only loaded in the header.
Hope it helps…
P.S. I think you don't need that !is_admin check. To load scripts in the admin, it's a different action (admin_enqueue_scripts). This function (wp_enqueue_script) only targets the front pages.
Upvotes: 0