1

I used a PHP script to move all scripts to the footer of my site, but some plugins don't work anymore.

I used the following:

// JavaScript to footer --> läd alles im footer! 
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_print_head_scripts', 5 );  
add_action( 'wp_footer', 'wp_enqueue_scripts', 5 );    

Is there a way to exclude a single script from this PHP code?

  • wp_dequeue_script() – Mayeenul Islam Oct 19 '15 at 12:58
  • 1
    This is the second time I've seen a similar question today. May I ask, why do you want all scripts in the footer? – phatskat Oct 19 '15 at 13:53
  • 1
    mh.. if i use the the google pagespeed tool with the scripts in the head my site seems to be very slow for google. When i use the tool with the scripts in the footer i win 7 points and it seems to be faster... – tom84 Oct 19 '15 at 14:31
  • @Mayeenul Islam: Thanks for the Tip! But im a php noob. To prevent one Script from moving to the footer i can use your function. But i dont know exactly how to use. maybe so: function wpdocs_dequeue_script() { wp_dequeue_script( 'my Script??' ); } add_action( 'wp_print_scripts', 'wpdocs_dequeue_script', 100 ); – tom84 Oct 20 '15 at 18:22
  • Same problem here. I want to move all my scripts on footer except JQuery and another one that is mandatory to be placed in header in order to render properly my page. I would try to move every script in footer and THEN move specific scripts in header again but unfortunally it seems that is not possibile to CHANGE the placement through wp_enqueue_script(). – Marco Panichi Jun 13 '16 at 8:48
1

To stop enqueueing a script

We're dequeueing the scripts completely (regardless from header or footer) with a higher priority (10+):

function dequeue_my_scripts() {
   wp_dequeue_script('script-handle-here');
}
add_action( 'wp_print_scripts', 'dequeue_my_scripts', 11 );

To put the script to footer

function enqueue_scripts_to_footer() {
   wp_enqueue_script('script-handle-here');
}
add_action( 'wp_footer', 'enqueue_scripts_to_footer' );

Right way doing this

But the right way enqueueing the scripts in footer is setting the last parameter, of wp_enqueue_script() or wp_register_script(), to true:

function my_custom_scripts() {
    wp_enqueue_script( 'script-name', get_template_directory_uri() .'/js/example.js', array(), '1.0.0', true ); //last parameter to true
}
add_action( 'wp_enqueue_scripts', 'my_custom_scripts' );
  • Thank you for your good response. i dont know what i have to put in for 'script-name' and '/js/example.js' i tried several ways for example function my_custom_scripts() { wp_enqueue_script( 'text/javascript', get_template_directory_uri() .'/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.2.1', array(), '1.0.0', true ); but the script is still in the head. can you help me with this? The script in the source code looks like this: <script type='text/javascript' src='http://www.saale-praezisionswerkzeuge.de/wp/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.2.1'></script> – tom84 Nov 9 '15 at 15:28
  • i also tried your second solution: function enqueue_scripts_to_footer() { wp_enqueue_script('/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.2.1'); } add_action( 'wp_footer', 'enqueue_scripts_to_footer' ); but the script is still in die head :-( – tom84 Nov 9 '15 at 15:37
  • 1. Consult the codex pages, the links I provided. Try to understand first, don't go for a copy-paste solution, please. 2. Open the plugin file in editor, find the call wp_enqueue_script( in all the files there, you will find the handle of the scripts the plugin enqueued. Put the handle in put-handle-here. ...But first, try to understand how wp_enqueue_script() is working, otherwise you can't do the alternate (dequeue) thing. – Mayeenul Islam Nov 10 '15 at 5:09
  • I'm sorry @MayeenulIslam but I really don't understand how your explanation above could answer the question. Could you edit it and post the exact code that you will put in your functions.php in order to move every scripts to footer except one (for example, the jquery script)? – Marco Panichi Jun 13 '16 at 8:45
  • 1
    @MarcoPanichi I can dequeue script in my end. Tried dequeueing 'twentysixteen-html5', and it's successful. What's the problem in your end? Perhaps, ask a new question with your code snippet, pointing this answer that, how the answer is not solving your issue. I assume you are trying to dequeue a script that is enqueued from a plugin. – Mayeenul Islam Jun 13 '16 at 17:41

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.