Skip to content

Administration: Use nowdocs for title-field autofocus inline scripts - #16318

Open
shail-mehta wants to merge 2 commits into
WordPress:trunkfrom
shail-mehta:add/66856-autofocus-inline-js
Open

shail-mehta wants to merge 2 commits into
WordPress:trunkfrom
shail-mehta:add/66856-autofocus-inline-js

Conversation

@shail-mehta

Copy link
Copy Markdown
Member

What

Trac ticket: #66856

Moves the four title/name field autofocus inline scripts from raw <script> tags to wp_print_inline_script_tag() with <<<'JS' nowdocs:

  • src/wp-admin/edit-form-advanced.php
  • src/wp-admin/edit-form-comment.php
  • src/wp-admin/edit-tag-form.php
  • src/wp-admin/edit-tags.php

The JavaScript is unchanged.

Why

#66856 lists these four title-field autofocus one-liners as remaining work. Using wp_print_inline_script_tag() removes raw <script> tags for CSP compatibility, and <<<'JS' nowdocs preserve IDE syntax highlighting.

How

Replace each raw <script> block with wp_print_inline_script_tag( <<<'JS' … JS ). No PHP values are interpolated, so no wp_json_encode() is needed.

Testing Instructions

  1. Run php -l on all four modified files and confirm no syntax errors.
  2. On desktop, open:
    • Classic Editor Add New Post (wp-admin/post-new.php) — confirm the title field receives focus.
    • Edit Comment (wp-admin/comment.php?action=editcomment&c=…) — confirm the author name field receives focus.
    • Categories / Tags list (wp-admin/edit-tags.php?taxonomy=category) — confirm the Name field in Add New Category receives focus.
    • Edit Category / Tag (wp-admin/term.php?taxonomy=category&tag_ID=…) — confirm the Name field receives focus.
  3. View page source on each screen and confirm the inline <script> is printed cleanly with no console errors.

Use of AI Tools

  • Cursor

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the administration autofocus updates aimed at improving CSP compatibility.

No open issues 🎉

What changed in this PR

Updates four administration autofocus scripts to use WordPress’s inline script helper for CSP compatibility, addressing the work described in Trac #66856.

Changes:

  • Replaces raw <script> tags with wp_print_inline_script_tag() and JS nowdocs.
  • Preserves the JavaScript and existing conditions controlling autofocus.
File Description
src/​wp-admin/​edit-tags.php Converts the Add New Category/Tag name autofocus script.
src/​wp-admin/​edit-tag-form.php Converts the term-editing name autofocus script.
src/​wp-admin/​edit-form-comment.php Converts the comment author name autofocus script.
src/​wp-admin/​edit-form-advanced.php Converts the Classic Editor title autofocus script.

Review effort: Balanced

@github-actions

Copy link
Copy Markdown

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

  • 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.

@shail-mehta
shail-mehta marked this pull request as ready for review November 30, 2026 12:53
@shail-mehta shail-mehta self-assigned this Nov 30, 2026
@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

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

Props shailu25, mukesh27.

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@mukeshpanchal27 mukeshpanchal27 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Optional: the one-liners could be formatted like other converted scripts, as in r63545. One example inline; the same applies to the other three files.

Comment thread src/wp-admin/edit-form-advanced.php Outdated
if ( ! wp_is_mobile() && post_type_supports( $post_type, 'title' ) && '' === $post->post_title ) {
wp_print_inline_script_tag(
<<<'JS'
try{document.post.title.focus();}catch(e){}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional formatting:

Suggested change
try{document.post.title.focus();}catch(e){}
try {
document.post.title.focus();
} catch ( e ) {}

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.

3 participants