0

I'm using WordPress Version 4.7.2

I'm trying to set up the new post page (wp-admin/post-new.php) so it automatically displays two custom fields. I'd like the custom field "name" pre-populated and the custom field "value" left blank.

Current New Post Page

enter image description here

Desired New Post Page

enter image description here

Any help is greatly appreciated. Thanks!

0

The below code will add default custom fields to your post ( Insert the code in your themes function.php ) .

add_action('wp_insert_post', 'set_default_custom_fields');
function set_default_custom_fields($post_id){
if ( $_GET['post_type'] == 'post' ) {

add_post_meta($post_id, 'Field Name', '', true);
add_post_meta($post_id, 'Another Field Name', '', true);
}
return true;
}

By default this will add an empty custom field to your post. If you want to enter a value by default too, use the code below.

add_action('wp_insert_post', 'set_default_custom_fields');
function set_default_custom_fields($post_id){
if ( $_GET['post_type'] == 'post' ) {
add_post_meta($post_id, 'Field Name', 'Field Value', true);
}
return true;
}
  • Thanks for replying! I did update my function.php file with the "2nd option" from your answer, but it did not work. I tried the 1st option also, and it didn't work either. I'm attempting to get the custom fields immediately when I click "new post". I don't what to have to select them from the "dropdown" list. I'm using the 'standard' template in a "new post". Ideas? Thanks again. – Mr. B Feb 1 '17 at 5:25
0

Usually, the best way to add custom-field is to use add_meta_boxes action and add_meta_box() function.

When you use this action, custom-field can be place where ever you want. If you want to pre-populate input value, it will easier, as you dedicated function will be thrown exactly like you want (values could be set).

You will find some more details add_meta_boxes

Hope it helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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