有些时候我们需要清空回收站,但是针对多用户同等权限情况下,任何一个人使用清空,就有可能把所有人加入回收站的内容全部删除,这样可能会出现别的作者误删或者想要恢复的文章直接消失了。
想要解决这个问题可能就需要用到一个过滤器,名为:pre_delete_post
这个过滤器的作用也是非常简单,也就是在删除帖子之前进行一下判断,如果你想要额外的判断,比如文章开头说道的,想要用户清空回收站时,仅仅清空属于自己的文章,那么可以尝试下面的代码:
add_filter( 'pre_delete_post', 'wpse_224246_pre_delete_post', 10, 3 ); function wpse_224246_pre_delete_post( $delete, $post, $force_delete ) { //* Escape early if post isn't already trashed if( 'trash' !== $post->post_status ) { return $delete; } //* Go ahead with deleting the post if the current user is the post author if( get_current_user_id() === $post->post_author ) { return $delete; } //* Returning any other value besides null will short circuit the deletion return true; }
代码方面来自: https://wordpress.stackexchange.com/questions/224246/how-to-cancel-the-trash-action-inside-wp-trash-post
如果你了解更多,可以尝试通过这个过滤器在更多方面使用,比如删除帖子的时候删除关联附件或者文章内容中关联的某些信息等等。