admin管理员组

文章数量:1134238

We used to have the following script in functions.php that worked for many years:

add_action('wp_enqueue_scripts', function () {
  wp_enqueue_script('swiper-script', '@8/swiper-bundle.min.js', in_footer: true);
  wp_enqueue_style('swiper-style', "@8/swiper-bundle.min.css");
});

Out of the blue this was welcomed with:

PHP Fatal error:  Uncaught Error: Unknown named parameter $in_footer in....

Was the parameter renamed? I still see the same in the documentation. We now rely on

add_action('wp_enqueue_scripts', function () {
  wp_enqueue_script('swiper-script', '@8/swiper-bundle.min.js', false, array(), false, true);
  wp_enqueue_style('swiper-style', "@8/swiper-bundle.min.css");
});

which ofcourse also works but does not document very well.

We used to have the following script in functions.php that worked for many years:

add_action('wp_enqueue_scripts', function () {
  wp_enqueue_script('swiper-script', 'https://unpkg.com/swiper@8/swiper-bundle.min.js', in_footer: true);
  wp_enqueue_style('swiper-style', "https://unpkg.com/swiper@8/swiper-bundle.min.css");
});

Out of the blue this was welcomed with:

PHP Fatal error:  Uncaught Error: Unknown named parameter $in_footer in....

Was the parameter renamed? I still see the same in the documentation. We now rely on

add_action('wp_enqueue_scripts', function () {
  wp_enqueue_script('swiper-script', 'https://unpkg.com/swiper@8/swiper-bundle.min.js', false, array(), false, true);
  wp_enqueue_style('swiper-style', "https://unpkg.com/swiper@8/swiper-bundle.min.css");
});

which ofcourse also works but does not document very well.

Share Improve this question edited Aug 17, 2023 at 6:18 th00ht asked Aug 16, 2023 at 15:26 th00htth00ht 1748 bronze badges 4
  • That code uses PHP 8 named parameters, which isn't supported by WP, it's why PHP 8.X support is still in beta because core contributors haven't finished auditing function argument names etc, so any renaming or deprecation will break these. Until WP officially declares support you should avoid using named function arguments – Tom J Nowell Commented Aug 16, 2023 at 16:30
  • Thanks, In my defence: it worked for a number of years. – th00ht Commented Aug 17, 2023 at 6:22
  • you should not rely on the names staying the same. Also you may want to bundle the swiper JS bundle in your theme/plugin, 3rd party CDNs don't share their cache across different domains in modern browsers for security reasons so you're not actually speeding the site up by using unpkg, and you can't take advantage of HTTP2/3 to speed that up either so it might be slower not faster – Tom J Nowell Commented Aug 17, 2023 at 10:15
  • @TomJNowell Thanks. that is indeed a good hint. It was a project I inherited. – th00ht Commented Aug 17, 2023 at 11:41
Add a comment  | 

1 Answer 1

Reset to default 3

As of WordPress 6.3, the in_footer parameter is now bundled into the args array parameter (source).

The correct syntax for the wp_enqueue_script() function is now as follows (leaving out the named function parameter as it not officially supported by WP core):

wp_enqueue_script( string $handle, string $src = '', string[] $deps = array(), string|bool|null $ver = false, array|bool $args = array() )

In your case, you would write your function as follows:

$args = array(
    'in_footer' => 'true'
);

wp_enqueue_script('swiper-script', 'https://unpkg.com/swiper@8/swiper-bundle.min.js', [], false, $args);

Your revised wp_enqueue_script() function you now rely on works because there is still backwards compatibility for the in_footer parameter as @Howdy_McGee points out, (see here), but only if you're not specifying in_footer: true and instead just true since the parameter is removed from the function declaration.

本文标签: phpinfooter gives syntax error