Skip to content

Strip HTML tags in privacy policy link - #11088

Open
shail-mehta wants to merge 13 commits into
WordPress:trunkfrom
shail-mehta:update/privacy-policy-link-title-escaping
Open

shail-mehta wants to merge 13 commits into
WordPress:trunkfrom
shail-mehta:update/privacy-policy-link-title-escaping

Conversation

@shail-mehta

@shail-mehta shail-mehta commented Feb 28, 2026 •

Copy link
Copy Markdown
Member

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

  • Used wp_kses() instead of esc_html()
Before After
before-patch after-patch

Use of AI Tools

  • Cursor

This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@github-actions

github-actions Bot commented Feb 28, 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. 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 shailu25, joedolson, westonruter, huzaifaalmesbah, mukesh27, sabernhardt, masteradhoc, peterwilsoncc.

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

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

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

Looks Good

Before Apply Patch After Apply Patch ✅
Before Apply Patch After Apply Patch

Comment thread src/wp-includes/link-template.php Outdated
'<a class="privacy-policy-link" href="%s" rel="privacy-policy">%s</a>',
esc_url( $privacy_policy_url ),
esc_html( $page_title )
wp_kses_post( $page_title )

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.

Technically, since get_the_title() was used, is Kses even needed? True it would be nicer for late escaping, but it would also be redundant:

Suggested change
wp_kses_post( $page_title )
$page_title

Not saying we should do this, but I wanted to point this out.

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.

WP core use esc_html for get_the_title in many places https://github.com/search?q=repo%3AWordPress%2Fwordpress-develop%20esc_html(%20get_the_title&type=code Do we needs to open separate issue for this to review all those instances?

Can we update the codebase something like:

$link               = '';
$privacy_policy_url = get_privacy_policy_url();
$policy_page_id     = (int) get_option( 'wp_page_for_privacy_policy' );

if ( $privacy_policy_url && $policy_page_id ) {
	$link = sprintf(
		'<a class="privacy-policy-link" href="%s" rel="privacy-policy">%s</a>',
		esc_url( $privacy_policy_url ),
		get_the_title( $policy_page_id ) ?? ''
	);
}

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.

Here is a smaller set with a more tailored regex: https://github.com/search?q=repo%3AWordPress%2Fwordpress-develop+%2Fesc_html%5C%28+get_the_title%5C%28%2F&type=code

This doesn't catch cases where get_the_title() is stored in a variable and then later echoed.

I think it would sense to change the scope of the ticket to address this issue for all these cases.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

get_the_title() needs to keep some escaping/cleaning.

Consider this absurdly extreme example for the title I added in Quick Edit:
<p class="privacy-policy-link"><span style="color:darkgreen">P</span><b>r</b><i>i</i><strong>v</strong><em>a</em><code>c</code><kbd>y</kbd></p><div>&nbsp;</div><h1>P</h1><h2>o</h2><h3>l</h3><h4>i</h4><h5>c</h5><h6>y</h6><span> & <a>Terms</a></span><style>.privacy-policy-link{font-size:1.2em}</style><script>console.log('privacy-policy-link');</script>

The link(s) should not allow style or script tags, the a element does not fit within a link, and I do not know of a case in which any of the block elements would be appropriate.

If someone wants the entire link text bold or italic, that would be better in CSS than adding HTML in the title wherever it appears. In the case when someone wants part of the title emphasized, such as <strong>Privacy</strong> Policy, the link filter can un-escape the desired element right now with esc_html().

function enable_strong_in_the_privacy_policy_link( $link ) {
	if ( str_contains( $link, '&lt;' ) ) {
		$link = str_replace(
			array( '&lt;strong&gt;', '&lt;/strong&gt;' ),
			array( '<strong>', '</strong>' ),
			$link
		);
	}

	return $link;
}
add_filter( 'the_privacy_policy_link', 'enable_strong_in_the_privacy_policy_link' );

Similarly, if the function switches to wp_strip_all_tags(), someone could use the filter to replace Privacy with <strong>Privacy</strong>.

If we have clear examples of appropriate tags to start allowing for any site, the function(s) could use
wp_kses( $page_title, array( 'strong' => array(), 'em' => array() ) )

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think there's a reasonable case for span, as well. From a design perspective, you sometimes want markup that doesn't have pre-defined style expectations or existing semantics. But I can't see a strong case outside those limits.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@shail-mehta would you be open to expand the PR to support:

  • strong
  • em
  • b
  • i
  • span (with css classes)

As well it would be great if we could make it consistent inside the codebase. A unit test could help cover this moving forward.

@shail-mehta

Copy link
Copy Markdown
Member Author

Should we consider removing esc_html() across all related instances?

@westonruter

westonruter commented Feb 28, 2026 •

Copy link
Copy Markdown
Member

Yes.

Update: But probably with KSES to allowlist a specific set of formatting tags.

Copilot AI review requested due to automatic review settings July 24, 2026 19:28

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@masteradhoc

Copy link
Copy Markdown

@shail-mehta as we have a way forward would you mind checking #11088 (comment) and let me know if you'd like to move this forward?

@shail-mehta

shail-mehta commented Jul 24, 2026 •

Copy link
Copy Markdown
Member Author

Sure, I'll work on this PR this weekend.

Copilot AI review requested due to automatic review settings July 25, 2026 03:41

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.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

src/wp-includes/link-template.php:4823

  • This change modifies how the privacy policy page title is sanitized in the rendered link, but there is no test coverage for titles containing HTML. Adding a unit test case (e.g. a privacy policy page title with <img>/<a>/<strong> tags) would prevent regressions and codify the expected “strip tags” behavior.
			wp_kses_post( $page_title )

Comment thread src/wp-includes/link-template.php Outdated
Copilot AI review requested due to automatic review settings July 25, 2026 04:33

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.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.

Comment thread src/wp-admin/comment.php
Comment thread src/wp-includes/link-template.php
Comment thread src/wp-includes/link-template.php
Copilot AI review requested due to automatic review settings July 25, 2026 04:39

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.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (2)

src/wp-admin/comment.php:237

  • There is an extra closing tag, which produces invalid table markup (double-closing the same cell). Remove the redundant closing tag to keep the table structure valid.
	</td>
	</td>

src/wp-includes/link-template.php:4832

  • The PR description says this change uses wp_kses_post(), but the implementation uses wp_kses() with a custom allowlist (and the PR title/description focus on the privacy policy link, while this PR also changes multiple other title outputs). Please align the PR description/title with the implemented approach/scope, or adjust the implementation if wp_kses_post()/different scoping is the intended requirement.
		$link = sprintf(
			'<a class="privacy-policy-link" href="%s" rel="privacy-policy">%s</a>',
			esc_url( $privacy_policy_url ),
			wp_kses(
				$page_title,
				array(
					'strong' => array(),
					'em'     => array(),
					'b'      => array(),
					'i'      => array(),
					'span'   => array(),
				)
			)

Copilot AI review requested due to automatic review settings July 25, 2026 04:46

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.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

src/wp-admin/comment.php:237

  • There is an extra closing here, which produces invalid table markup (two consecutive tags for the same cell).
	</td>
	</td>

Comment on lines +128 to +139
<a href="<?php echo esc_url( get_permalink( $year_post ) ); ?>">
<?php
echo wp_kses(
$title,
array(
'strong' => array(),
'em' => array(),
'b' => array(),
'i' => array(),
'span' => array(),
)
);
Copilot AI review requested due to automatic review settings July 25, 2026 05:03

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.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

src/wp-admin/comment.php:240

  • There is an extra closing </td> in this row, which results in invalid table markup and can break the layout of the comment edit screen.
	</td>
	</td>
</tr>

Comment thread tests/phpunit/tests/link/getThePrivacyPolicyLink.php Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 25, 2026 05:18

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.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (2)

src/wp-admin/comment.php:240

  • There is an extra closing which results in invalid table markup (a is closed twice). This can break the layout of the comment edit screen.
	</td>
	</td>
</tr>

src/wp-includes/link-template.php:4825

  • The PR title/description focus on the privacy policy link, but this change set also updates title sanitization/output in several other admin and theme templates (e.g. comment screens, nav menu editor, dashboard widget, Twenty Twenty-One). If that broader scope is intended, please update the PR title/description accordingly; otherwise consider splitting/reducing the changes to the privacy-policy-link fix for easier review.
		$link = sprintf(
			'<a class="privacy-policy-link" href="%s" rel="privacy-policy">%s</a>',
			esc_url( $privacy_policy_url ),
			wp_kses(
				$page_title,
				array(

Comment thread tests/phpunit/tests/link/getThePrivacyPolicyLink.php Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 25, 2026 05:24

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.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

src/wp-admin/comment.php:240

  • There is an extra closing </td> here, which results in invalid table markup (two </td> closing tags for the same cell).
	</td>
	</td>
</tr>

Comment on lines +90 to +114
'post_title' => '<strong>Privacy</strong> <em>Policy</em> <b>Bold</b> <i>Italic</i> <script>alert("test")</script>',

)

$privacy_policy_page_id = self::$privacy_policy_page_id;
$filter = static function ( $title, $post_id ) use ( $privacy_policy_page_id ) {
if ( (int) $privacy_policy_page_id === (int) $post_id ) {
return '<span class="page-title">' . $title . '</span>';
}

return $title;
};

add_filter( 'the_title', $filter, 10, 2 );
}



return $title;

};



add_filter( 'the_title', $filter, 10, 2 );
Comment thread src/wp-admin/comment.php Outdated
Comment on lines +206 to +218
$post_title = wp_kses(
get_the_title( $post_id ),
array(
'strong' => array(),
'em' => array(),
'b' => array(),
'i' => array(),
'span' => array(
'class' => true,
),

)
);

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.

This feels like duplicate logic across multiple files. I'm not sure if it's worth it, but perhaps we could extract it into a utility function and reuse it instead.

WDYT?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes I agree

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I will update it

@peterwilsoncc peterwilsoncc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it would be best to keep this focused on the display of the privacy policy link as that is affecting the front end display.

That will allow for the display of titles within the admin to be handled as a separate issue and discussed with a focus on what is best within the admin.

Merge latest trunk and keep HTML formatting support only on the front-end privacy policy link, leaving admin title display for a separate change.
The file was saved with CRLF, so the pull request diff rewrote every line.
The markup test did not parse and had extra blank lines that fail PHPCS.
Inject title markup through the_title after core formatting filters so the assertion is not affected by wptexturize or title-save KSES.
@shail-mehta

Copy link
Copy Markdown
Member Author

I think it would be best to keep this focused on the display of the privacy policy link as that is affecting the front end display.

That will allow for the display of titles within the admin to be handled as a separate issue and discussed with a focus on what is best within the admin.

Thanks @peterwilsoncc. Updated to limit this PR to the privacy policy link only.

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.

9 participants