Add custom post notice after post delete

I am trying to add a custom info notice after the post has been deleted from the trash, but I’m not having any luck with it

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

I’m clearly missing something here, but I couldn’t find out what on the google.

If I just use the admin_notices hook, I’ll get the notice shown always on my posts admin page

2 Answers
2

Checking the bulk counts

We can check the bulk counts, to see if any post was deleted:

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

where we define the callback with our custom notice as:

function wpse_243594_notice()
{
    printf( 
        '<div class="notice notice-info is-dismissible">%s</div>',
        esc_html__( 'Hello World!', 'domain' )
    );
}

The bulk counts contains further info for updated, locked, trashed and untrashed.

Output example

custom notice on post delete

Hope you can adjust this further to your needs!

Leave a Comment