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 added a Custom Post Type and fields. But when I enter values and save, they do not show up in the Wordpress edit screen.

 <?php
 add_action( 'init', 'create_post_type' );
 function create_post_type() {

 $supports = array('title', 'editor', 'thumbnail');

 register_post_type( 'acme_property',
    array(
            'labels' => array(
            'name' => __( 'Properties' ),
            'singular_name' => __( 'Property' ),
            'search_items' => __( 'Search Properties' ),
            'not_found' => __( 'No Properties found' )
        ),
    'public' => true,
    'has_archive' => true,
    'supports' => $supports
    )
   );
 }

 add_action('add_meta_boxes', 'add_property');

function add_property(){
    add_meta_box("property-meta", "Property Info", "meta_options", "acme_property", "side", "low");
}

function meta_options(){
    global $post;
    $values = get_post_custom($post->ID);
    $Prop_ID = isset( $values['property_id'] ) ? esc_attr( $values['property_id'][0] ) : '';
    $address = isset( $values['address'] ) ? esc_attr( $values['address'][0] ) : ''; 
?>
    <p>
    <label>Property #: </label><br/><input name="property_id" value="<?php echo $Prop_ID; ?>" />
    </p>
    <p>
    <label>Address:</label><br/><textarea name="address" cols="22" rows="3" value="<?php echo $address; ?>"></textarea>
    </p>
 <?php
}

 add_action( 'save_post', 'property_save' );

 function property_save( $post_id )
 {
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;

// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;

// now we can actually save the data
$allowed = array(
        'a' => array( // on allow a tags
        'href' => array() // and those anchors can only have href attribute
    )
);

// Make sure your data is set before trying to save it
if( isset( $_POST['property_id'] ) )
    update_post_meta( $post_id, 'property_id', wp_kses( $_POST['property_id'], $allowed ) );

if( isset( $_POST['address'] ) )
    update_post_meta( $post_id, 'address', esc_attr( $_POST['address'] ) );

 }
share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

It seems the problem might be that you haven't added a nonce - so when the post is being saved, you are failing the nonce check.

Try adding

 <?php wp_nonce_field('my_meta_box_nonce','meta_box_nonce'); ?>

to your metabox. Remember the nonce action (in this case my_meta_box_nonce) should be

  • unique to the action (saving a property),
  • unique to an object (e.g. the property's ID),
  • and unique to your (your plug-in name).

Also - for these types of debugging questions use var_dump and wp_die to output variables and kill the processing (for instance, in your save_property function to see if that function is being called / where the function is aborted).

Not related to you original question, but you should use get_post_meta rather than get_post_custom.

Finally, I don't think you want to use wp_kses for the property ID. This is an expensive function, intended to strip out some HTML tags but allow others. If the ID is just a number, use intval or sanitize_key for alpha-numerics.

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.