-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Interactivity API: Move Store's data encoding to the echo
call
#51974
Merged
DAreRodz
merged 3 commits into
trunk
from
interactivity-store-json-encode-inside-render
Jul 24, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you considered using native WordPress functions like
wp_register_script
that support inline scripts? They give better control over where the code is going rendered in the document, and plugin authors can use filtering if necessary.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that would work, you can connect it with the existing interactivity script handle and put it before or after depending on the actual use case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect, thanks. Added to the roadmap 🙂👍️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gziolo, do you know if
wp_add_inline_script
works withapplication/json
scripts? 👀There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DAreRodz, most likely, you would need to use WP hooks to inject that custom type.
@adamsilverstein or @westonruter, what would you advise to handle in this special case? Is it something that we should bake into
wp_add_inline_script
in WP core?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As of WordPress 6.3, such inline scripts get printed via
\WP_Scripts::get_inline_script_tag()
which finally makes use ofwp_get_inline_script_tag()
. This function fortunately includes awp_inline_script_attributes
filter for manipulating the inline script's attributes, such as adding anonce
or even change thetype
toapplication/json
. Nevertheless, since this is a low-level function it isn't aware of the script handle specifically, although there is anid
attribute that is passed. So if the script handle wasinteractivity
and the script was abefore
inline script, this should work:I'm not sure whether it makes sense to add a more direct API to change the type of such inline scripts without seeing whether there are more such use cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@westonruter, thank you so much for the detailed explanation. The
id
should be enough in this case as it is standardized based on the script handle and always included for a few major WP releases.Let's see if
wp_add_inline_script
needs to be extended. If it's one of case, then the filter is more than enough.