WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do auto populate the excerpt field with the first 15 words of the post content? I found this snippet:

function wps_excerpt_content( $content ) {
    $content = "YOUR EXCERPT HERE";
    return $content;
add_filter( 'default_excerpt', 'wps_excerpt_content' );

But I don't know how to modify it with what I need. Any help?

share|improve this question
up vote 3 down vote accepted

How about using the excerpt_length filter?

function my_excerpt_lenght( $length ) {
    return 15;
add_filter( 'excerpt_length', 'my_excerpt_lenght', 999 );
share|improve this answer

The OP code snippet is for setting the default Excerpt when creating a post: http://example.com/wp-admin/post-new.php. So, it's not useful for the desired action.
Also, check the final note for a more detailed use of that filter.

At first, I thought of the save_post action to do it, but found a nice tip in this answer from Eugene Manuilov.

The following code auto-populates the post_excerpt whenever a post is created or saved, if it had no previous excerpt.
Note that the excerpt creation can be greatly improved, as it doesn't differentiates Urls nor short words.

// Define the custom excerpt length
$wpse_40574_custom_excerpt_length = 15;

add_filter( 'wp_insert_post_data', 'wpse_40574_populate_excerpt', 99, 2 );

 * Checks if the the post has excerpt or not
 * Code reference: http://wordpress.stackexchange.com/a/52897/12615
function wpse_40574_populate_excerpt( $data, $postarr ) 
    global $wpse_40574_custom_excerpt_length;

    // check if it's a valid call
    if ( !in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) && 'post' == $data['post_type'] ) 
        // if the except is empty, call the excerpt creation function
        if ( strlen($data['post_excerpt']) == 0 ) 
            $data['post_excerpt'] = wpse_40574_create_excerpt( $data['post_content'], $wpse_40574_custom_excerpt_length );

    return $data;

 * Returns the original content string if its word count is lesser than $length, 
 * or a trimed version with the desired length.
 * Reference: see this StackOverflow Q&A - http://stackoverflow.com/q/11521456/1287812
function wpse_40574_create_excerpt( $content, $length = 20 )
    $the_string = preg_replace( '#\s+#', ' ', $content );
    $words = explode( ' ', $the_string );

     * The following is a more efficient way to split the $content into an array of words
     * but has the caveat of spliting Url's into words ( removes the /, :, ., charachters )
     * so, not very useful in this context, could be improved though.
     * Note that $words[0] has to be used as the array to be dealt with (count, array_slice)
    //preg_match_all( '/\b[\w\d-]+\b/', $content, $words );

    if( count($words) <= $length ) 
        $result = $content;
        $result = implode( ' ', array_slice( $words, 0, $length ) );

    return $result;


Example of default_excerpt filter usage:

add_filter( 'default_excerpt', 'wpse_40574_excerpt_content', 10, 2 );

function wpse_40574_excerpt_content(  $post_excerpt, $post  ) 
    // Do nothing if not the correct post type
    // http://codex.wordpress.org/Post_Types
    if( 'post' != $post->post_type )


    return $post_excerpt;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.