Make WordPress Core

Opened 14 months ago

Last modified 3 months ago

#59972 new defect (bug)

Clarify description of wp_script_add_data()

Reported by: crstauf's profile crstauf Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Script Loader Keywords: has-patch
Focuses: docs Cc:

Description

The function description does not include strategy as a possible value for $key. Suggest to adjust to indicate function is a wrapper for WP_Scripts::add_data(), and then add possible values conditional and strategy to WP_Scripts::add_data() function description or parameter description.

This documentation does not make it very clear that this can’t be used for adding arbitrary values attributes to the script tag. It only supports ‘conditional’ or, more recently, a “strategy” value.

For adding an alternate type attribute, for example, you’ll still need to use older strategies like the script_loader_tag filter.

See this StackExchange post for details.

Reported by @mrwweb via User Contributed Notes.

Change History (4)

#1 @sabernhardt
6 months ago

  • Component changed from General to Script Loader
  • Keywords needs-patch added

#2 @niravsherasiya7707
6 months ago

@crstauf I recommend updating the function name because wp_script_add_data might raise the question, "Which data?".

The purpose of this function is to add attributes to the script if wp_script_add_data matches the 'browser version' when the 'conditional' value is passed as $key or the strategy 'defer' or 'async' is used.

This ticket was mentioned in PR #7539 on WordPress/wordpress-develop by @debarghyabanerjee.


3 months ago
#3

  • Keywords has-patch added; needs-patch removed

Trac Ticket: Core-59972

## Overview

  • This PR enhances the documentation for the wp_script_add_data function to clarify its purpose and usage. It updates the comments to provide more detailed information on the allowed keys, specifically highlighting the conditional and strategy options. The documentation now explicitly states that this function is a wrapper for WP_Scripts::add_data() and does not support adding arbitrary attributes to the script tag.

## Changes Made

### Updated the function comments to:

  • Clarify that wp_script_add_data does not support arbitrary attributes for the script tag.
  • Provide examples of possible values for the $key parameter, including conditional and strategy.
  • Mention supported strategies such as defer and async.

## Benefits

  • Improves clarity and usability of the documentation for developers using the wp_script_add_data function.
  • Helps prevent misuse of the function by clearly stating its limitations regarding arbitrary attributes.
  • Provides concrete examples to guide developers in effectively utilizing the strategy key.

@hellofromTonya commented on PR #7539:


3 months ago
#4

Hello @Debarghya-Banerjee, Thank you for submitting this patch.

Data is not limited to only 'strategy' or 'conditional'. It can be any key along with a scalar data value.

For example:

  • group key

wp_register_script() and wp_enqueue_script() add the 'group' key when 'in_footer' is set.

  • 'data' key
  • WP_Script::has_inline_script() checks for 'before' and 'after'.

https://core.trac.wordpress.org/browser/trunk/src/wp-includes/class-wp-scripts.php#L996

which has a wrapper wp_add_inline_script() which when tracing uses the same underlying WP_Scripts::add_data().

Note: See TracTickets for help on using tickets.