帖子删除后添加自定义帖子通知

时间:2016-10-22 作者:dingo_d

我试图在帖子从垃圾桶中删除后添加一个自定义信息通知,但我没有任何运气

add_action( \'delete_post\', \'show_admin_notice\' );
/**
 * Show admin notice after post delete
 *
 * @since 1.0.0.
 */
function show_admin_notice(){
    add_action( \'admin_notices\', \'show_post_order_info\' );
}

/**
 * Display notice when user deletes the post
 *
 * When user deletes the post, show the notice for the user
 * to go and refresh the post order.
 *
 * @since 1.0.0
 */
function show_post_order_info() {
    $screen = get_current_screen();

    if ( $screen->id === \'edit-post\' ) {
        ?>
        <div class="notice notice-info is-dismissible">
            <p><?php echo esc_html__( \'Please update the \', \'nwl\' ) . \'<a href="\' . esc_url( admin_url( \'edit.php?page=post-order\' ) ). \'">\' . esc_html__( \'post order settings\', \'nwl\' ) . \'</a>\' . esc_html__( \' so that the posts would be correctly ordered on the front pages.\', \'nwl\' ); ?></p>
        </div>
        <?php
    }
}
很明显,我在这里遗漏了一些东西,但我无法在谷歌上找到什么。

如果我使用admin_notices 胡克,我会在我的帖子管理页面上看到通知

2 个回复
最合适的回答,由SO网友:birgire 整理而成

检查批量计数我们可以检查批量计数,查看是否有帖子被删除:

add_filter( \'bulk_post_updated_messages\', function( $bulk_messages, $bulk_counts )
{
    // Check the bulk counts for \'deleted\' and add notice if it\'s gt 0
    if( isset( $bulk_counts[\'deleted\'] ) && $bulk_counts[\'deleted\'] > 0 )
        add_filter( \'admin_notices\', \'wpse_243594_notice\' );

    return $bulk_messages;
}, 10, 2 );
其中,我们使用自定义通知将回调定义为:

function wpse_243594_notice()
{
    printf( 
        \'<div class="notice notice-info is-dismissible">%s</div>\',
        esc_html__( \'Hello World!\', \'domain\' )
    );
}
批量计数包含更新、锁定、丢弃和未清除的进一步信息。

输出示例

custom notice on post delete

希望您可以根据您的需要进一步调整此选项!

SO网友:T.Todua

我认为这将解决:

if(isset($_GET[\'post_status\']) && $_GET[\'post_status\']==\'trash\'){
     add_action( \'admin_notices\', \'show_post_order_info\' );
}