我正在为一个插件重建一个元盒,它现在不会保存我输入的数据。HTML几乎与以前完全相同,但save\\u元函数根本不起作用。我已经阅读了不少教程,在StackExchange和其他几个网站上查看了大量问题,并试图找出任何容易的陷阱,但这些似乎都不是问题所在。有人能看看这个,告诉我我错过了什么吗?
备注:
这都是一节课。
time\\U date\\U元函数中的数组用于另一个返回HTML的函数。据我所知,这不是问题所在。HTML字段显示正确,其源代码与我之前手动写出的HTML字段几乎相同。手写HTML与此函数输出之间的区别在于名称、类和id属性。
当我在重写此元框之前编辑一篇已经包含元数据的文章时,只有时间和位置字段显示正确的数据。我没有任何与视频URL和日期没有任何工作。
function __construct() {
// Add Seminar time and date meta box
add_action( \'load-post.php\', array( $this, \'seminar_create_meta_box\' ) );
add_action( \'load-post-new.php\', array( $this, \'seminar_create_meta_box\' ) );
// Save meta data
add_action( \'save_post\', array( $this, \'save_meta\' ), 10, 2 );
}
function seminar_create_meta_box() {
add_meta_box( \'cemb_seminar_meta\', \'Seminar Info\', array ( $this, \'time_date_meta\'), \'cemb_seminar\', \'side\', \'high\' );
} // End of seminar_create_meta_box()
function time_date_meta( $post ) {
// Grab data
global $post;
$checks = array( \'date\', \'hour\', \'minute\', \'ampm\', \'location\' );
$metas = array();
foreach ( $checks as $check ) {
$meta_key = \'cemb_seminar_\' . $check;
$meta_value = get_post_meta( $post->ID, $meta_key, true );
$date = ( $check == \'date\' && empty( $meta_value ) ? time() : date( \'D, n/j/Y\', strtotime( $meta_value ) ) );
$value = ( $check == \'date\' ? $date : esc_attr( $meta_value ) );
$metas[$check] = ( !empty( $meta_value ) ? $value : \'\' );
} // End of $checks foreach
// Get optional video URL
$meta_value = get_post_meta( $post->ID, \'cemb_seminar_video_url\', true );
$metas[\'videourl\'] = ( !empty( $meta_value ) ? esc_url( $meta_value ) : \'\' );
// Security
wp_nonce_field( basename( __FILE__ ), \'cemb_seminar_nonce\' );
// Output
?>
<div class="cemb_seminar_meta">
<ul>
<li><?php
$datepicker_args[\'class\'] = \'seminar_date\';
$datepicker_args[\'id\'] = \'cemb_seminar_date\';
$datepicker_args[\'label\'] = \'Date\';
$datepicker_args[\'value\'] = $metas[\'date\'];
echo $this->toolkit->date_picker( $datepicker_args ); ?></li>
<li><?php
$time_args[\'class\'] = \'floatright\';
$time_args[\'id\'] = \'cemb_seminar_time\';
$time_args[\'label\'] = \'Time\';
$time_args[\'hours\'][\'class\'] = \'seminar_time\';
$time_args[\'hours\'][\'id\'] = \'cemb_seminar_hour\';
$time_args[\'hours\'][\'military\'] = false;
$time_args[\'hours\'][\'value\'] = $metas[\'hour\'];
$time_args[\'minutes\'][\'class\'] = \'seminar_time\';
$time_args[\'minutes\'][\'id\'] = \'cemb_seminar_minute\';
$time_args[\'minutes\'][\'mintype\'] = \'quarters\';
$time_args[\'minutes\'][\'value\'] = $metas[\'minute\'];
$time_args[\'ampm\'][\'class\'] = \'seminar_time\';
$time_args[\'ampm\'][\'id\'] = \'cemb_seminar_ampm\';
$time_args[\'ampm\'][\'value\'] = $metas[\'ampm\'];
echo $this->toolkit->time_fields( $time_args ); ?></li>
<li><?php
$input_args[\'class\'] = \'seminar_location\';
$input_args[\'id\'] = \'cemb_seminar_location\';
$input_args[\'inputtype\'] = \'text\';
$input_args[\'label\'] = \'Location\';
$input_args[\'value\'] = $metas[\'location\'];
echo $this->toolkit->input_field( $input_args ); ?></li>
<li><?php
$input_args[\'class\'] = \'seminar_video\';
$input_args[\'id\'] = \'cemb_seminar_video_url\';
$input_args[\'inputtype\'] = \'text\';
$input_args[\'label\'] = \'Video URL\';
$input_args[\'value\'] = $metas[\'videourl\'];
echo $this->toolkit->input_field( $input_args ); ?></li>
</ul>
</div> <?php
} // End of seminar_meta_box()
function save_meta( $post_id ) {
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) {
return $post_id;
} // End of Auto-Save check
if ( $_POST[\'post_type\'] == \'cemb_seminar\' ) {
if ( !current_user_can( \'edit_post\', $post_id ) ) {
return $post_id;
} elseif ( !current_user_can( \'edit_page\', $post_id ) ) {
return $post_id;
} // End of capability check
} // End of post_type check
if ( empty( $_POST[\'cemb_seminar_nonce\'] ) && check_admin_referer( basename( __FILE__ ), \'cemb_seminar_nonce\' ) ) {
return $post_id;
} else {
$checks = array( \'date\', \'hour\', \'minute\', \'ampm\', \'location\', \'video_url\' );
foreach ( $checks as $check ) {
$meta_key = \'cemb_seminar_\' . $check;
$posted = $_POST[$meta_key];
if ( empty( $posted ) && $posted != \'cemb_seminar_video_url\' ) {
// Return an error message if any field but video_url is empty
$msg_args = array( \'which\' => \'empty\', \'field\' => $meta_key );
$notices[] = $this->display_notices( $msg_args );
return;
} else {
if ( $meta_key == \'cemb_seminar_date\' ) {
// Get the posted date and convert it to PHP time
$new_meta_value = ( isset( $posted ) ? strtotime( $posted ) : \'\' );
} else {
// Get the posted data and sanitize it for use as an HTML class.
$new_meta_value = ( isset( $posted ) ? sanitize_html_class( $posted ) : \'\' );
} // End of $meta_key check
// Get the meta value of the custom field key.
$meta_value = get_post_meta( $post_id, $meta_key, true );
if ( $new_meta_value && $meta_value == \'\' ) {
// If a new meta value was added and there was no previous value, add it.
add_post_meta( $post_id, $meta_key, $new_meta_value, true );
} elseif ( $new_meta_value && $new_meta_value != $meta_value ) {
// If the new meta value does not match the old value, update it.
update_post_meta( $post_id, $meta_key, $new_meta_value );
} elseif ( $new_meta_value == \'\' && $meta_value ) {
// If there is no new meta value but an old value exists, delete it.
delete_post_meta( $post_id, $meta_key, $meta_value );
} // End of meta value checks
} // End of empty check
} // End of $checks foreach
$semester_meta = get_post_meta( $post_id, \'_semester\', true );
if ( empty( $semester_meta ) ) {
$semester = $this->determine_semester( $date ) . \' \' . date( \'Y\', $date );
update_post_meta( $post_id, \'_semester\', $semester );
} // End of empty check
} // End of nonce check
} // End of save_meta()