我使用transition\\u post\\u状态解决了这个问题。
function check_post_title_length($new_status, $old_status, $post){
if($new_status == \'auto-draft\' || $new_status == \'draft\'){
return;
}
if(str_word_count($post->post_title, 0) > 12){
//update-nag is the term for a yellow warning box in admin_notices
//notice here I use the WP_Error data field to choose the type of error box to show
$admin_notices = new WP_Error(\'check_post_title_length\',\'If possible, consider making the post title shorter.\', \'update-nag\');
//we could also if we needed, add more notices to the list such as
//$admin_notices->add(\'some_other_check\', \'Sorry you need to add an author\');
add_user_meta(get_current_user_id(), \'admin_notices\', $admin_notices, true);
}
}
add_action(\'transition_post_status\', \'check_post_title_length\', 10, 3);
function display_admin_notices(){
$user_id = get_current_user_id();
$admin_notices = get_user_meta($user_id, \'admin_notices\', true);
if(!empty($admin_notices)){
//make sure its a WP_Error object
if(is_wp_error($admin_notices)){
//delete error from user meta so error is gone on page reload
delete_user_meta($user_id, \'admin_notices\');
$notices = $spm_admin_notices->get_error_messages();
if(!empty($notices)){
$notice_type = $admin_notices->get_error_data();
if(!empty($notice_type)){
?>
<div class="<?php echo $notice_type; ?>">
<?php
} else {
?>
<div class="error">
<?php
}
//here we loop through all notices we want to display
//this is useful if the user needs to adjust more than one item
foreach($notices AS $notice){
?>
<p><?php echo $notice; ?></p>
<?php
}
?>
</div>
<?php
}
}
}
}
add_action(\'admin_notices\', \'display_admin_notices\');