5

I'd like to have a number of custom fields automatically added and pre-filled (with a static default value) when a user opens the 'Add New Post' page in the admin panel to create a new post. The point is to have the custom fields already set with a default value so the person adding the article can see them and modify the values if needed.

I've looked through the hooks but I can't find any suitable one. I'm also not sure how to go about pre-populating the custom fields section in the new post form. Any ideas?

2
  • The way to add custom field is explained here. In step 2 change the input field to <input type="number" name="wpse_value" value="default value here">, and you are done! Sep 3, 2015 at 10:40
  • From what I understand, add_meta_box() creates completely new fields in the new post page, whereas I'd like to use the built-in Custom Fields functionality.
    – Martin
    Sep 3, 2015 at 12:16

1 Answer 1

6

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' );
} );
4
  • This works, thank you! It does feel a bit hacky with the date and auto-draft check (I assume these are to ensure that we're adding the custom field if and only if it's just loaded a brand new post page), so I hope it's future proof.
    – Martin
    Sep 3, 2015 at 12:19
  • 1
    I don't think you need any other check except for auto_draft. I have done something similar using the status transition hook from auto_draft Sep 3, 2015 at 12:35
  • 1
    yes, we can most likely remove some of these checks and I just added them here to be sure ;-) There exists handy functions like wp_is_post_revision() and wp_is_post_autosave() but I don't think we can mix them to our needs here.
    – birgire
    Sep 3, 2015 at 12:51
  • 1
    Ok so I tested the updated version, seems to work fine, no duplicates or other issues when saving or updating the post.
    – Martin
    Sep 3, 2015 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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