Complete Code.
// Set UI labels for Custom Post Type
$labels = array(
'name' => _x( 'Events', 'evn_calendar'),
'singular_name' => _x( 'events', 'evn_calendar'),
'menu_name' => __( 'Events', 'evn_calendar' ),
'parent_item_colon' => __( 'Parent Event', 'evn_calendar' ),
'all_items' => __( 'All Events', 'evn_calendar' ),
'view_item' => __( 'View Event', 'evn_calendar' ),
'add_new_item' => __( 'Add New Event', 'evn_calendar' ),
'add_new' => __( 'Add New', 'evn_calendar' ),
'edit_item' => __( 'Edit Event', 'evn_calendar' ),
'update_item' => __( 'Update Event', 'evn_calendar' ),
'search_items' => __( 'Search Event', 'evn_calendar' ),
'not_found' => __( 'Not Found', 'evn_calendar' ),
'not_found_in_trash' => __( 'Not found in Trash', 'evn_calendar' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'events', 'evn_calendar' ),
'description' => __( 'Event news and reviews', 'evn_calendar' ),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', ),
// You can associate this CPT with a taxonomy or custom taxonomy.
'taxonomies' => array( 'genres' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 20,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
);
// Registering your Custom Post Type
register_post_type( 'events', $args );
function eventcalendar_add_meta_box() {
$screens = array( 'events' );
foreach ( $screens as $screen ) {
add_meta_box(
'Event Information',
'Event Information',
'eventcalendar_show_custom_meta_box',
$screen,
'normal',
'high'
);
}
}
add_action( 'add_meta_boxes', 'eventcalendar_add_meta_box' );
function eventcalendar_show_custom_meta_box( $post ) {
wp_nonce_field( 'evn_from_date', 'evn_from_date_nonce' );
$from_date = get_post_meta( $post->ID, '_evn_from_date', true );
echo '<label for="evn_from_date">'; _e( 'When', 'event-calendar-plugin' ); echo '</label> ';
echo '<input type="date" id="evn_from_date" name="evn_from_date" value="' . esc_attr( $from_date ) . '" size="25" />';
wp_nonce_field( 'evn_from_time', 'evn_from_time_nonce' );
$from_time = get_post_meta( $post->ID, '_evn_from_time', true );
echo '<input type="time" id="evn_from_time" name="evn_from_time" value="' . esc_attr( $from_time ) . '" size="25" />';
wp_nonce_field( 'evn_to_date', 'evn_to_date_nonce' );
$from_date = get_post_meta( $post->ID, '_evn_to_date', true );
echo '<label for="evn_to_date">'; _e( 'To', 'event-calendar-plugin' ); echo '</label> ';
echo '<input type="date" id="evn_to_date" name="evn_to_date" value="' . esc_attr( $from_date ) . '" size="25" />';
wp_nonce_field( 'evn_to_time', 'evn_to_time_nonce' );
$from_time = get_post_meta( $post->ID, '_evn_to_time', true );
echo '<input type="time" id="evn_to_time" name="evn_to_time" value="' . esc_attr( $from_time ) . '" size="25" />';
$request = wp_safe_remote_get(plugins_url('/js/latest.json', __FILE__));
$data = wp_remote_retrieve_body( $request );
$data = json_decode($data);
wp_nonce_field( 'evn_timezone', 'evn_timezone_nonce' );
$evn_timezone = get_post_meta( $post->ID, '_evn_timezone', true );
echo '<select id="evn_timezone" name="evn_timezone" >';
foreach ($data->countries as $country) {
$selected = ( $country[ $country->name ] == esc_attr( $country->name ) ) ? ' selected="selected"' : '';
printf( '<option value="%s(%s)"%s>%s</option>', $country->abbr, $country->name, $selected, $country->name );
}
echo '</select>';
wp_nonce_field( 'evn_all_day', 'evn_all_day_nonce' );
$evn_all_day = get_post_meta( $post->ID, 'evn_all_day', true );
echo '<input type="checkbox" id="evn_all_day" name="evn_all_day" value="' . esc_attr( $evn_all_day ) . '" />';
echo '<label for="evn_all_day">'; _e( 'All day?', 'event-calendar-plugin' ); echo '</label> ';
wp_nonce_field( 'evn_location', 'evn_location_nonce' );
$evn_location = get_post_meta( $post->ID, '_evn_location', true );
echo '<br><label for="evn_location">'; _e( 'Event Location', 'event-calendar-plugin' ); echo '</label> ';
echo '<input type="text" id="evn_location" name="evn_location" value="' . esc_attr( $evn_location ) . '" size="100" />';
wp_nonce_field( 'evn_link', 'evn_link_nonce' );
$evn_link = get_post_meta( $post->ID, '_evn_link', true );
echo '<br><label for="evn_link">'; _e( 'Link', 'event-calendar-plugin' ); echo '</label> ';
echo '<input type="text" id="evn_link" name="evn_link" value="' . esc_attr( $evn_link ) . '" size="100" placeholder=""/>';
}
function eventcalendar_save_meta_box_data( $post_id ) {
if ( ! isset( $_POST['evn_from_date_nonce'] ) &&
! isset( $_POST['evn_from_time_nonce'] ) &&
! isset( $_POST['evn_to_date_nonce'] ) &&
! isset( $_POST['evn_to_time_nonce'] ) &&
! isset( $_POST['evn_location_nonce'] ) &&
! isset( $_POST['evn_timezone'] ) &&
! isset( $_POST['evn_link'] ))
{
return;
}
if (! wp_verify_nonce( $_POST['evn_from_date_nonce'], 'evn_from_date' ) &&
! wp_verify_nonce( $_POST['evn_from_time_nonce'], 'evn_from_time' ) &&
! wp_verify_nonce( $_POST['evn_to_date_nonce'], 'evn_to_date' ) &&
! wp_verify_nonce( $_POST['evn_to_time_nonce'], 'evn_to_time' ) &&
! wp_verify_nonce( $_POST['evn_timezone_nonce'], 'evn_timezone' ) &&
! wp_verify_nonce( $_POST['evn_location_nonce'], 'evn_location' ) &&
! wp_verify_nonce( $_POST['evn_link_nonce'], 'evn_link' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
if ( ! isset( $_POST['evn_from_date'] ) &&
! isset( $_POST['evn_from_time'] ) &&
! isset( $_POST['evn_to_date'] ) &&
! isset( $_POST['evn_to_time'] ) &&
! isset( $_POST['evn_timezone'] ) &&
! isset( $_POST['evn_location'] )&&
! isset( $_POST['evn_link'] )
) {
return;
}
$evn_from_date = sanitize_text_field( $_POST['evn_from_date'] );
update_post_meta( $post_id, '_evn_from_date', $evn_from_date );
$evn_from_time = sanitize_text_field( $_POST['evn_from_time'] );
update_post_meta( $post_id, '_evn_from_time', $evn_from_time );
$evn_to_date = sanitize_text_field( $_POST['evn_to_date'] );
update_post_meta( $post_id, '_evn_to_date', $evn_to_date );
$evn_to_time = sanitize_text_field( $_POST['evn_to_time'] );
update_post_meta( $post_id, '_evn_to_time', $evn_to_time );
$evn_timezone = sanitize_text_field( $_POST['evn_timezone'] );
update_post_meta( $post_id, '_evn_timezone', $evn_timezone );
$evn_link = sanitize_text_field( $_POST['evn_link'] );
update_post_meta( $post_id, '_evn_link', $evn_link );
$evn_location = sanitize_text_field( $_POST['evn_location'] );
update_post_meta( $post_id, '_evn_location', $evn_location );
}
add_action( 'save_post', 'eventcalendar_save_meta_box_data' );
$data
? What does it contain? Where does it come from?