Remove all messages, when untrash a post

I'm trying to replace default untrash message to my own with error class. admin_notices do everything as expected, but even if I return empty messages array in bulk_post_updated_messages filter, I'm still see notice, but with empty content with Edit link.

According to wp-admin/edit.php, messages generating based on $bulk_counts, but bulk_post_updated_messages filter returns $bulk_messages only and even if I change this array, nothing happened. How to access $bulk_counts?

$bulk_messages = apply_filters( 'bulk_post_updated_messages', $bulk_messages, $bulk_counts );
$bulk_counts   = array_filter( $bulk_counts );

...

// If we have a bulk message to issue:
$messages = array();
foreach ( $bulk_counts as $message = $count ) {
    if ( isset( $bulk_messages[ $post_type ][ $message ] ) ) {
        $messages[] = sprintf( $bulk_messages[ $post_type ][ $message ], number_format_i18n( $count ) );
    } elseif ( isset( $bulk_messages['post'][ $message ] ) ) {
        $messages[] = sprintf( $bulk_messages['post'][ $message ], number_format_i18n( $count ) );
    }

    if ( 'trashed' === $message  isset( $_REQUEST['ids'] ) ) {
        $ids        = preg_replace( '/[^0-9,]/', '', $_REQUEST['ids'] );
        $messages[] = 'a href=' . esc_url( wp_nonce_url( edit.php?post_type=$post_typedoaction=undoaction=untrashids=$ids, 'bulk-posts' ) ) . '' . __( 'Undo' ) . '/a';
    }

    if ( 'untrashed' === $message  isset( $_REQUEST['ids'] ) ) {
        $ids = explode( ',', $_REQUEST['ids'] );

        if ( 1 === count( $ids )  current_user_can( 'edit_post', $ids[0] ) ) {
            $messages[] = sprintf(
                'a href=%1$s%2$s/a',
                esc_url( get_edit_post_link( $ids[0] ) ),
                esc_html( get_post_type_object( get_post_type( $ids[0] ) )-labels-edit_item )
            );
        }
    }
}

Topic plugin-development plugins Wordpress

Category Web


As stated before, bulk messages output based on $bulk_counts variable and can't be edited in admin_notices hook. This is strange, because they has their own html wrapping in wp-admin/edit.php without any actions and filters. So I dig deeper in WP core.

$bulk_counts variable generating in the wp-admin/edit.php in this line:

$bulk_counts = array(
    'updated'   => isset( $_REQUEST['updated'] ) ? absint( $_REQUEST['updated'] ) : 0,
    'locked'    => isset( $_REQUEST['locked'] ) ? absint( $_REQUEST['locked'] ) : 0,
    'deleted'   => isset( $_REQUEST['deleted'] ) ? absint( $_REQUEST['deleted'] ) : 0,
    'trashed'   => isset( $_REQUEST['trashed'] ) ? absint( $_REQUEST['trashed'] ) : 0,
    'untrashed' => isset( $_REQUEST['untrashed'] ) ? absint( $_REQUEST['untrashed'] ) : 0,
);

As we can see, untrashed key gets from the query string. If we untrash single record, this action processing in wp-admin/post.php in this code:

case 'untrash':
        check_admin_referer( 'untrash-post_' . $post_id );

        if ( ! $post ) {
            wp_die( __( 'The item you are trying to restore from the Trash no longer exists.' ) );
        }

        if ( ! $post_type_object ) {
            wp_die( __( 'Invalid post type.' ) );
        }

        if ( ! current_user_can( 'delete_post', $post_id ) ) {
            wp_die( __( 'Sorry, you are not allowed to restore this item from the Trash.' ) );
        }

        if ( ! wp_untrash_post( $post_id ) ) {
            wp_die( __( 'Error in restoring the item from Trash.' ) );
        }

        $sendback = add_query_arg(
            array(
                'untrashed' => 1,
                'ids'       => $post_id,
            ),
            $sendback
        );
        wp_redirect( $sendback );
        exit;

no filters and actions again, but at the end we got wp_redirect() function, which has a filter wp_redirect.

To disable messages, we must remove untrashed=1 argument and optional ids=*** with current post id. In my case I'm writing a flag in session and check this flag in wp_redirect

After redirection

'untrashed' => isset( $_REQUEST['untrashed'] ) ? absint( $_REQUEST['untrashed'] ) : 0;

part returns 0 and according to code in my initial post messages will disappear.

At this time this is the only solution which I've found. Any other solutions are welcomes.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.