1

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.

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
    Aug 16, 2023 at 16:30
  • Thanks, In my defence: it worked for a number of years.
    – th00ht
    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
    Aug 17, 2023 at 10:15
  • @TomJNowell Thanks. that is indeed a good hint. It was a project I inherited.
    – th00ht
    Aug 17, 2023 at 11:41

1 Answer 1

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.

5
  • 1
    It does look like for backwards compatibility that wp_enqueue_script() will still accept a Boolean for $args -> github.com/WordPress/wordpress-develop/blob/6.3/src/wp-includes/…
    – Howdy_McGee
    Aug 16, 2023 at 15:43
  • 2
    @Howdy_McGee Ah, so it does! But using the in_footer variable name as OP does in the first block of text will still fail, as it no longer exists in the function declaration, which is why the call in OP's second attempt still works. Thanks for the catch!
    – YourManDan
    Aug 16, 2023 at 15:50
  • 1
    noting that the solution posted still uses the named function parameter feature that caused the problem in the first place, named function parameters are not supported by WP core and should be avoided until official support is announced. argument names can and do change
    – Tom J Nowell
    Aug 17, 2023 at 10:12
  • If you don't have dependencies or a version you can pass [], false for those parameters
    – Tom J Nowell
    Aug 17, 2023 at 10:14
  • I assume this would also work wp_enqueue_script( 'swiper-script', 'https://unpkg.com/swiper@8/swiper-bundle.min.js', args:["in_footer"=> true] ); Ofcourse until the WP core means to rename or remove the args parameter.
    – th00ht
    Aug 19, 2023 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.