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've built a custom post type called "Events". Each event has a future date and I don't want to use the normal postdate of the post.

I want to use add_meta_box to add some inputs for this "event-date".

Do you have any idea how I could use the probably same date-picker as there is right now for the postdate in a meta_box?

Thank you in advance.

matt

update:

add_action("admin_init", "admin_init");

function admin_init(){
    add_meta_box("event-date-meta", "Event Date", "event_date", "wr_event", "side", "low");
}


function year_completed(){
    global $post;
    $custom = get_post_custom($post->ID);
    //…
    ?>
    <label>Event-Date:</label>
    <div id="timestampdiv" class=""><?php touch_time(($action == 'edit'),1,4); ?></div>
    <?php
}
share|improve this question
1  
take a look at my answer to your other question wordpress.stackexchange.com/a/48408/2487 –  Bainternet Apr 9 '12 at 15:19
add comment

1 Answer

up vote 0 down vote accepted

Never really thought about it, but it should indeed be a good option.

The date picker is called from wp-admin/includes/meta-boxes.php (line 183) as:

<div id="timestampdiv" class="hide-if-js"><?php touch_time(($action == 'edit'),1,4); ?></div>

and touch_time() is defined in wp-admin/includes/template.php (line 550) as:

/**
 * {@internal Missing Short Description}}
 *
 * @since 0.71
 *
 * @param unknown_type $edit
 * @param unknown_type $for_post
 * @param unknown_type $tab_index
 * @param unknown_type $multi
 */
function touch_time( $edit = 1, $for_post = 1, $tab_index = 0, $multi = 0 ) {
    global $wp_locale, $post, $comment;

    if ( $for_post )
        $edit = ! ( in_array($post->post_status, array('draft', 'pending') ) && (!$post->post_date_gmt || '0000-00-00 00:00:00' == $post->post_date_gmt ) );

    $tab_index_attribute = '';
    if ( (int) $tab_index > 0 )
        $tab_index_attribute = " tabindex=\"$tab_index\"";

    // echo '<label for="timestamp" style="display: block;"><input type="checkbox" class="checkbox" name="edit_date" value="1" id="timestamp"'.$tab_index_attribute.' /> '.__( 'Edit timestamp' ).'</label><br />';

    $time_adj = current_time('timestamp');
    $post_date = ($for_post) ? $post->post_date : $comment->comment_date;
    $jj = ($edit) ? mysql2date( 'd', $post_date, false ) : gmdate( 'd', $time_adj );
    $mm = ($edit) ? mysql2date( 'm', $post_date, false ) : gmdate( 'm', $time_adj );
    $aa = ($edit) ? mysql2date( 'Y', $post_date, false ) : gmdate( 'Y', $time_adj );
    $hh = ($edit) ? mysql2date( 'H', $post_date, false ) : gmdate( 'H', $time_adj );
    $mn = ($edit) ? mysql2date( 'i', $post_date, false ) : gmdate( 'i', $time_adj );
    $ss = ($edit) ? mysql2date( 's', $post_date, false ) : gmdate( 's', $time_adj );

    $cur_jj = gmdate( 'd', $time_adj );
    $cur_mm = gmdate( 'm', $time_adj );
    $cur_aa = gmdate( 'Y', $time_adj );
    $cur_hh = gmdate( 'H', $time_adj );
    $cur_mn = gmdate( 'i', $time_adj );

    $month = "<select " . ( $multi ? '' : 'id="mm" ' ) . "name=\"mm\"$tab_index_attribute>\n";
    for ( $i = 1; $i < 13; $i = $i +1 ) {
        $monthnum = zeroise($i, 2);
        $month .= "\t\t\t" . '<option value="' . $monthnum . '"';
        if ( $i == $mm )
            $month .= ' selected="selected"';
        $month .= '>' . $monthnum . '-' . $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) ) . "</option>\n";
    }
    $month .= '</select>';

    $day = '<input type="text" ' . ( $multi ? '' : 'id="jj" ' ) . 'name="jj" value="' . $jj . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off" />';
    $year = '<input type="text" ' . ( $multi ? '' : 'id="aa" ' ) . 'name="aa" value="' . $aa . '" size="4" maxlength="4"' . $tab_index_attribute . ' autocomplete="off" />';
    $hour = '<input type="text" ' . ( $multi ? '' : 'id="hh" ' ) . 'name="hh" value="' . $hh . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off" />';
    $minute = '<input type="text" ' . ( $multi ? '' : 'id="mn" ' ) . 'name="mn" value="' . $mn . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off" />';

    echo '<div class="timestamp-wrap">';
    /* translators: 1: month input, 2: day input, 3: year input, 4: hour input, 5: minute input */
    printf(__('%1$s%2$s, %3$s @ %4$s : %5$s'), $month, $day, $year, $hour, $minute);

    echo '</div><input type="hidden" id="ss" name="ss" value="' . $ss . '" />';

    if ( $multi ) return;

    echo "\n\n";
    foreach ( array('mm', 'jj', 'aa', 'hh', 'mn') as $timeunit ) {
        echo '<input type="hidden" id="hidden_' . $timeunit . '" name="hidden_' . $timeunit . '" value="' . $$timeunit . '" />' . "\n";
        $cur_timeunit = 'cur_' . $timeunit;
        echo '<input type="hidden" id="'. $cur_timeunit . '" name="'. $cur_timeunit . '" value="' . $$cur_timeunit . '" />' . "\n";
    }
?>

<p>
<a href="#edit_timestamp" class="save-timestamp hide-if-no-js button"><?php _e('OK'); ?></a>
<a href="#edit_timestamp" class="cancel-timestamp hide-if-no-js"><?php _e('Cancel'); ?></a>
</p>
<?php
}

Doesn't look very difficult to work it from there... will try.

share|improve this answer
    
Thank you! Let me know what you come up with! If you remove the "hide-if-js" class it already appears as I want it! I updated my question with some code … –  mathiregister Apr 9 '12 at 15:40
    
I just don't know how to store/save this information correctly! And how I read it afterwards. I of course want to sort all my events by simply by date! –  mathiregister Apr 9 '12 at 15:41
    
I would store it as you'd normally store a custom-value, using update_post_meta so you'd probably split that touch_time() function in two, using one of them to build the form as your callback in add_meta_box and the other one fetching values from $_POST[] abd saving, hooked to the save_post action. –  moraleida Apr 9 '12 at 15:53
    
Thank you! Would you mind giving me an example of how I could store those values and read them in the front-end? I've never worked with this stuff before. I would really appreciate it! What do you mean by splitting the touch_time() function in two? –  mathiregister Apr 9 '12 at 19:54
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.