Using WordPress ‘edit_{$field}’ PHP filter

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

Usage

add_filter( 'edit_post_field_name', 'modify_post_field', 10, 2 );

function modify_post_field( $value, $post_id ) {
  // Your custom code here

  return $value;
}

Parameters

  • $value (mixed): Value of the post field.
  • $post_id (int): Post ID.

More information

See WordPress Developer Resources: edit_{$field}

Examples

Change post title for editing

Modify the post title before it’s displayed in the editor:

add_filter( 'edit_post_title', 'change_post_title', 10, 2 );

function change_post_title( $title, $post_id ) {
  $new_title = 'Edited: ' . $title;
  return $new_title;
}

Append author name to content

Add the author name at the end of the post content:

add_filter( 'edit_post_content', 'append_author_name', 10, 2 );

function append_author_name( $content, $post_id ) {
  $author_name = get_the_author_meta( 'display_name', get_post_field( 'post_author', $post_id ) );
  return $content . ' - ' . $author_name;
}

Modify post excerpt length

Trim the post excerpt length to 100 characters:

add_filter( 'edit_post_excerpt', 'modify_excerpt_length', 10, 2 );

function modify_excerpt_length( $excerpt, $post_id ) {
  return substr( $excerpt, 0, 100 );
}

Capitalize post title

Capitalize the first letter of each word in the post title:

add_filter( 'edit_post_title', 'capitalize_post_title', 10, 2 );

function capitalize_post_title( $title, $post_id ) {
  return ucwords( $title );
}

Replace specific words in content

Replace the word “WordPress” with “WP” in the post content:

add_filter( 'edit_post_content', 'replace_specific_words', 10, 2 );

function replace_specific_words( $content, $post_id ) {
  return str_replace( 'WordPress', 'WP', $content );
}