admin管理员组文章数量:1122846
I have done an excessive amount of googling and haven't found exactly what I need yet.
The client uses the editor (WYSIWYG) to create posts. She adds photos, links to external photos, videos, embeds external videos, and all other formatting. But she does not use the tag nor the excerpt field.
When I use wp_trim_words()
I get mixed results.
- Posts with local videos leak markup onto the page
- A couple posts are only images so there isn't any content at all
- Posts with a lot of formatting look really wonky
Anyone have any other ideas on how to trim content or better ways to use wp_trim_words() ?
EDIT: To make myself clear basically what I want is a trimmed version of the full post that still contains all the formatting, images, videos, links, embeds, whatever else she throws into the editor. I do not want markup leaking through, obviously.
I have done an excessive amount of googling and haven't found exactly what I need yet.
The client uses the editor (WYSIWYG) to create posts. She adds photos, links to external photos, videos, embeds external videos, and all other formatting. But she does not use the tag nor the excerpt field.
When I use wp_trim_words()
I get mixed results.
- Posts with local videos leak markup onto the page
- A couple posts are only images so there isn't any content at all
- Posts with a lot of formatting look really wonky
Anyone have any other ideas on how to trim content or better ways to use wp_trim_words() ?
EDIT: To make myself clear basically what I want is a trimmed version of the full post that still contains all the formatting, images, videos, links, embeds, whatever else she throws into the editor. I do not want markup leaking through, obviously.
Share Improve this question edited Sep 30, 2016 at 19:36 winchendonsprings asked Sep 30, 2016 at 19:13 winchendonspringswinchendonsprings 1233 silver badges9 bronze badges2 Answers
Reset to default 0wp_strip_all_tags() will strip out all the tags.
However with posts which just have images and no content. There is no solution to that, the excerpt or wherever you are trying to use it will show blank since it cannot "magically" create content for the post when there is no content at all.
For more info > https://codex.wordpress.org/Function_Reference/wp_strip_all_tags
This is going to be pretty complex to do. You could write your own function to try and trim the content and then close any unclosed tags at the end. Example:
/**
* Helper function to display custom excerpts with HTML.
*/
function custom_excerpt_with_html( $word_count = 40 ) {
$text = get_the_content( null, false ); // Note: you may need to pass this through do_blocks()
if ( ! $text ) {
return;
}
if ( strlen( strip_tags( $text ) ) <= $word_count ) {
return $text; // text shorter than length.
}
$last_space = $word_count + 1; // add +1 because arrays start at 0
$words = explode( ' ', $text, $last_space );
array_pop( $words ); // Shorten array by 1 since the final item will be the sum of all the words after the last_space.
$excerpt = implode( ' ', $words ); // create excerpt string again from words
echo close_open_html_tags( $excerpt );
}
/**
* Helper function to close any open HTML tags in a string.
*/
function close_open_html_tags( $html ) {
preg_match_all('#<([a-zA-Z0-9]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result );
if ( empty( $result[1] ) ) {
return $html; // no tags.
}
$opened_tags = $result[1];
preg_match_all('#</([a-zA-Z0-9]+)>#iU', $html, $result );
$closed_tags = $result[1];
$opend_tags_length = count( $opened_tags );
if ( count( $closed_tags ) === $opend_tags_length ) {
return $html;
}
$opened_tags = array_reverse($opened_tags);
for ( $i=0; $i < $opend_tags_length; $i++ ) {
if ( ! in_array( $opened_tags[ $i ], $closed_tags ) ) {
$html .= '</'.$opened_tags[ $i ].'>';
} else {
unset( $closed_tags[ array_search( $opened_tags[ $i ], $closed_tags ) ] );
}
}
return $html;
}
I pulled this code from some custom work I did a few months ago. But the client only had a few html tags they wanted to keep such as bold text and it worked good there, not sure if it will work very well with images and links - but it could perhaps be modified to better suit your needs.
If the client wants to display HTML in the excerpts I would highly recommend you teach them to use the "more" tag (or block if they are using Gutenberg).
Any sort of custom auto trimming function you create is not going to be very good. The only way to properly split the HTML is by inserting some sort of marker into the post (like the more tag) that you can split the content by.
If the user at least wanted to let's say split the content after the first image or paragraph, then perhaps that would be more feasible.
You may want to check out this answer: https://stackoverflow.com/a/24160854/7607379
本文标签: formattingWhat39s the best way to trim content from WYSIWYG editor
版权声明:本文标题:formatting - What's the best way to trim content from WYSIWYG editor? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736290505a1928527.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论