【问题标题】:remove published post notice while error handled处理错误时删除已发布的发布通知
【发布时间】:2013-04-15 13:07:09
【问题描述】:

我正在为自定义帖子类型创建错误处理程序。

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);
}

这很好用,但我收到一条“已发布的帖子”通知,我想删除它。

有什么方法可以消除该消息吗? 我看到了类似这个脚本的某种通知删除器,但仅适用于更新通知。

function hideUpdateNag() {
    remove_action( 'admin_notices', 'update_nag', 3 );
}
add_action('admin_menu','hideUpdateNag');

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案1】:

    是的。可以使用过滤器post_updated_messages

    add_filter( 'post_updated_messages', 'remove_all_messages_so_16015959' );
    
    function remove_all_messages_so_16015959( $messages )
    {
        return array();
    }
    

    在本例中,返回一个空数组。结果根本没有消息。
    这可以使用下面的示例值进行微调,这些值是过滤器之前$messages 的内容。

    Array
    (
        [post] => Array
            (
                [0] => 
                [1] => Post updated. View post
                [2] => Custom field updated.
                [3] => Custom field deleted.
                [4] => Post updated.
                [5] => 
                [6] => Post published. View post
                [7] => Post saved.
                [8] => Post submitted. Preview post
                [9] => Post scheduled for: Jan 12, 2013 @ 19:03. Preview post
                [10] => Post draft updated. Preview post
            )
    
        [page] => Array
            (
                [0] => 
                [1] => Page updated. View page
                [2] => Custom field updated.
                [3] => Custom field deleted.
                [4] => Page updated.
                [5] => 
                [6] => Page published. View page
                [7] => Page saved.
                [8] => Page submitted. Preview page
                [9] => Page scheduled for: Jan 12, 2013 @ 19:03. Preview page
                [10] => Page draft updated. Preview page
            )
    
        [attachment] => Array
            (
                [1] => Media attachment updated.
                [2] => Media attachment updated.
                [3] => Media attachment updated.
                [4] => Media attachment updated.
                [5] => Media attachment updated.
                [6] => Media attachment updated.
                [7] => Media attachment updated.
                [8] => Media attachment updated.
                [9] => Media attachment updated.
                [10] => Media attachment updated.
            )
    
    )
    

    【讨论】:

    • 我的来源是来源,Luke,它已经链接在我的答案中;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    相关资源
    最近更新 更多