0

I have a custom post type called "stock" and each post is going to have a stock_id, stock_name, stock_description and stock_available fields,...I would like to have defaults for these fields so the user doesn't have to load them on each new item. Is there a way?

2 Answers 2

0

A simple way to do that would be to use Verve Meta Boxes plugin which lets you define a metabox with a simple UI (no code is needed) and you have an options to set the default value for each field.

4
  • and is there a way to set these meta boxes to show up only on custom post types?
    – user5057
    Commented Jul 17, 2011 at 20:25
  • If i recall there is an option to set the post type
    – Bainternet
    Commented Jul 17, 2011 at 22:08
  • I've been having lots of troubles getting these values the way explains so( eg get_post_custom_values('stock_id');) any clues on how to debug/figure this out?
    – user5057
    Commented Jul 18, 2011 at 0:17
  • Simply get them using get_post_Meta codex.wordpress.org/Function_Reference/get_post_meta
    – Bainternet
    Commented Jul 18, 2011 at 4:53
0

Two angles of approach: 1) More user friendly: would be to make a meta box for these custom fields. Your meta box for these custom fields would contain the <input /> elements - you can then put a default value in these, which when the post is saved will be stored.

See add_meta_box for more information.

2) At minimum, you can hook onto the 'save_post' action, and check if there's a value, and if not store that as the meta value:

add_action( 'save_post', 'myplugin_save_postdata' );

function myplugin_save_postdata($post_id)
{
  // verify if this is an auto save routine. 
  // If it is our form has not been submitted, so we dont want to do anything
  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
      return;

  // Check permissions
  if ( 'page' == $_POST['post_type'] ) 
  {
    if ( !current_user_can( 'edit_page', $post_id ) )
        return;
  }
  else
  {
     if ( !current_user_can( 'edit_post', $post_id ) )
        return;
  }

  // OK, we're authenticated: we need to find and save the data

  $mydata = $_POST['myplugin_new_field'];

  if (!$mydata)
  {
    // add default value
    $mydata = 'a default value;
  }

  // save the data
  update_post_meta($post_id, 'my_meta_field', $mydata);

  return $mydata;
}
1
  • may I ask you how you'd have a <select> with "yes" or "no" answers there? I'm trying to do something like this but it doesn't work: function stock_available() { global $post; echo '<input type="hidden" name="stockmeta_noncename" id="stockmeta_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />'; $location = get_post_meta($post->ID, '_available', true); // Echo out the field echo '<option><select name="_available" value="yes">SI</select><select name="_nonavailable" value="no">NO</select></option>'; }
    – user5057
    Commented Jul 17, 2011 at 19:19

Your Answer

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

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