Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: wp_trim_words function to allow specific html tags #6798

Open
wants to merge 12 commits into
base: trunk
Choose a base branch
from

Conversation

up1512001
Copy link

@up1512001 up1512001 commented Jun 13, 2024

Trac ticket:

https://core.trac.wordpress.org/ticket/59270


As wp_strip_all_tags function strips all HTML tags this won't allow doing any formatting to excerpt. I have used wp_kses() here with specific allowed HTML tags.

Allowed tag list

$allowed_tags = array(
		'a'      => array(
			'class' => array(),
			'style' => array(),
			'href'  => array(),
			'id'    => array(),
		),
		'strong' => array(),
		'b'      => array(),
		'em'     => array(),
		'img'    => array(
			'class'  => array(),
			'style'  => array(),
			'src'    => array(),
			'alt'    => array(),
			'width'  => array(),
			'height' => array(),
		),
		'mark'   => array(
			'class' => array(),
			'style' => array(),
		),
		'code'   => array(
			'class' => array(),
			'style' => array(),
		),
		'kbd'    => array(
			'class' => array(),
			'style' => array(),
		),
		'bdo'    => array(
			'class' => array(),
			'style' => array(),
			'dir'   => array(),
			'lang'  => array(),

		),
		'span'   => array(
			'class' => array(),
			'style' => array(),
		),
		'sup'    => array(
			'class' => array(),
			'style' => array(),
		),
		'sub'    => array(
			'class' => array(),
			'style' => array(),
		),
		's'      => array(
			'class' => array(),
			'style' => array(),
		),
	);

Example output

Screen.Recording.2024-06-13.at.12.28.18.mov
Copy link

github-actions bot commented Jun 13, 2024

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 up1512001, mukesh27, audrasjb, swissspidy.

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

@up1512001
Copy link
Author

Hi @swissspidy can you please review this PR?

@up1512001 up1512001 changed the title update: wp_trim_works function to allow specific html tags Jun 13, 2024
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.

@audrasjb
Copy link
Contributor

audrasjb commented Jun 13, 2024

Hi, thanks for the PR!

I think we should consider passing $allowed_tags as an argument of wp_trim_words() so we can 1) make it optional (and maybe disabled by default?); 2) allow extenders to customize it their way.

What do you think?

@up1512001
Copy link
Author

@audrasjb we can definitely do that.

Copy link
Contributor

@audrasjb audrasjb left a comment

Choose a reason for hiding this comment

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

Thanks for the update. We also need a since mention in the docblock :)

src/wp-includes/formatting.php Show resolved Hide resolved
@up1512001 up1512001 requested a review from audrasjb June 13, 2024 10:12
@up1512001
Copy link
Author

@mukeshpanchal27 @audrasjb any update for this PR?

@up1512001
Copy link
Author

@swissspidy can you please review this PR?

@swissspidy
Copy link
Member

Why me specifically? 😅 I am on vacation

@up1512001
Copy link
Author

@swissspidy Sorry for disturbing you.

$text = wp_strip_all_tags( $text );
$num_words = (int) $num_words;
if ( $allow_tags ) {
$allowed_tags = array(
Copy link
Author

Choose a reason for hiding this comment

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

added all possible tags & styles to support excerpt styling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants