我有一个自定义帖子类型发言人,而在扬声器中有一个自定义字段Speaker_organization.我该如何验证并在管理员通知中显示错误.
add_action( 'add_Meta_Boxes', 'speaker_organization_Box' );
function speaker_organization_Box() {
add_Meta_Box(
'speaker_organization',
__( 'Organization', 'dbem' ),
'speaker_organization_Box_content',
'speaker',
'side',
'high'
);
}
function speaker_organization_Box_content( $post ) {
// generate a nonce field
wp_nonce_field( basename( __FILE__ ), 'dbem-speaker-organization-nonce' );
// get prevIoUsly saved Meta values (if any)
$speaker_organization = get_post_meta( $post->ID, 'speaker_organization', true );
echo '<label for="speaker_organization"></label>';
echo '<input type="text" id="speaker_organization" name="speaker_organization" placeholder="Organization Name" value="'.$speaker_organization.'" />';
}
function speaker_organization_Box_save( $post_id ) {
$speaker_organization = $_POST['speaker_organization'];
update_post_Meta( $post_id, 'speaker_organization', $speaker_organization );
}
add_action( 'save_post', 'speaker_organization_Box_save' );
解决方法:
使用
add_action( ‘admin_notices’, ‘my_admin_notice’ );
或使用验证js
add_action('admin_enqueue_scripts', 'add_my_js');
function add_my_js(){
wp_enqueue_script('my_validate', 'path/to/jquery.validate.min.js', array('jquery'));
wp_enqueue_script('my_script_js', 'path/to/my_script.js');
}
jQuery().ready(function() {
jQuery("#post").validate();
});
<input type="text" name="my_custom_text_field" class="required"/>
function wpse_update_post_custom_values($post_id, $post) {
// Do some checking...
if($_POST['subhead'] != 'value i expect') {
// Add an error here
$errors->add('oops', 'There was an error.');
}
return $errors;
}
add_action('save_post','wpse_update_post_custom_values',1,2);
using condition + admin notice
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。