admin管理员组文章数量:1122846
Who can help me a little.
I try to get an admin error notice if a metabox field is empty during save post. Wordpress refresh the page after saving so the hook admin_notices
does not work.
Here is my saving code
add_action('save_post', function($post_id){
if(isset($_POST['add']) && $_POST['add'] != ''){
update_post_meta($post_id, 'add', $_POST['add']);
}else{
add_action( 'admin_notices', function(){
?>
<p><?php _e( 'Error!', 'Field add is empty!' ); ?></p>
<?php
});
}
}
Who can help me a little.
I try to get an admin error notice if a metabox field is empty during save post. Wordpress refresh the page after saving so the hook admin_notices
does not work.
Here is my saving code
add_action('save_post', function($post_id){
if(isset($_POST['add']) && $_POST['add'] != ''){
update_post_meta($post_id, 'add', $_POST['add']);
}else{
add_action( 'admin_notices', function(){
?>
<p><?php _e( 'Error!', 'Field add is empty!' ); ?></p>
<?php
});
}
}
Share
Improve this question
asked May 20, 2024 at 22:32
RickBRickB
1
1
|
1 Answer
Reset to default 0You could for example save an error flag to a transient in save_post
, if a required field is empty, and the display the admin notice based on the transient. This concept is demonstrated in in this old answer.
Another option is to add a simple script to the editing view which would alert the user that the said field is empty. Or just add required
html attribute to the input field.
本文标签: hooksShow admin notice if metabox field is empty during save post
版权声明:本文标题:hooks - Show admin notice if metabox field is empty during save post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736306425a1932954.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
required
attribute to the meta field? – Chris Cox Commented May 26, 2024 at 11:18