add_filter('wp_trim_excerpt', function($text){
$max_length = 140;
if(mb_strlen($text, 'UTF-8') > $max_length){
$split_pos = mb_strpos(wordwrap($text, $max_length), "\n", 0, 'UTF-8');
$text = mb_substr($text, 0, $split_pos, 'UTF-8');
}
return $text;
});
This should take into account your max length and split the text at the nearest word boundary.
Apply the filter, and call the_excerpt();
in your templates
Apparently there’s a wp_trim_words
function from WP 3.3 that you can also use, but from the source looks very inefficient. Appart from using 3 regexes, it splits the text into an array of words, and this can get very slow and memory exhaustive for large chunks of text…
Related Posts:
- Limit the Excerpt field in WP-Admin in words
- Allow HTML in excerpt
- Decrease file size upload in Media
- How to return the_excerpt (without echo)?
- Remove at the beginning of the_excerpt() with str_replace
- Automatic Excerpt Not Working
- Trim first 2 words of the exceprt
- Extremely slow pageload for long post when using the_content?
- How to change / delete product short description in Woocommerce
- Why does running get_the_excerpt() when generating JSON file take 28 seconds vs. 599 milliseconds without it?
- Excerpt unwanted text “”
- Word Count Function Preventing Permalink Editing
- Custom excerpt function re-factoring
- How to Display a Single Post Excerpt
- How to remove the excerpt in the Dzonia Lite theme
- How do I Add HTML to the_excerpt() & the_content() Output?
- Mass update excerpt
- How to put “Read more” link in Custom Excerpt inside p tag?
- Error when adding excerpt to the content through functions.php
- HTML tags not showing in excerpt
- Limit 1 global comment per minute
- Query Pages and post excerpts dynamically
- My custom get_the_excerpt() can’t get excerpt by ID
- Adding Meta Tags to a Post using its Tags, Excerpt and content
- Using the_excerpt() on a page
- Defined WordPress Memory Limit to Unlimited
- How to show an entire post content and not also the excerpt?
- How to solve Woocommerce Memory Limit
- stripping tags from excerpt in WordPress is not working
- Almost Done… Post Format Code
- Automatic excerpt is not shown with the_excerpt() command
- WordPress hit memory limit but not from the server
- Remove   from the_excerpt
- Why is my max_upload_filesize being limited to 2M? [duplicate]
- Shortcodes showing in excerpt despite using strip_shortcodes
- How to remove the space before the ellipsis in excerpt?
- Need help removing […] after excerpt
- Post class only applying to first excerpt; why?
- Assign Custom classes to every Excerpt
- Problem in replacing the_content with the_excerpt()
- Find and print first entry from WordPress post in custom excerpt?
- How can i limit the number of posts to the most recent 6 in my query?
- Cannot figure out how to add a read more link to a manual excerpt. Please help
- Trying to modify content.php
- How to trim content AND retain HTML?
- How to apply filter
- Add point on excerpt post
- Limit Search Queries per IP per Day
- Archive page if else not working for post_excerpt and post_content
- Image limit from 1 to 5
- Creating a function that sanitizes the custom metabox excerpt
- How can I add more code to this?
- Merging two excerpt functions to work with conditions
- I can’t seem to get excerpts showing inside get_pages and don’t know why?
- Excerpt all post content Content Same Size without word cutting off
- Remove post image caption from post_excerpt();
- generate excerpt from the part of big text dump that contains keyword
- Strip and print only the numbers found in current’s post excerpt (even if they are without space)
- I have issue with decoding json ex. ‘{“a”:”b”}’
- Automatically load WordPress page on server every XX hours
- How to use custom fields to replace top-level parent title with an image using wp_list_pages?
- Fix My Custom Function: Remove Automatic
- PHP Puzzle: Unique Styles with PHP loop
- Change default comments form title [duplicate]
- Programmatically change post templates? [duplicate]
- custom contact form no longer working (because of 3.2?)
- Catchable fatal error in appcloud free theme by Tokokoo
- Add Column to Term Database
- File Upload with Server in safe_mode
- I want Page titles and excerpts to show up on home.php in certain order
- WP plugin updates [duplicate]
- Hide a nav menu item based on get_user_meta results
- Custom Archive Page
- How to change a specific character in a theme option value – replace a space with a plus sign
- Crop images in both sides on wordpress [duplicate]
- 2 small questions: How to redirect to a created page & show that pages title in wp, bp
- Custom query_posts() parameter
- How to execute add_action() function from custom plugin to Crontrol plugin or do_action()?
- Incomplete term slugs output from a foreach loop with get_the_terms
- Posts sortable column not sorting properly for custom field numbers
- How to set if meta_value is lower < than other meta_value in a get_posts array?
- How to include support for all page types, calendar urls, archive, etc
- How can I add page template after post of a post type?
- Custom Shortcode with Dynamic Button
- Is it possible to display conditional HTML based on page name in wordpress?
- Global variables and re-use
- Cannot get custom javascript to execute on page
- How to declare a variable at the bottom of the page and use its value at the top?
- How do I modify the with wp_nav_menu()
- How to add custom checkout field in user details mail template
- The search engine of my website finds only posts and not pages, how can I solve this problem?
- MySQL update text field with ‘
- Pull GET parameter from URL in functions.php
- Shortcode working in page.php but not in category.php in wordpress
- Fatal error: Uncaught Error: Call to undefined function wp_nav_menu()
- How to NOT override inline css rules
- WP_Query based on another query on the page
- WordPress Memory limit not increasing
- Open all external links in new window – need help with the code
- What’s the best way to format ACF number fields for display on the front end?