D
D
Darknirk2021-07-21 23:58:11
WordPress
Darknirk, 2021-07-21 23:58:11

How to move styles to the footer in the new WordPress 5.8?

Previously working code stopped working in the new version of WP.

function footer_enqueue_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('after_setup_theme','footer_enqueue_scripts');

Displays an error
: Trying to get property 'queue' of non-object in
site\wp-includes\script-loader.php
on line
2652


Warning
: Invalid argument supplied for foreach() in
site\wp-includes\script-loader.php
on line
2652

Code to wrap all 'wp_enqueue_scripts' styles in the footer.
What to replace it with? Or how to fix?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Litvinenko, 2021-07-22
@Darknirk

// Move jQuery to the footer
function theme_move_jquery_to_footer() {
  wp_scripts()->add_data( 'jquery', 'group', 1 );
  wp_scripts()->add_data( 'jquery-core', 'group', 1 );
  wp_scripts()->add_data( 'jquery-migrate', 'group', 1 );
}
add_action( 'wp_enqueue_scripts', 'theme_move_jquery_to_footer' );

this will move all scripts to the footer if you specify them as dependent on the jekware.
Alternative Solution
// Enqueue scripts and styles.
function theme_scripts() {
  wp_deregister_script( 'jquery' );
  wp_register_script( 'jquery', includes_url( '/js/jquery/jquery.min.js' ), false, NULL, true );
  wp_enqueue_script( 'jquery' );
}
add_action( 'wp_enqueue_scripts', 'theme_scripts' );

I
Igor Vorotnev, 2021-07-22
@HeadOnFire

You shouldn't do that, it's an anti-pattern and a wild crutch.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question