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