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 created two custom taxonomies i.e states and provinces but am unable to save the data in custom build meta boxes in wp-admin:

<?php function custom_meta_box() {

    remove_meta_box( 'tagsdiv-states', 'post', 'side' );
    remove_meta_box( 'tagsdiv-provinces', 'post', 'side' );

    add_meta_box( 'tagsdiv-states', 'US States', 'states_meta_box', 'post', 'side' );
    add_meta_box( 'tagsdiv-provinces', 'Canada Provinces', 'provinces_meta_box', 'post', 'side' );

}
add_action('add_meta_boxes', 'custom_meta_box');

/* Prints the taxonomy box content */
function states_meta_box($post) {

    $tax_name = 'states';
    $taxonomy = get_taxonomy($tax_name);
?>
<div class="tagsdiv" id="<?php echo $tax_name; ?>">
    <div class="jaxtag">
    <?php 
    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), 'states_noncename' );
    $states_IDs = wp_get_object_terms( $post_id, 'states', array('fields' => 'ids') );
    wp_dropdown_categories('taxonomy=states&hide_empty=0&orderby=name&name=states&show_option_none=Select US State&selected='.$states_IDs[0]); ?>
    <p class="howto">Select US State</p>
    </div>
</div>
<?php
}

/* When the post is saved, saves our custom taxonomy */
function states_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 ) || wp_is_post_revision( $post_id ) ) 
      return;

  // verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times

  if ( !wp_verify_nonce( $_POST['states_noncename'], plugin_basename( __FILE__ ) ) )
      return;


  // Check permissions
  if ( 'post' == $_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

  $states_ID = $_POST['states'];

  $states = ( $states_ID > 0 ) ? get_term( $states_ID, 'states' )->slug : NULL;

  wp_set_object_terms(  $post_id , $states, 'states' );

}
/* Do something with the data entered */
add_action( 'save_post', 'states_save_postdata' );


/* Prints the taxonomy box content */
function provinces_meta_box($post) {

    $tax_name = 'provinces';
    $taxonomy = get_taxonomy($tax_name);
?>
<div class="tagsdiv" id="<?php echo $tax_name; ?>">
    <div class="jaxtag">
    <?php 
    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), 'provinces_noncename' );
    $provinces_IDs = wp_get_object_terms( $post_id, 'provinces', array('fields' => 'ids') );
    wp_dropdown_categories('taxonomy=provinces&hide_empty=0&orderby=name&name=provinces&show_option_none=Select Province&selected='.$provinces_IDs[0]); ?>
    <p class="howto">Select Canadian Province</p>
    </div>
</div>
<?php
}

/* When the post is saved, saves our custom taxonomy */
function provinces_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 ) || wp_is_post_revision( $post_id ) ) 
      return;

  // verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times

  if ( !wp_verify_nonce( $_POST['provinces_noncename'], plugin_basename( __FILE__ ) ) )
      return;


  // Check permissions
  if ( 'post' == $_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

  $provinces_ID = $_POST['provinces'];

  $provinces = ( $provinces_ID > 0 ) ? get_term( $provinces_ID, 'provinces' )->slug : NULL;

   wp_set_post_terms(  $post_id , $provinces, 'provinces' );

}
/* Do something with the data entered */
add_action( 'save_post', 'provinces_save_postdata' );?>
share|improve this question
    
If you remove all the authentication do they save? –  m0r7if3r Feb 2 '12 at 11:05
    
@Anthony, could you edit your question / accept the edits - the formatting of the code is incorrect. –  Stephen Harris Feb 2 '12 at 11:05
add comment

1 Answer

up vote 1 down vote accepted

Rather than 'returning' on a failure of authentication check, die with an appropriate message:

For example:

  if ( !wp_verify_nonce( $_POST['states_noncename'], plugin_basename( __FILE__ ) ) )
      wp_die("Nonce-check failure");

This will indicate on what check (if any) that you are failing. In most cases, I would advise doing this rather than returning as it gives the user feedback. Obviously, you should not do this for the 'auto-save' check - but otherwise, if its clear there is desire to do something, but they are not allowed to, an error message would be preferable to a silent fail.

I think, though that the error lies in the permissions check:

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

You check that the post is a post (rather than CPT/page) but then you check if the current user can edit this page. This is likely to return false, because the $post_id refers to a post not a page.

Try swapping the !current_user_cans around. As I mentioned above, the 'die' messages should help you debug this.

Finally, and though not related to your problem, you should make sure your functions do not have generatic names: custom_meta_box - you should always prefix them with something unique. If this isn't something for general release, you might be ok, but it's just good practise.

share|improve this answer
add comment

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.