1

I'm trying to create notifications only when I deleted published posts, but it fire when I update any published post?

What I'm doing wrong! Thanks.

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

I tried to add if ( wp_is_post_revision( $post ) ) return; but nothing happen, How can i avoid this notification when I updating published post? It should fire only when I deleted the post! Any help please.

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

I returned the empty trash to 0 into the wp-config.php like so define('EMPTY_TRASH_DAYS', 0); that's because i don't need to keep them at all!

The original purpose that I need to delete any post permanently with the post attachments by using this action

add_action( 'before_delete_post', function( $id ) {
  $attachments = get_attached_media( '', $id );
  foreach ($attachments as $attachment) {
    wp_delete_attachment( $attachment->ID, 'true' );
  }
} );

My blog for Artists and they uploading more images, So I deleting the post with the attachments to save some space in my host storage

2

The hook before_delete_post fires before postmeta is deleted. That explains why it's not isolating the condition you want. Another part of the problem is you're trying to add postmeta while it is being deleted - it's not clear why you need published_send_once but you'll have to find another way to save that information than adding postmeta at the time all postmeta is deleted. Side note, posts of type "revision" cannot have the status "publish" - revisions always have a status of "inherit."

When a post is deleted, the hooks transition_post_status, post_updated, and save_post are all run. transition_post_status runs first of the three, so I'd suggest using it to trigger your notification:

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

Using this other hook will allow you to verify that your code only runs when a publish status post switches to trash status.

  • Thanks so much @WebElaine for your answer, but all my posts is deleted permanently not into the trash! So your suggestion will work with that? – Adham Mohamed Apr 1 at 18:17
  • I test it out but not working as i told you I'm deleting all posts permanently, So which hook work with that, Thank you! – Adham Mohamed Apr 1 at 18:36
  • How are you deleting the posts? As long as you're using a Core method (edit the post and hit trash, or bulk edit posts and hit trash, followed by emptying the trash) these hooks will run too. – WebElaine Apr 1 at 19:42
  • You still have not explained how you are deleting the posts themselves. Are you using code? Or using steps in wp-admin? Or editing the database directly? – WebElaine Apr 1 at 20:14
  • Yeah from wp-admin I hitting Delete permanently. – Adham Mohamed Apr 1 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.