Support » Themes and Templates » $wp_scripts->add_data($handle, 'group', 1) doesn't move scripts to footer

  • I’m looking to be educated. I’ve read source and am still scratching my head.

    This doesn’t work, and I’m wondering why and how to fix it (for purely academic reasons):

    function js_to_footer() {
    global $wp_scripts;
    set_footer_true( $wp_scripts->queue );
    function set_footer_true( $scripts ) {
    global $wp_scripts;
    foreach( $scripts as $handle ) {
    $wp_scripts->add_data( $handle, 'group', 1 );
    set_footer_true( $wp_scripts->registered[$handle]->deps );
    // looking at the registered scripts confirms their grouping changed
    // var_dump( $wp_scripts->registered[$handle] );
    add_action( 'wp_enqueue_scripts', 'js_to_footer', 11 );

    I really don’t understand why. Setting a lower priority leaves jQuery in the head, but they don’t get loaded at all if a higher priority is set.

    The code below is from Scripts to Footer plugin. It works, and I wonder why does simply removing these actions from the head get everything printed out in the footer?

    function custom_clean_head() {
    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_enqueue_scripts', 'custom_clean_head' );


Viewing 1 replies (of 1 total)
  • The bulk of the work is the second remove_action, remove wp_print_head_scripts stops the scripts from being printed in the header. The way the scripts are called and printed if they are not printed in the header they will be printed in the footer instead. The other two remove_actions are there to insure no scripts slide through.

    Check out wp-includes/script-loader.php.

Viewing 1 replies (of 1 total)
  • The topic ‘$wp_scripts->add_data($handle, 'group', 1) doesn't move scripts to footer’ is closed to new replies.