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 = '';
$image = '.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 = '';
$image = '.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
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/submission-rules';
$image = 'https://www.cgartzone/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/submission-rules';
$image = 'https://www.cgartzone/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
Share Improve this question edited Apr 1, 2019 at 20:11 Adham Mohamed asked Mar 31, 2019 at 18:10 Adham MohamedAdham Mohamed 1853 silver badges16 bronze badges1 Answer
Reset to default 2The 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/submission-rules';
$image = 'https://www.cgartzone/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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745638439a4637539.html
评论列表(0条)