Opened 14 months ago
Last modified 3 months ago
#59972 new defect (bug)
Clarify description of wp_script_add_data()
Reported by: | 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 thescript_loader_tag
filter.
Reported by @mrwweb via User Contributed Notes.
Change History (4)
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 theconditional
andstrategy
options. The documentation now explicitly states that this function is a wrapper forWP_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, includingconditional
andstrategy
.
- Mention supported strategies such as
defer
andasync
.
## 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
- Customizer adds a `'data'` key for its settings.
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()
.
@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.