im creating an error handler for a custom post type.
add_filter( 'wp_insert_post_data' , 'filter_post_data' , '99', 2 );
function filter_post_data( $data , $postarr )
{
//error handling
if($data['post_type'] == 'post_annunci')
{
if($postarr['annuncio_prezzo'] == '' || !is_numeric($postarr['annuncio_prezzo']))
{
$errors .= 'Annuncio non salvato, devi inserire un prezzo con valori numerici.</br>prezzo = '.$postarr['annuncio_prezzo'];
update_option('my_admin_errors', $errors);
$data['post_status'] = 'draft';
}
}
return $data;
}
add_action( 'admin_notices', 'admin_error_handler_annunci' );
function admin_error_handler_annunci() {
$errors = get_option('my_admin_errors');
if($errors)
echo '<div class="error"><p>' . $errors . '</p></div>';
}
// Clear any errors
add_action( 'admin_footer', 'clear_error_handler_annunci' );
function clear_error_handler_annunci() {
update_option('my_admin_errors', false);
}
This works perfectly but i get a “published post” notice that i want to remove.
Is there some way to get rid of that message?
I seen some sort of notice remover like this script, but works only for update notices.
function hideUpdateNag() {
remove_action( 'admin_notices', 'update_nag', 3 );
}
add_action('admin_menu','hideUpdateNag');
Yes. It is possible with the filter
post_updated_messages
.In this example, an empty array is returned. Resulting in no messages at all.
This can be fine tuned using the sample values bellow, which are the contents of
$messages
before the filter.