Automatically generates AI-ready smart excerpts from content when the excerpt field is empty.
add_action( 'save_post', 'rds_ai_generate_excerpt', 20, 2 );
function rds_ai_generate_excerpt( $post_id, $post ) {
if ( wp_is_post_revision( $post_id ) ) {
return;
}
if ( ! empty( $post->post_excerpt ) ) {
return;
}
$content = wp_strip_all_tags( $post->post_content );
$excerpt = wp_trim_words( $content, 35, '...' );
remove_action( 'save_post', 'rds_ai_generate_excerpt', 20 );
wp_update_post(
array(
'ID' => $post_id,
'post_excerpt' => $excerpt,
)
);
add_action( 'save_post', 'rds_ai_generate_excerpt', 20, 2 );
}