Using WordPress ‘default_excerpt’ PHP filter

The default_excerpt WordPress PHP filter allows you to modify the default post excerpt initially used in the “Write Post” form.

Usage

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

function my_custom_default_excerpt($post_excerpt, $post) {
    // your custom code here
    return $post_excerpt;
}

Parameters

  • $post_excerpt (string) – Default post excerpt.
  • $post (WP_Post) – Post object.

More information

See WordPress Developer Resources: default_excerpt

Examples

Change default excerpt text

This example changes the default post excerpt text to “Your custom excerpt goes here.”

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

function change_default_excerpt($post_excerpt, $post) {
    $post_excerpt = 'Your custom excerpt goes here.';
    return $post_excerpt;
}

Add a prefix to the default excerpt

This example adds a prefix to the default post excerpt.

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

function add_prefix_default_excerpt($post_excerpt, $post) {
    $post_excerpt = 'Prefix: ' . $post_excerpt;
    return $post_excerpt;
}

Add a suffix to the default excerpt

This example adds a suffix to the default post excerpt.

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

function add_suffix_default_excerpt($post_excerpt, $post) {
    $post_excerpt = $post_excerpt . ' - Suffix';
    return $post_excerpt;
}

Use post title as the default excerpt

This example sets the post title as the default excerpt.

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

function use_title_as_default_excerpt($post_excerpt, $post) {
    $post_excerpt = $post->post_title;
    return $post_excerpt;
}

Default excerpt based on post category

This example sets the default excerpt based on the post’s category.

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

function set_excerpt_based_on_category($post_excerpt, $post) {
    $category = get_the_category($post->ID);
    if (!empty($category) && $category[0]->cat_name == 'News') {
        $post_excerpt = 'This is a news article.';
    } else {
        $post_excerpt = 'This is a regular post.';
    }
    return $post_excerpt;
}