Filter
uncanny-automator
automator_get_the_excerpt_length
Filters the excerpt length before it's displayed, allowing customization of the default character count.
add_filter( 'automator_get_the_excerpt_length', $callback, 10, 1 );
Description
Filters the character length for auto-generated excerpts. Developers can modify this to control how many characters of post content are included when a custom excerpt is not present. The default length is 55 characters.
Usage
add_filter( 'automator_get_the_excerpt_length', 'your_function_name', 10, 1 );
Parameters
-
$length(mixed) - This parameter controls the maximum number of words to include in the generated excerpt.
Return Value
The filtered value.
Examples
<?php
/**
* Filter the excerpt length to a custom value.
*
* This function modifies the default excerpt length provided by the
* automator_get_the_excerpt_length filter.
*
* @param int $length The default excerpt length.
* @return int The modified excerpt length.
*/
function my_custom_excerpt_length( $length ) {
// Set a new, shorter excerpt length for all posts.
return 20;
}
add_filter( 'automator_get_the_excerpt_length', 'my_custom_excerpt_length', 10, 1 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/core/lib/utilities/class-automator-utilities.php:746
public function automator_get_the_excerpt( $post_id, $length = 25 ) {
$post = get_post( $post_id );
if ( ! $post instanceof WP_Post ) {
return '';
}
$post_content = $post->post_content;
$post_excerpt = $post->post_excerpt;
if ( ! empty( $post_excerpt ) ) {
// If custom excerpt is defined, return the same
return apply_filters( 'automator_get_the_excerpt', $post_excerpt, $post_content, $post_id, $length );
}
$length = apply_filters( 'automator_get_the_excerpt_length', $length );
$excerpt = sanitize_text_field( strip_shortcodes( wp_strip_all_tags( $post_content ) ) );
$words = explode( apply_filters( 'automator_get_the_excerpt_separator', ' ' ), $excerpt );
$len = min( $length, count( $words ) );
$excerpt = array_slice( $words, 0, $len );
$excerpt = join( ' ', $excerpt );
if ( ! empty( $excerpt ) ) {
$excerpt = $excerpt . apply_filters( 'automator_get_the_excerpt_continuity', '...', $post_id );
}
return apply_filters( 'automator_get_the_excerpt', $excerpt, $post_content, $post_id, $length );
}