我正在尝试用error类替换默认的反垃圾消息。admin_notices按预期做了所有事情,但是即使我在bulk_post_updated_messages过滤器中返回空消息数组,我仍然可以看到注意事项,但是带有编辑链接的内容是空的。

根据wp-admin/edit.php的说法,消息是基于$bulk_counts生成的,但是bulk_post_updated_messages过滤器只返回$bulk_messages,即使我更改了这个数组,也什么也没有发生。如何访问$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[] = '' . __( 'Undo' ) . '';
}
if ( 'untrashed' === $message && isset( $_REQUEST['ids'] ) ) {
$ids = explode( ',', $_REQUEST['ids'] );
if ( 1 === count( $ids ) && current_user_can( 'edit_post', $ids[0] ) ) {
$messages[] = sprintf(
'%2$s',
esc_url( get_edit_post_link( $ids[0] ) ),
esc_html( get_post_type_object( get_post_type( $ids[0] ) )->labels->edit_item )
);
}
}
}发布于 2022-05-10 17:17:13
如前所述,批量消息输出基于$bulk_counts变量,不能在admin_notices钩子中进行编辑。这很奇怪,因为他们在wp-admin/edit.php中有自己的html包装,没有任何操作和过滤器。所以我更深入地研究WP核心。
$bulk_counts变量在该行的wp-admin/edit.php中生成:
$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,
);正如我们所看到的,untrashed键从查询字符串中获取。如果我们清拆单个记录,则此代码中wp-admin/post.php中的操作处理:
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;没有过滤器和操作,但是在最后我们得到了wp_redirect()函数,它有一个过滤器wp_redirect。
要禁用消息,我们必须删除untrashed=1参数和带有当前post id的可选ids=***。在我的例子中,我在会话中编写一个标志,并在wp_redirect中检查这个标志
重定向后
'untrashed' => isset( $_REQUEST['untrashed'] ) ? absint( $_REQUEST['untrashed'] ) : 0;部件返回0,根据我最初发布的代码,消息将消失。
这是我找到的唯一解决办法。欢迎任何其他解决办法。
https://wordpress.stackexchange.com/questions/405560
复制相似问题