0

I am following Noel Tock's tutorial on making an Events CPT and so far I was able to show the custom columns and the custom meta box.

The problem now is that I cannot save the data inside the meta box. I am confused as I have found that when I press the update button, the values won't stay on the fields and won't reflect on the custom columns. The code is similar to the tutorial though.

The custom columns:

function events_custom_columns($column){
global $post;
$custom = get_post_custom();
switch($column){
    case 'event_col_type':
            $eventcats = get_the_terms($post->ID, 'event_type');
            $ev_cat_html = array();
            if($eventcats){
                foreach($eventcats as $eventcat)
                    array_push($ev_cat_html, $eventcat->name);
                    echo implode($ev_cat_html, ', ');
            }
            else {
                echo 'None';
            }
    break;
    case 'event_date':
            $start = $custom['event_start_date'][0];
            $end = $custom['event_end_date'][0];

            $startdate = date('F j, Y', $start);
            $enddate = date('F j, Y', $end);

            echo $startdate . '<br><em>' . $enddate . '</em>';
    break;
    case 'event_time':

            $time_formtt = get_option('time_format');
            $starttime = $custom['event_start_date'][0];
            $endtime = $custom['event_end_date'][0];

            $startT = date($time_formtt, $starttime);
            $endT = date($time_formtt, $endtime);

            echo $startT . ' - ' . $endT;

    break;
    case 'event_location':
            echo $custom['event_location'][0];
    break;
}

}

The custom meta box: add_action('admin_init', 'events_admin_init');

function events_admin_init(){
    add_meta_box('event_meta', 'Event Details', 'event_details_meta', 'events', 'normal', 'default');
}

function event_details_meta(){
global $post;
$custom = get_post_custom($post->ID);

$meta_SDate = $custom['event_start_date'][0];
$meta_EDate = $custom['event_end_date'][0];
$meta_STime = $meta_SDate;
$meta_ETime = $meta_EDate;

$time_format = get_option('time_format');

if($meta_SDate == null){
    $meta_SDate = time();
    $meta_EDate = $meta_SDate;
    $meta_STime = 0;
    $meta_ETime = 0;
}

$sd = date("D, M d, Y", $meta_SDate);
$ed = date("D, M d, Y", $meta_EDate);
$st = date($time_format, $meta_STime);
$et = date($time_format, $meta_ETime);

echo '<input type = "hidden" name = "events_nonce1" id = "events_nonce1" value = "' . wp_create_nonce('events_nonce1') . '>"';

?>

    <ul style = "list-style-type: none;">
        <li><label>Start Date: </label><input name = "event_start_date" class = "dp_date" value = "<?php echo $sd; ?>"</li>
        <li><label>End Date: </label><input name = "event_end_date" class = "dp_date" value = "<?php echo $ed; ?>"</li>
        <li><label>Start Time: </label><input name = "event_start_time" value = "<?php echo $st; ?>"</li>
        <li><label>End Time: </label><input name = "event_end_time" value = "<?php echo $et; ?>"</li>
    </ul>

<?php
}

Here's the save function:

add_action('save_post', 'save_events_deets');

function save_events_deets(){
   global $post;

   if(!wp_verify_nonce1($_POST['events_nonce1'], 'events_nonce1'))
   {    return $post -> ID;}

   if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
       return;

   if(!current_user_can('edit_post', $post -> ID))
   {    return $post -> ID;}

   if(!isset($_POST['event_start_date'])){
       return $post;
   }

   $updStartD = strtotime($_POST['event_start_date'] . $_POST['event_start_time']);
   update_post_meta($post -> ID, 'event_start_date', $updStartD);


   if(!isset($_POST['event_end_date'])){
       return $post;
   }

   $updEndD = strtotime($_POST['event_end_date'] . $_POST['event_end_time']);
   update_post_meta($post -> ID, 'event_end_date', $updEndD);   
}
  • Make sure you have debugging with logging enabled, run the code again, then check wp-content/debug.log and report back with the errors (if any). – TheDeadMedic May 15 '16 at 9:18
  • @TheDeadMedic I found 2 same errors, Undefined index on $meta_SDate = $custom['event_start_date'][0]; $meta_EDate = $custom['event_end_date'][0]; – Smee May 15 '16 at 9:37
  • check events_nonce vs. events_nonce1? – majick May 15 '16 at 10:44
  • @majick i have changed it. but problem still persists. – Smee May 15 '16 at 11:19
  • is this the actual code now? wp_verify_nonce is a function but wp_verify_nonce1 is not... the previous mismatch was only in the nonce name not the function. – majick May 19 '16 at 7:59
0

Since you are using single values anyway, how about trying just:

$meta_SDate = get_post_meta($post->ID,'event_start_date',true);
$meta_EDate = get_post_meta($post->ID,'event_end_date',true);

Also note the following lines will need a space to separate the posted values so strtotime can work properly:

$updStartD = strtotime(trim($_POST['event_start_date']) . ' ' . trim($_POST['event_start_time']));
$updEndD = strtotime(trim($_POST['event_end_date']) . ' ' . trim($_POST['event_end_time']));

I have added trim to remove left and right whitespace just in case too.

  • it's still not doing anything after i changed it to that... – Smee May 16 '16 at 22:31
  • have you checked the database table to see if the values are actually saving? it would narrow it down to whether it is a saving or retrieving problem. – majick May 18 '16 at 5:42
  • yes i did, and the values weren't saving. i am using a plugin for now, but i am also eager to know the root of the problem. – Smee May 19 '16 at 5:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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