0

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' );
4
  • Please share the complete code Commented Jan 30, 2019 at 12:45
  • it is complete code when on line number 106 here selected( $evn_timezone["$country->name"] , esc_attr("$country->name"), false); I am getting error Commented Jan 30, 2019 at 12:47
  • What is $data? What does it contain? Where does it come from? Commented Jan 30, 2019 at 12:50
  • i am fetching json data in $data variable. Commented Jan 30, 2019 at 12:58

2 Answers 2

0

It's probably failing because Andorra isn't a key in $evn_timezone.

Can you var_dump($evn_timezone); so we can see what that contains?

You can also add checks such as:

if(isset($evn_timezone[$country->name])) {
   selected($evn_timezone["$country->name"], esc_attr("$country->name"), false);
}

That way, if the country isn't in the array, you won't get an error.

3
  • it is just printing string(5) "Array" . Commented Jan 30, 2019 at 12:56
  • OK, so the type is correct - so you should be able to see 5 country names listed then - and I'm guessing that Andorra isn't on there? This means that you have countries in $data which are not in $evn_timezone. Commented Jan 30, 2019 at 13:04
  • yes you are right. Commented Jan 30, 2019 at 13:05
0

Also I am editing your codes. It is not following the correct WP coding structure. I am simplifying it

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 = ( $evn_timezone[ $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>';
6
  • Chinmoy Kumar Paul great error gone. But while clicking on submit select option data in not saving. Commented Jan 30, 2019 at 13:00
  • you will check the option value into the printf statement and your callback function of save_post hook. Commented Jan 30, 2019 at 13:03
  • no error is coming when i use inspect element error showing there but not in browser so issue is there. Commented Jan 30, 2019 at 13:04
  • what is the code under save_post action? Commented Jan 30, 2019 at 13:09
  • Can you replace $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 ); with $selected = ( ! empty( $evn_timezone ) && $country->name == esc_attr( $evn_timezone ) ) ? ' selected="selected"' : ''; printf( '<option value="%s"%s>%s</option>', $country->name, $selected, $country->name ); Commented Jan 30, 2019 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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