Php 如何在添加新自定义帖子和编辑自定义帖子时添加错误消息
我试图在自定义后期保存操作中的自定义字段上添加错误消息,但我的验证无效。检查下面的代码Php 如何在添加新自定义帖子和编辑自定义帖子时添加错误消息,php,wordpress,custom-post-type,Php,Wordpress,Custom Post Type,我试图在自定义后期保存操作中的自定义字段上添加错误消息,但我的验证无效。检查下面的代码 add_action( 'save_post', 'save_event_meta_data' ); function save_event_meta_data( $post_id ) { $event_university = $_POST['event_university']; if ( isset( $_POST['post_type'] ) && 'tribe_even
add_action( 'save_post', 'save_event_meta_data' );
function save_event_meta_data( $post_id ) {
$event_university = $_POST['event_university'];
if ( isset( $_POST['post_type'] ) && 'tribe_events' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
if($event_university=="") {
new WP_Error( 'Error', __( "Please select university" ) );
return;
}
$event_university_data = $event_university ;
update_post_meta( $post_id, 'event_university', $event_university_data );
}
请执行所需操作。要在其他页面上显示错误消息,需要将其保存在某个变量中。所以试试下面的代码
if($event_university=="") {
global $error;
$error = new WP_Error();
$error->add("Please select university");
return;
}
然后在另一个页面上,您可以使用以下代码访问该错误消息:
global $error;
echo $error->get_error_message();