Skip Navigation

[Resolved] Notification when custom post deleted

This thread is resolved. Here is a description of the problem and solution.

Problem:

I'm wondering if there is a way for admin to get notified when a custom post has been deleted by the user.

Solution:

There isn't such kind of feature within shortcode [cred_delete_post_link], see our document:

https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_delete_post_link

In your case, you can consider custom codes, for example:

https://toolset.com/forums/topic/notification-when-custom-post-deleted/#post-1371833

Relevant Documentation:

https://developer.wordpress.org/reference/hooks/wp_trash_post/

https://developer.wordpress.org/reference/functions/wp_mail/

This support ticket is created 5 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 13 replies, has 2 voices.

Last updated by FelipeP5703 5 years ago.

Assisted by: Luo Yang.

Author
Posts
#1371755

Tell us what you are trying to do?

I'm wondering if there is a way for admin to get notified when a custom post has been deleted by the user. I have a View that gives the option for the user to delete his/her custom post with the following code:

[cred_delete_post_link action='trash' text='Deletar' message_after='Anúncio deletado!' message='Tem certeza que deseja deletar? Não poderá recuperar o perfil e terá que criar um novo!' message_show='1' class='cred-refresh-after-delete dropdown-item']

Now is there anyway to include a notification so admin receives an email about the deletion of that custom post? What is an easy solution?

Thanks!

#1371833

Hello,

There isn't such kind of feature within shortcode [cred_delete_post_link], see our document:
https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_delete_post_link

In your case, you can consider custom codes, for example:
When user click the delete link "Deletar", the post will be trashed, and you can use WordPress action hook "wp_trash_post" to trigger a PHP function, in this PHP function send the email notification to admin user.

More help:
https://developer.wordpress.org/reference/hooks/wp_trash_post/
Fires before a post is sent to the trash.
https://developer.wordpress.org/reference/functions/wp_mail/
Send mail

#1372731

Ok before I go and add the function to my site, I just want to make sure the code is correct, could you please check this code? Btw, I'm not a programmer, so I'm copying from the internet and trying my best.

function action_wp_trash_post( $post_id ) { 
   $post = get_post( $post_id );
   $title = get_post($post_title);

$to = get_option('admin_email');
$subject = '$title was deleted';
$body = 'This email is to let you know that $title was deleted.';
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $message, '', array( '' ) );

}; 
add_action( 'wp_trash_post', 'action_wp_trash_post', 10, 1 ); 
#1372777

For example:

function prefix_send_email_cred_delete_post( $post_id ) {
    $deleted_post = get_post( $post_id );
    // If it is a my-cpt post
    if ( $deleted_post->post_type == 'my-cpt' ) {
        // Set the email address
        $email_to = 'test@test.com';
        // Set the email subject
        $email_subject = $title . ' was deleted';
        // Set the email content
        $content = 'This email is to let you know that' . $title . ' was deleted.';
        // Set the email headers
        $headers = array('Content-Type: text/html; charset=UTF-8');
        wp_mail( $email_to, $email_subject, $content , $headers );
    }
}
add_action( 'wp_trash_post', 'prefix_send_email_cred_delete_post' );

Please replace "my-cpt" with your custom post type slug.

#1373045

Luo,

Thanks for the code. Although it does send the email once the custom post is deleted. The title is not working, so I tried adding this code to get the title but it is not working:

$title = isset( $post->post_title ) ? $post->post_title : '';

The whole function is below:

//Function to let Admin know when a post is deleted
function prefix_send_email_cred_delete_post( $post_id ) {
    $deleted_post = get_post( $post_id );
	$title = isset( $post->post_title ) ? $post->post_title : '';
    // If it is a my-cpt post
    if ( $deleted_post->post_type == 'anuncio' ) {
        // Set the email address
        $email_to = 'test@test.com';
        // Set the email subject
        $email_subject = $title . ' was deleted';
        // Set the email content
        $content = 'This email is to let you know that' . $title . ' was deleted.';
        // Set the email headers
        $headers = array('Content-Type: text/html; charset=UTF-8');
        wp_mail( $email_to, $email_subject, $content , $headers );
    }
}
#1373069

Please try to replace this line from:

$title = isset( $post->post_title ) ? $post->post_title : '';

To:

$title = $deleted_post->post_title;

And test again

#1373305

That worked great! Thank you!

Sorry for abusing your generosity but I tried to use this function in order to also let the user know the post was deleted but now the function does not working.

I added this to get the user's email:

$useremail = $current_user->user_email;

and this to send it to the customer:

$email_to = $useremail;

and this so I can also receive the email without the customer knowing by using BC:

$headers[] = 'From: My site <test@test.com>';
		$headers[] = 'Bc: test@test.com';

Here is the whole code:

//Function to let Admin know when a post is deleted
function prefix_send_email_cred_delete_post( $post_id ) {
    $deleted_post = get_post( $post_id );
	$title = $deleted_post->post_title;
	$useremail = $current_user->user_email;
    // If it is a my-cpt post
    if ( $deleted_post->post_type == 'anuncio' ) {
        // Set the email address
        $email_to = $useremail;
        // Set the email subject
        $email_subject = 'O anúncio da ' . $title . ' foi deletado';
        // Set the email content
        $content = 'Esse email é só para te avisar que o anúncio de ' . $title . ' foi deletado.';
        // Set the email headers
        $headers[] = 'From: My Site <test@test.com>';
        $headers[] = 'Bc: test@test.com';
		
        wp_mail( $email_to, $email_subject, $content , $headers );
    }
}
add_action( 'wp_trash_post', 'prefix_send_email_cred_delete_post' );
#1373551

For the new issue: let the user know the post was deleted but now the function does not working.

You will try another WordPress action hook "pre_delete_post", for example, modify your PHP codes, replace this line from:

add_action( 'wp_trash_post', 'prefix_send_email_cred_delete_post' );

With:

add_action( 'before_delete_post', 'prefix_send_email_cred_delete_post' );

More help:
https://developer.wordpress.org/reference/hooks/before_delete_post/

#1373867

I replaced the code you mentioned but neither the admin nor the user received the email notice. Do I need to create another function just for the "before_delete_post" in order to get the user's email address? Or do I have to modify the current function in any way?

I'm a bit lost :/

#1374399

Since it is a custom PHP codes problem, please provide a test site with the same problem, also point out the problem page URL, where and how can I edit your custom PHP codes, thanks

#1375257

There is some misunderstandings:
1) Please edit the post view "Meus Anúncios Publicados":
hidden link
in section "Loop item in Meus Anúncios Test", you are using below codes:

[cred_delete_post_link action='trash' text='Deletar' message_after='Anúncio deletado!' message='Tem certeza que deseja deletar? Não poderá recuperar o perfil e terá que criar um novo!' message_show='1' class='cred-refresh-after-delete dropdown-item']

The shortcode attribute is action='trash', so the post will be trashed, it won't be deleted, so you just need to use the action hook "wp_trash_post"

2) Please edit your theme file "functions.php", add a line just above this line:

$useremail = $current_user->user_email;

For example:

$current_user = wp_get_current_user(); //add this line
$useremail = $current_user->user_email;

More help:
https://developer.wordpress.org/reference/functions/wp_get_current_user/

#1375411

Almost there. The User is getting the notification email but admin is not... so the headers[ ] BC is not working. How do I fix this, please?

#1376047

There isn't "Bc" parameter, please check the document I mentioned above:
https://developer.wordpress.org/reference/functions/wp_mail/#using-headers-to-set-from-cc-and-bcc-parameters

Section "Using $headers To Set “From:”, “Cc:” and “Bcc:” Parameters".

You can to modify "Bc" as "Bcc", and test again.

#1376217

I overlooked the "c", I'm sorry! It's working fine now! You are the man! My issue is resolved now. Thank you!