Using WordPress ‘edit_post_{$field}’ PHP filter

The edit_post_{$field} WordPress PHP filter allows you to modify the value of a specific post field before it’s saved in the database.

Usage

add_filter( 'edit_post_title', 'customize_post_title', 10, 2 );
function customize_post_title( $title, $post_id ) {
    // your custom code here
    return $title;
}

Parameters

  • $field (string) – The field you want to modify (e.g., ‘title’, ‘content’, ‘excerpt’, etc.).
  • $value (mixed) – The current value of the field.
  • $post_id (int) – The ID of the post being edited.

More information

See WordPress Developer Resources: edit_post_{$field}

Examples

Change post title to uppercase

This example converts the post title to uppercase before saving it.

add_filter( 'edit_post_title', 'uppercase_post_title', 10, 2 );
function uppercase_post_title( $title, $post_id ) {
    $title = strtoupper( $title );
    return $title;
}

Add a prefix to post content

This example adds a prefix to the post content before saving it.

add_filter( 'edit_post_content', 'add_content_prefix', 10, 2 );
function add_content_prefix( $content, $post_id ) {
    $prefix = 'This is a prefix: ';
    $content = $prefix . $content;
    return $content;
}

Truncate post excerpt

This example truncates the post excerpt to a maximum of 100 characters.

add_filter( 'edit_post_excerpt', 'truncate_post_excerpt', 10, 2 );
function truncate_post_excerpt( $excerpt, $post_id ) {
    $excerpt = substr( $excerpt, 0, 100 );
    return $excerpt;
}

Add custom metadata to post

This example adds custom metadata to a post before saving it.

add_filter( 'edit_post_meta', 'add_custom_meta', 10, 2 );
function add_custom_meta( $meta, $post_id ) {
    $meta['_custom_key'] = 'Custom value';
    return $meta;
}

Modify post slug

This example modifies the post slug by adding a timestamp to it.

add_filter( 'edit_post_name', 'add_timestamp_to_slug', 10, 2 );
function add_timestamp_to_slug( $slug, $post_id ) {
    $timestamp = time();
    $slug = $slug . '-' . $timestamp;
    return $slug;
}