Skip to content

Add a filter to leave shortcode content unformatted in wpautop - #16275

Open
Vedant-Gandhi wants to merge 4 commits into
WordPress:trunkfrom
Vedant-Gandhi:feat/no-autop-shortcodes
Open

Vedant-Gandhi wants to merge 4 commits into
WordPress:trunkfrom
Vedant-Gandhi:feat/no-autop-shortcodes

Conversation

@Vedant-Gandhi

Copy link
Copy Markdown

Description

Adds an opt-in no_autop_shortcodes filter to wpautop(). Shortcodes listed there, including the content between their opening and closing tags, are left as written instead of getting <p> and <br /> tags.

It is inspired from the precedent of no_texturize_shortcodes filter. The default is an empty array, so nothing changes unless a plugin or theme opts in:

add_filter(
    'no_autop_shortcodes',
    static function ( $shortcodes ) {
        $shortcodes[] = 'code';
        return $shortcodes;
    }
);

Why: wpautop() runs before do_shortcode(), so a shortcode callback receives content that has already been paragraphed. In the ticket's example, a [code] shortcode wrapping PHP arrives with <p> and </p> inserted into the code. Changing this for every shortcode would break the ones that expect paragraphed text, and moving do_shortcode() earlier would break many plugins (see comment 12 on the ticket), so this is opt-in.

How:

  • Matches the listed shortcodes with get_shortcode_regex() and swaps each match for a placeholder, the way <pre> blocks are handled.
  • Escaped shortcodes ([[code]]) are skipped, as in do_shortcode_tag().
  • Placeholders are replaced with the original text after autop, before the <pre> blocks are restored.
  • The filter is only applied when the text contains a [.

Notes:

  • The filter is opt-in instead of on by default as it can break existing functionality.
  • Only content that goes through wpautop() is affected.
  • Known limitation: a <pre> inside a protected shortcode ends up nested in a <p>.

Tests are located in tests/phpunit/tests/formatting/wpAutop.php. To run tests, execute the following command:

npm run test:php -- --filter Tests_Formatting_wpAutop

Trac ticket: https://core.trac.wordpress.org/ticket/6984

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5.5
Used for: Investigating the ticket, drafting the implementation and tests, and writing this description. I reviewed the diff, ran the changes, and edited and take responsibility for the final code.

@github-actions

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

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.

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

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.

1 participant