Feature : Toggle to prevent inline JS#75456
Feature : Toggle to prevent inline JS#75456Vrishabhsk wants to merge 1 commit intoWordPress:trunkfrom
Conversation
|
Warning: Type of PR label mismatch To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.
Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task. |
|
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
What?
disableInlineScriptattribute to the Categories block that allows developers to prevent the injection of inline JavaScript when using the block in dropdown mode. This provides better control over the block's output, especially when using block variations or custom navigation implementations.Why?
With the growing use of block variations and custom block implementations, developers often need to use the Categories block as a primitive building block. Currently, when displaying terms as a dropdown, the block automatically injects an inline script tag that handles client-side navigation. This can be problematic when:
The current workaround of manually removing the inline script is cumbersome and unreliable. This PR provides a clean, supported way to disable the inline script injection.
How?
The implementation adds a new optional attribute
disableInlineScript(boolean, defaults tofalse) to maintain backward compatibility:block.json): Added the newdisableInlineScriptattribute with default valuefalseedit.js): Added a toggle control in the block inspector that appears when "Display as dropdown" is enabled, allowing users to disable inline scriptsindex.php): Modified the render function to check thedisableInlineScriptattribute before injecting the inline JavaScript. When the attribute istrue, the script tag is omitted from the outputThe changes are backward compatible - existing blocks will continue to work as before since the default value is
false.Testing Instructions
Basic Testing
<script>tag should be present after the</select>tagControl Test (Default Behavior)
<script>tag should be present after the<select>element, enabling client-side navigationBlock Variation Testing (Developer Scenario)
Additional Notes
falseensures existing blocks continue to work as beforedisplayAsDropdownis enabled