“删除帖子前”操作在帖子更新时触发吗?

时间:2019-03-31 作者:Adham Mohamed

我试图只在删除已发布的帖子时创建通知,但当我更新任何已发布的帖子时会触发通知?

我做错了什么!谢谢

function deleted_published_post_notify_author( $post ) {

    global $post;

    $send_published = get_post_meta( $post->ID, \'published_send_once\', true );
    if ( ! empty( $send_published ) ) return;

    if( current_user_can(\'administrator\')) {

        if( get_post_status($post) == \'publish\' ) {

        $post = get_post($post->ID);
        $author = $post->post_author;
        $author_name = get_the_author_meta( \'display_name\', $author );
        $link =  \'https://www.cgartzone.com/submission-rules\';
        $image = \'https://www.cgartzone.com/wp-content/themes/hitmag-child/assets/images/sad-icon-01.svg\';
        $title = \'<strong>\'.$author_name.\'</strong> \' . __(\'We are sorry to inform you that your artwork has been deleted. We recommended you to re-read our rules or contact us for more informations\', \'dw-notifications\'). \' <strong>\'.$post->post_title.\'</strong>\';
        $notif = array(\'title\'=>$title, \'link\' => $link, \'image\' => $image);

        update_post_meta( $post->ID, \'published_send_once\', \'1\' );

        dwnotif_add_notification($author, $notif);
        }
    }
}
add_action( \'before_delete_post\', \'deleted_published_post_notify_author\' );

Edit 1

我试图添加if ( wp_is_post_revision( $post ) ) return; 但什么都没有发生,当我更新发布的帖子时,如何避免此通知?它应该只有在我删除帖子的时候才会开火!请帮忙。

function deleted_published_post_notify_author( $post ) {

    global $post;

    $send_published = get_post_meta( $post->ID, \'published_send_once\', true );
    if ( ! empty( $send_published ) ) return;

    if( current_user_can(\'administrator\')) {

        if( get_post_status($post) == \'publish\' ) {

                    if ( wp_is_post_revision( $post ) )
                       return;

        $post = get_post($post->ID);
        $author = $post->post_author;
        $author_name = get_the_author_meta( \'display_name\', $author );
        $link =  \'https://www.cgartzone.com/submission-rules\';
        $image = \'https://www.cgartzone.com/wp-content/themes/hitmag-child/assets/images/sad-icon-01.svg\';
        $title = \'<strong>\'.$author_name.\'</strong> \' . __(\'We are sorry to inform you that your artwork has been deleted. We recommended you to re-read our rules or contact us for more informations\', \'dw-notifications\'). \' <strong>\'.$post->post_title.\'</strong>\';
        $notif = array(\'title\'=>$title, \'link\' => $link, \'image\' => $image);
        update_post_meta( $post->ID, \'published_send_once\', \'1\' );
        dwnotif_add_notification($author, $notif);
        }
    }
}
add_action( \'before_delete_post\', \'deleted_published_post_notify_author\' );

Edit 2

我将空垃圾桶返回到wp配置中的0。像这样的phpdefine(\'EMPTY_TRASH_DAYS\', 0); 那是因为我根本不需要保存它们!

我需要使用此操作永久删除带有帖子附件的任何帖子的原始目的

add_action( \'before_delete_post\', function( $id ) {
  $attachments = get_attached_media( \'\', $id );
  foreach ($attachments as $attachment) {
    wp_delete_attachment( $attachment->ID, \'true\' );
  }
} );
我的艺术家博客,他们上传了更多图片,所以我删除了带有附件的帖子,以节省主机存储空间

1 个回复
SO网友:WebElaine

钩子before_delete_post 在删除Posteta之前激发。这就解释了为什么它不能隔离你想要的条件。问题的另一部分是您试图在删除Posteta时添加它-不清楚您为什么需要published_send_once 但您必须找到另一种方法来保存该信息,而不是在删除所有Posteta时添加Posteta。请注意,“修订”类型的帖子不能具有“发布”状态-修订的状态始终为“继承”

删除帖子时,钩子transition_post_status, post_updated, 和save_post 都在运行。transition_post_status 运行三个选项中的第一个,因此我建议使用它来触发通知:

// hook to transition_post_status
add_action(\'transition_post_status\', \'wpse_test\', 10, 3);
// the hook gives you the new status, old status, and post object
function wpse_test($new_status, $old_status, $post) {
    // only run when the status changes from publish to trash
    // you might also want to only run when the post_type is attachment
    if($new_status == \'trash\' && $old_status == \'publish\') {
        // verify current user is an Administrator - by capability, not role
        if(current_user_can(\'update_core\')) {
            // send notification
            $author = $post->post_author;
            $author_name = get_the_author_meta( \'display_name\', $author );
            $link =  \'https://www.cgartzone.com/submission-rules\';
            $image = \'https://www.cgartzone.com/wp-content/themes/hitmag-child/assets/images/sad-icon-01.svg\';
            $title = \'<strong>\'.$author_name.\'</strong> \' . __(\'We are sorry to inform you that your artwork has been deleted. We recommended you to re-read our rules or contact us for more informations\', \'dw-notifications\'). \' <strong>\'.$post->post_title.\'</strong>\';
            $notif = array(\'title\'=>$title, \'link\' => $link, \'image\' => $image);
            dwnotif_add_notification($author, $notif);
        }
    }
}
使用另一个钩子将允许您验证代码仅在publish 状态post切换到trash 地位

相关推荐

PHP:对REST请求进行身份验证?

我确实使用REST api为正在开发的插件创建了一些自定义端点。它工作得很好,但现在我想保护这些请求:我不需要外部用户(EDIT: 我指的是远程请求),以便能够进行查询。但我只找到有关javascript身份验证的文档(REST API Handbook).如何使用PHP实现身份验证(此处为WP 5.1.1)?谢谢