Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 0 down vote accepted

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.

share|improve this answer
    
and is there a way to set these meta boxes to show up only on custom post types? –  user5057 Jul 17 '11 at 20:25
    
If i recall there is an option to set the post type –  Bainternet Jul 17 '11 at 22:08
    
thanks a lot I figured after installing the plugin –  user5057 Jul 17 '11 at 22:10
    
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 Jul 18 '11 at 0:17
    
Simply get them using get_post_Meta codex.wordpress.org/Function_Reference/get_post_meta –  Bainternet Jul 18 '11 at 4:53

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;
}
share|improve this answer
    
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 Jul 17 '11 at 19:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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