Pre-filling custom fields on New Post

Here’s one way to have a custom field, already added and visible, for new posts:

/**
 * Preset a custom field for new posts
 *
 * @link http://wordpress.stackexchange.com/a/200554/26350
 */
add_action( 'save_post_post', function( $post_ID, $post, $update )
{
    if( 
            is_a( $post, '\WP_Post' )
        &&  'auto-draft' === $post->post_status
        &&  post_type_supports( $post->post_type, 'custom-fields' ) 
        && '0000-00-00 00:00:00' === $post->post_date_gmt 
        && $post_ID > 0
        && ! $update
    )
        add_post_meta( $post_ID, 'wpse_custom_field', '123' );

}, 10, 3 );

Here we use the save_post_{post-type} hook.

We will then see this on the Add New Post screen:

preset custom field

As mentioned in the comments by @Alpha_Hydrae and @MarkKaplun,
we should be able to simplify this to:

/**
 * Preset a custom field for new posts
 *
 * @link http://wordpress.stackexchange.com/a/200554/26350
 */
add_action( 'save_post_post', function( $post_ID )
{
    if( 'auto-draft' === get_post_status( $post_ID )
        &&  post_type_supports( get_post_type( $post_ID ), 'custom-fields' ) 
    )
        add_post_meta( $post_ID, 'wpse_custom_field', '123' );
} );

Leave a Comment