Filter
uncanny-automator
automator_get_the_excerpt
Filters the post excerpt content and length before it is displayed, allowing modification of the generated excerpt.
add_filter( 'automator_get_the_excerpt', $callback, 10, 4 );
Description
Allows developers to modify the post excerpt. This filter fires when an excerpt is being generated or retrieved. Developers can override the default excerpt, adjust its length, or implement custom logic for excerpt creation. The hook provides access to the excerpt, content, post ID, and desired length.
Usage
add_filter( 'automator_get_the_excerpt', 'your_function_name', 10, 4 );
Parameters
-
$post_excerpt(mixed) - This parameter contains the post's pre-defined excerpt.
-
$post_content(mixed) - This parameter holds the post's pre-defined excerpt, which will be used if it's not empty.
-
$post_id(mixed) - This parameter contains the full content of the post, which can be used to generate an excerpt if no specific excerpt is provided.
-
$length(mixed) - This parameter represents the unique identifier for the post.
Return Value
The filtered value.
Examples
<?php
/**
* Example function to filter the excerpt generated by the automator plugin.
* This example adds a prefix to the generated excerpt if the post content
* contains a specific keyword.
*
* @param string $post_excerpt The original excerpt.
* @param string $post_content The full post content.
* @param int $post_id The ID of the post.
* @param int $length The desired length of the excerpt.
* @return string The filtered excerpt.
*/
add_filter( 'automator_get_the_excerpt', function( $post_excerpt, $post_content, $post_id, $length ) {
// Define a keyword to check for in the post content
$keyword_to_check = 'special offer';
// Check if the keyword exists in the post content (case-insensitive)
if ( stripos( $post_content, $keyword_to_check ) !== false ) {
// If the keyword is found, prepend a message to the existing excerpt
$prefix = 'Act fast! ';
$post_excerpt = $prefix . $post_excerpt;
}
// Return the potentially modified excerpt
return $post_excerpt;
}, 10, 4 );
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:744
src/core/lib/utilities/class-automator-utilities.php:756
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 );
}