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 cannot figure out why these functions aren't saving the data for price and location while saving startdate and starttime. When I do print_r($custom) - I can see that [events_price] => Array ( [0] => ) is blank..

 function event_detail_box_content( $post ) {
    $custom = get_post_custom($post->ID);
    $meta_sd = $custom["events_startdate"][0];
    $meta_pr = $custom["events_price"][0];
    $meta_lo = $custom["events_location"][0];

    $meta_st = $meta_sd;

    $time_format = get_option('time_format');

    if ($meta_sd == null) { $meta_sd = time(); $meta_st = 0;}

    $clean_sd = date("D, M d, Y", $meta_sd);
    $clean_st = date($time_format, $meta_st);

    echo '<input type="hidden" name="events-nonce" id="events-nonce" value="' . wp_create_nonce( 'events-nonce' ) . '" />';

    ?>
    <div class="tf-meta">
    <ul>
        <li><label>Event Date</label><input name="events_startdate" class="tfdate" value="<?php echo $clean_sd; ?>" /></li>
        <li><label>Event Time</label><input name="events_starttime" value="<?php echo $clean_st; ?>" /><em>Use 24h format (7pm = 19:00)</em></li>
        <li><label>Event Price</label><input name="events_price" value="<?php echo $meta_pr; ?>" /></li>
        <li><label>Event Location</label><input name="events_location" value="<?php echo $meta_lo; ?>" /></li>
    </ul>
    </div>
    <?php
    print_r($custom);
 }

   add_action ('save_post', 'save_events');

   function save_events($post_id){

    if ( !wp_verify_nonce( $_POST['events-nonce'], 'events-nonce' )) { return; }

    if ( !current_user_can( 'edit_post')) return;

    if(!isset($_POST["events_startdate"])): return; endif;

    $updatestartd = strtotime ( $_POST["events_startdate"] . $_POST["events_starttime"] );
    update_post_meta($post_id, "events_startdate", $updatestartd ); 

    update_post_meta($post_id, "events_price", $POST["events_price"] ); 
    update_post_meta($post_id, "events_location", $POST["events_location"] ); 
 }
share|improve this question
add comment

3 Answers

I can see a couple of errors you've made in your code above, so I've taken the liberty of fixing it up for you and let me know if it resolves your problem. You were a little vague in your question, but can see your code should be fine with the following fixes.

I've annotated with double forward slash comments where values have been changed, added or removed.

add_action( 'add_meta_boxes', 'event_detail_box' );

 function event_detail_box() {
     add_meta_box( 
    'event_detail_box',
    __( 'Event Detail', 'myplugin_textdomain' ),
    'event_detail_box_content',
    'event',
    'normal',
    'high'
    );
}

 function event_detail_box_content( $post ) {
    // No need to globalise a post that is an argument on this callback
    $meta_p = get_post_meta($post->ID, "events_price", true);
    $meta_l = get_post_meta($post->ID, "events_location", true);

    echo '<input type="hidden" name="events-nonce" id="events-nonce" value="' . wp_create_nonce( 'events-nonce' ) . '" />';

    ?>
    <div class="tf-meta">
    <ul>
        <li><label>Event Price</label><input name="events_price" value="<?php echo $meta_p; ?>" /></li>
        <li><label>Event Location</label><input name="events_location" size="50" value="<?php echo $meta_l; ?>" /></li>
    </ul>
    </div>
    <?php
 }

 add_action ('save_post', 'save_events');

 // The save_post hook provides the post id to the return function
 function save_events( $post_id ){

    if ( !wp_verify_nonce( $_POST['events-nonce'], 'events-nonce' )) {
        return $post_id;
    }

    if ( !current_user_can( 'edit_post', $post_id ))
        return $post_id; 

    update_post_meta($post_id, "events_price", $POST['events_price'] ); 
    update_post_meta($post_id, "events_location", $POST['events_location'] ); 
 }
share|improve this answer
    
Why return $post_id;? It should be only return;, no? –  brasofilo Feb 21 '13 at 0:41
    
This is the code the question askee had. There's no harm in return the post_id I don't think. –  DigitalSea Feb 21 '13 at 0:43
    
Actions don't return values: ottopress.com/2011/actions-and-filters-are-not-the-same-thing –  brasofilo Feb 21 '13 at 1:29
    
Implementing your changes didn't fix the issue of the values saving and being displayed in the meta-boxes. –  MF1 Feb 21 '13 at 2:17
add comment

I would try using the post ID that's automatically passed to save_events, rather than the $post global. So:

 function save_events( $post_id ){

    if ( !wp_verify_nonce( $_POST['events-nonce'], 'events-nonce' )) {
        return $post_id;
    }

    if ( !current_user_can( 'edit_post', $post_id ))
        return $post_id; 

    update_post_meta($post_id, "events_price", $POST['events_price'] ); 
    update_post_meta($post_id, "events_location", $POST['events_location'] ); 
 }

I'm not sure the global $post is reliable here.

If that fails, check that your nonce is being received correctly.

share|improve this answer
    
Where did you guys find this return $post_id? –  brasofilo Feb 21 '13 at 0:42
    
^ Read the original code. –  DigitalSea Feb 21 '13 at 0:44
add comment

The problem with the code was that price and location were not saving. After doing numerous testing, we saw that there was a syntax error and wnderscores are missing in

    update_post_meta($post_id, "events_price",  $POST["events_price"] );  
    update_post_meta($post_id, "events_location", $POST["events_location"] ); 

Changing $POST to $_POST, fixed the problem.

share|improve this answer
    
Would you please explain how this relates to the question and why it would solve the issue? The whys and wherefores may be obvious to you but are likely not obvious to the person asking or there would have been no reason for the question. –  s_ha_dum Feb 21 '13 at 15:57
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.