Skip to content

Conversation

@dmsnell
Copy link
Member

@dmsnell dmsnell commented Sep 18, 2025

Trac ticket: Core-61501
See: #6429

As part of opening up custom data attributes, it’s important for Core to know what is and what isn’t a custom data attribute. Further, it’s important to know what the translated name of a custom data attribute is when read via the JavaScript .dataset property.

This patch introduces two new functions to transform between these two representations. While this started as a one-way transform from custom attribute name to dataset property name, it became apparent that the reverse transform could be just as confusing in some cases as the forward transform. Therefore, a second function has been added to lower the risk of producing a mistaken custom data attribute.


With this patch, developers can call wp_html_custom_data_attribute_name( 'postId' ) to get the name of the proper HTML attribute name such that it will match the JS code, building correlations between the JS and PHP sources. In this case, it would return data-post-id.

More complicated cases make the relevance of having well-defined functions more obvious:

  • data-test <> test
  • data-1 <> 1
  • data-😢-8 <> 😢-8
  • data-one:more <> one:more
  • data-CAPS <> caps (<> data-caps)
  • DATA--UPPER-alreadY <> UpperAlready (<> data--upper-already)
  • data---one---two--- <> -One--Two---
  • data- <> ``

@dmsnell dmsnell force-pushed the js/add-dataset-name branch from a94d150 to a2d5a53 Compare September 18, 2025 20:52
@dmsnell dmsnell marked this pull request as ready for review September 18, 2025 20:53
@github-actions
Copy link

github-actions bot commented Sep 18, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props dmsnell, westonruter.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@dmsnell dmsnell force-pushed the js/add-dataset-name branch 2 times, most recently from 233a214 to f85c1a8 Compare September 19, 2025 04:54
@dmsnell dmsnell force-pushed the js/add-dataset-name branch 5 times, most recently from 5142726 to e467ef3 Compare September 25, 2025 18:47
@dmsnell dmsnell force-pushed the js/add-dataset-name branch 5 times, most recently from 25ecd94 to 86333ab Compare October 6, 2025 21:18
@dmsnell dmsnell force-pushed the js/add-dataset-name branch 4 times, most recently from 1a1a75d to d9f2e6a Compare October 9, 2025 23:39
@dmsnell
Copy link
Member Author

dmsnell commented Oct 17, 2025

@peterwilsoncc @westonruter do either of you have thoughts about the structure of this proposed new module? It serves the work opening up custom data attributes by providing the necessary functions to parse them.

But it introduces another module in the top-level of wp-includes too. I think there could be plenty of new JS-interop functions, and there are some Trac tickets open for such things, so I reasoned that it might make sense to collect them in one place.

@westonruter
Copy link
Member

@dmsnell I love this.

In regards to where the functions should be located, to me it seems they may make sense being added to src/wp-includes/script-loader.php. There are other related functions in there.

@dmsnell
Copy link
Member Author

dmsnell commented Oct 18, 2025

@westonruter I like this idea, but once I started implementing it I started liking it less.

  • script-loader is full of larger and more integrated functionality for generating HTML on the rendered page.
  • it’s a huge module with over three thousand lines.

my idea with js-interop.php is that it could be full of relatively isolated and pure functions whose purpose is purely to translate between PHP and JavaScript.

I added your changes in bb63d3c, so I would like to know your thoughts after the change and if you think it’s worth the tradeoffs to group it in that existing file.

@dmsnell dmsnell force-pushed the js/add-dataset-name branch from 790741c to 4fa1e2c Compare October 21, 2025 04:24
@dmsnell
Copy link
Member Author

dmsnell commented Oct 21, 2025

@westonruter I’m going to merge as-is, but I would really love some additional thought on this: if we leave it in script-loader.php then it’s not available until a lot of other modules such as post.php and temp†e.php. If those are only providing functions I guess it’s all the same, but I want to double-check that we aren’t inviting issues with calling these functions in theme code before it’s been loaded.

I expect the if we identify problems during the Beta period we can move these back into their own new module.

pento pushed a commit that referenced this pull request Oct 21, 2025
This patch introduces two new functions: `wp_js_dataset_name()` and `wp_html_custom_data_attribute_name()`. Together, these provide reliable mapping between the HTML attribute names for custom data attributes, and the properties found in JavaScript for a given `HTMLElement`’s `.dataset` property.

These are to be used where matching names is important, such as in the Interactivity API and when WordPress is deciding whether or not to allow an attribute as a custom data attribute.

Developed in #9953
Discussed in https://core.trac.wordpress.org/ticket/61501

Props dmsnell, westonruter.

See #61501.


git-svn-id: https://develop.svn.wordpress.org/trunk@61007 602fd350-edb4-49c9-b593-d223f7449a82
@dmsnell
Copy link
Member Author

dmsnell commented Oct 21, 2025

Merged in 16c66d7
[61007]

@dmsnell dmsnell closed this Oct 21, 2025
@dmsnell dmsnell deleted the js/add-dataset-name branch October 21, 2025 05:11
markjaquith pushed a commit to WordPress/WordPress that referenced this pull request Oct 21, 2025
This patch introduces two new functions: `wp_js_dataset_name()` and `wp_html_custom_data_attribute_name()`. Together, these provide reliable mapping between the HTML attribute names for custom data attributes, and the properties found in JavaScript for a given `HTMLElement`’s `.dataset` property.

These are to be used where matching names is important, such as in the Interactivity API and when WordPress is deciding whether or not to allow an attribute as a custom data attribute.

Developed in WordPress/wordpress-develop#9953
Discussed in https://core.trac.wordpress.org/ticket/61501

Props dmsnell, westonruter.

See #61501.

Built from https://develop.svn.wordpress.org/trunk@61007


git-svn-id: http://core.svn.wordpress.org/trunk@60343 1a063a9b-81f0-0310-95a4-ce76da25c4cd
github-actions bot pushed a commit to platformsh/wordpress-performance that referenced this pull request Oct 21, 2025
This patch introduces two new functions: `wp_js_dataset_name()` and `wp_html_custom_data_attribute_name()`. Together, these provide reliable mapping between the HTML attribute names for custom data attributes, and the properties found in JavaScript for a given `HTMLElement`’s `.dataset` property.

These are to be used where matching names is important, such as in the Interactivity API and when WordPress is deciding whether or not to allow an attribute as a custom data attribute.

Developed in WordPress/wordpress-develop#9953
Discussed in https://core.trac.wordpress.org/ticket/61501

Props dmsnell, westonruter.

See #61501.

Built from https://develop.svn.wordpress.org/trunk@61007


git-svn-id: https://core.svn.wordpress.org/trunk@60343 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants