我为我的自定义主题创建了这个元框。我以为我遵守了所有的指导原则,但我遇到了麻烦。meta框和所有字段都会显示出来,但当我更新时,数据不会保存。
Meta box addition:
function drw_events_metabox(){
add_meta_box(\'events_info\', \'EVENT INFO\', \'drw_evenet_info_clbk\', \'drw-events\', \'side\', \'high\');
}
function drw_evenet_info_clbk($post){
wp_nonce_field(\'drw_event_info\', \'drw_event_info_nonce\');
$datevalue = get_post_meta( $post->ID, \'_event_date_valkey\', true);
$starttimevalue = get_post_meta( $post->ID, \'_event_time_valkey\', true);
$endtimevalue = get_post_meta( $post->ID, \'_event_endtime_valkey\', true);
$covervalue = get_post_meta( $post->ID, \'_event_cover_valkey\', true);
$freeEvent = get_post_meta( $post->ID, \'_event_free_valkey\', true);
$stdTicketLink = get_post_meta( $post->ID, \'_event_stdticket_valkey\', true);
$customTicketLink = get_post_meta( $post->ID, \'_event_customTicketLink_valkey\', true);
$eventCancelled = get_post_meta( $post->ID, \'_event_cancel_valkey\', true);
//event date
echo\'<br><label for="drw_event_date_field">Event Date </label>\';
echo \'<input type="date" id="drw_event_date_field" name="drw_event_date_field" value="\'.esc_attr($datevalue).\'" required/><br><br><hr><br>\';
//event time
echo\'<label for="drw_event_time_field">Event start time </label>\';
echo \'<input type="time" id="drw_event_time_field" name="drw_event_time_field" value="\'.esc_attr($starttimevalue).\'" required/><br><br>\';
echo\'<label for="drw_event_endtime_field">Event end time </label>\';
echo \'<input type="time" id="drw_event_endtime_field" name="drw_event_endtime_field" value="\'.esc_attr($endtimevalue).\'" required/><br><br><hr><br>\';
//event price
echo\'<label for="drw_event_free_field">Event is free </label>\';
echo\'<input type="checkbox" id="drw_event_free_field" name="drw_event_free_field" value="\'.esc_attr($freeEvent).\'" checked/><br><br>\';
echo\'<label for="drw_event_cover_field">Event cover $ </label>\';
echo \'<input type="number" id="drw_event_cover_field" name="drw_event_cover_field" value="\'.esc_attr($covervalue).\'" size="2"/><br><br><hr><br>\';
//event ticket
echo \'<h2><strong><em>Choose a ticket purchase link</em></strong></h2><br>\';
echo\'<label for="drw_event_stdtktlink_field">Default link </label>\';
echo \'<input type="checkbox" id="drw_event_stdtktlink_field" name="drw_stdtktlink_field" value="#" checked/><br><br>\';
echo\'<label for="drw_event_customTicketLink_field">Custom link </label>\';
echo \'<input type="text" id="drw_event_customTicketLink_field" name="drw_customTicketLink_field" value="\'.esc_attr($customTicketLink).\'"/><br><br><hr><br>\';
//event cancellation
echo \'<h2><strong><em>**Use only for event cancellation!</em></strong></h2><br>\';
echo\'<label for="drw_event_cancelled_field">Event Cancelled </label>\';
echo \'<input type="checkbox" id="drw_event_cancelled_field" name="drw_event_cancelled_field" value="\'.esc_attr($eventCancelled).\'"/><br><br><hr><br>\';
}
add_action(\'add_meta_boxes\', \'drw_events_metabox\');
Saving meta box data:
function save_drw_events_metabox_data( $post_id ) {
if( ! isset( $_POST[\'drw_event_info_nonce\'] ) ){
return;
}
if( ! wp_verify_nonce( $_POST[\'drw_event_info_nonce\'], \'save_drw_events_metabox_data\') ) {
return;
}
if( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ){
return;
}
if( ! current_user_can( \'edit_post\', $post_id ) ) {
return;
}
$event_date = sanitize_text_field( $_POST[\'drw_event_date_field\'] );
$event_time = sanitize_text_field( $_POST[\'drw_event_time_field\'] );
$event_entime = sanitize_text_field( $_POST[\'drw_event_endtime_field\'] );
$event_free = sanitize_text_field( $_POST[\'drw_event_free_field\'] );
$event_cover = sanitize_text_field( $_POST[\'drw_event_cover_field\'] );
$event_linkdef = sanitize_text_field( $_POST[\'drw_stdtktlink_field\'] );
$event_tix = sanitize_text_field( $_POST[\'drw_customTicketLink_field\'] );
$event_cancel = sanitize_text_field( $_POST[\'drw_event_cancelled_field\'] );
if( ! isset( $_POST[\'drw_event_date_field\'] ) ) {
return;
update_post_meta($post_id, \'_event_date_valkey\', $event_date);
}
if( ! isset( $_POST[\'drw_event_time_field\'] ) ) {
return;
update_post_meta($post_id, \'_event_time_valkey\', $event_time);
}
if( ! isset( $_POST[\'drw_event_endtime_field\'] ) ) {
return;
update_post_meta($post_id, \'_event_endtime_valkey\', $event_entime);
}
if( ! isset( $_POST[\'drw_event_cover_field\'] ) ) {
return;
update_post_meta($post_id, \'_event_cover_valkey\', $event_cover);
}
if( ! isset( $_POST[\'drw_event_free_field\'] ) ) {
return;
update_post_meta($post_id, \'_event_free_valkey\', true);
} else {
update_post_meta( $post_id, \'_event_free_valkey\', FALSE );
}
if( ! isset( $_POST[\'drw_stdtktlink_field\'] ) ) {
return;
update_post_meta($post_id, \'_event_stdticket_valkey\', $event_linkdef);
}
if( ! isset( $_POST[\'drw_customTicketLink_field\'] ) ) {
return;
update_post_meta($post_id, \'_event_customTicketLink_valkey\', $event_tix);
}
if( ! isset( $_POST[\'drw_event_cancelled_field\'] ) ) {
return;
update_post_meta($post_id, \'_event_cancel_valkey\', true);
} else {
update_post_meta( $post_id, \'_event_cancel_valkey\', FALSE );
}
}
add_action( \'save_post\', \'save_drw_events_metabox_data\');
真的,我不知道我在这一点上遗漏了什么。我所有的研究都表明代码是正确的,但仍然没有保存任何数据!
请帮忙。
最合适的回答,由SO网友:Paul \'Sparrow Hawk\' Biron 整理而成
为了跟进@mmm的评论,您有以下几点陈述:
if ( ! isset ($_POST[\'drw_event_date_field\'] ) ) {
return ;
update_post_meta ($post_id, \'_event_date_valkey\', $event_date) ;
}
请注意,您正在将呼叫转接到
update_post_meta()
内部
!isset()
条件。
您想要的是:
if ( ! isset ($_POST[\'drw_event_date_field\'] ) ) {
return ;
}
update_post_meta ($post_id, \'_event_date_valkey\', $event_date) ;
此外,即使如上所述更正逻辑,也可能无法保存某些值,例如,如果未设置“drw\\U event\\U date\\U field”,则不会保存
any 值,即使是那些已设置的值。。。这可能不是你想要的。
您可能会更好地使用以下逻辑:
$fields = array (
\'_event_time_valkey\' => \'drw_event_date_field\',
\'_event_endtime_valkey\' => \'drw_event_time_field\',
\'_event_endtime_valkey\' => \'drw_event_endtime_field\',
\'_event_free_valkey\' => \'drw_event_free_field\',
\'_event_cover_valkey\' => \'drw_event_cover_field\',
\'_event_stdticket_valkey\' => \'drw_stdtktlink_field\',
\'_event_customTicketLink_valkey\' => \'drw_customTicketLink_field\',
\'_event_cancel_valkey\' => \'drw_event_cancelled_field\',
) ;
foreach ($fields as $meta_key => $field) {
if (!isset ($_POST[$field])) {
if ($field == \'drw_event_cancelled_field\') {
update_post_meta ($post_id, $meta_key, false) ;
}
continue ;
}
$meta_val = sanitize_text_field ($_POST[$field]) ;
update_post_meta ($post_id, $meta_key, $meta_val) ;
}