[Resolved] How to send auto notifications when deleting a post via CRED form

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Tagged: 

This topic contains 30 replies, has 3 voices.

Last updated by jamesH-3 4 years, 11 months ago.

Assigned support staff: Shane.

Author
Posts
#288814

Hi Shane
I checked junk email boxes for both email addresses and there is nothing.
Thanks

#289266

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi James,

I found a plugin that might help a bit more than the code I provided earlier.

Could you please try using this plugin https://wordpress.org/plugins/email/

It is a lot more user friendly 🙂

#289394

Hi Shane

Although I was not comfortable with it (because the Email plugin seems to be relatively new with a rating lower than I would expect and I rather not use plugins if I don't have to).

Nevertheless, out of desperation and a lit bit of frustration, I disabled the code that you recommended me to use before and installed the Email plugin. It seems it still does not work.

Please note that the email I set up through CRED for for new coupon posts and through Gravity Forms for new subscriptions, work with no problems.

I am not sure if it is related but when you go Coupon Manager page and click on Delete This Coupon button, it does not refresh the page and the coupon looks like it is still active but it is in fact deleted (you see that it it has already been deleted once I refresh the page manually).

I think I first posted this issue on 26 February and after about 20 days it looks like I am still where I started.

I do understand and appreciate that you are doing the best you can but I would really appreciate it if you can speak to one of the developers and come up with a solution as soon as possible.

Please let me know if there is anything else I can do.

Thanks

#289703

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi James,

It seems that the coupons behave a bit differently from posts.

This looks like it would require a great deal of custom development.

I must apologize for not being more helpful with this.

However what I can do is to recommend that you contact our custom development team.

They should be able to get this up and running for you.

Please contact them here : hidden link

In the meantime is there anything else that I can assist you with ?

#289885

Hi Shane

I have to say this threat has been by far the most frustrating experience I have had with Toolset.

Just to recap, you first told me that the code “should?? work. And then you told me that it "seemed" to be an issue with my hosting provider (the hosting provider confirmed that it was not). Then you told me that it "may" have gone to my junk email. You then asked me to use a very new and untested email plugin, which did not work either. And now you are telling me it "seems" the coupons behave a bit differently from posts.

If the coupons are behaving differently from posts, my conclusion from this would be there seems to be a problem with CRED.

I am not really sure you know what is causing the problem and I feel like you are just brushing me off to your developers.

Unless you can tell me what the issue is and what is wrong with the code, my conclusion is that there must be a glitch with CRED and I am not prepared to pay extra for a CRED glitch to be fixed.

I would appreciate it if you can reallocate this threat to another support person or a supervisor and am not happy with the way this threat has been handled.

Thanks

#289954

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi James,

I know how frustrating it can be when things are not working as they are supposed to.

I consulted with one of my fellow support teammates on this matter in order to get this resolved for you.

I have attached a couple screenshots showing that the code I sent you is actually fully working as well as the plugin.

The next step for me to do is to manually implement it on your website for you.

In order for me to do this I will need to get your FTP details.

I will be enabling the private fields again so that you can provide this information.

#290235

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi James,

I tested the plugin with my email and it works when I delete a coupon.

Please see screenshot.

So the plugin is actually working.

All I did was to create a new email notification for deleting a coupon.

#291735

Hi Shaun
Unfortunately it is not working.

When you cancelled the Tesr coupon 2, I also received an email; however, this email was sent by the email I had created. (I have attached the screenshot. Please note this email was sent to “all authors?? so it did not work as it was supposed to)

I then changed the email I created to Draft and the kept the email you created. I put in the email_to box “administrator?? first and then I tried with my other email address but it did not send any emails. I tried this as admin and also logged in separately as a user and it did not work.

Did you check the code as well? How did you go with that?

Thanks

#291981

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi James,

I think i have found the issue, see screenshot.

You need to write out the email of the actual admin.

I placed my email in this field and it was able to send the email to my personal email.

Please try this and let me know.

#292119

Hi Shane

I had tried this before by using a different email address. I have not tried it with my admin email address and it did not work me. In line with your advice for my other thread, I also tried by deactivating all plugins that are not related to Toolset.

Even if it, if this plug in is not going to send emails to the roles (please refer to one of the very first emails that I sent and I want to send the emails to the email address of the logged in author and cc or bcc it to the admin) it is not going to work for me?

I think I have already asked a couple of times and not received an answer for: Have you checked the code that you had recommended and thought should work?

Thanks

#292350

Juan
Supporter

Timezone: Europe/Madrid (GMT+02:00)

Hi James

This is Juan here, lead developer on the Toolset team. Let me start by telling you that I am sorry this has taken so long to find a solution for your issue. I will try to help you with that 🙂

First, let me recap. You have a custom post type, "coupon", and you have added delete links created with CRED, so frontend users can delete coupons by clicking on them. You now want to send notifications to one (at least) email containing the deleted coupon ID, title and content.

Let's start by assuming that your server is capable of sending emails, since Gravity Forms can, and I assume you also can get notifications when a visitor leaves a new comment (if you stated so in the WordPress settings). This means that we should be able to use the wp_mail() function without any problem. The issue here would be then to know when we should use it, and feed it with content.

As you might now, CRED delete links can perform two ctions: adelete a post, or just trash it, based on the action attribute of the cred_delete_post_link shortcode. I assume that you want to execute this mailing action in any of those scenarios. Your request has illuminated a feature that we do not have inherently in CRED; an action to be executed when deleting or trashing a post using its links. We have created a feature request and will consider adding this in a future release.

But for now, we can try a workaround using the native actions that WordPress executes when trashing or deleting a post. This has a potential disadvantage: it will also be executed when trashing or deleting a post on the backend, by any admin or user with the capability to do so, but it should work and help you while we get the feature implemented.

We will not be using any third party plugin at all. We will use our own code, so please go back to the starting place once again.

When deleting a post, WordPress executes one action named "before_delete_post" that takes the deleted post ID as parameter. We will hook into it and add our own callback:

add_action( 'before_delete_post', 'prefix_send_email_cred_delete_coupon' );

When trashing a post, WordPress executes another action named "wp_trash_post" that also takes the trashed post ID as parameter. Note that this is not "trash_post", which was renamed some time ago (and now, "trash_post" is only executed when trashing regular WordPress posts, so it will not work on "coupon" post types):

add_action( 'wp_trash_post', 'prefix_send_email_cred_delete_coupon' );

Note that we are using the same callback, "prefix_send_email_cred_delete_coupon", because on both cases we are getting the affected post ID as parameter. Now, let's build this function and make it send mails. Please remember to update all the involved email addresses to real ones.

function prefix_send_email_cred_delete_coupon( $post_id ) {
	$deleted_post = get_post( $post_id );
	if ( $deleted_post->post_type == 'coupon' ) {
		// Set the intended recipient
		// Please remember to adjust test@test.com as needed
		$email_to = 'test@test.com';
		// Set the email subject
		// Will be something like "The coupon Whatever with ID 234 has been deleted"
		$email_subject = 'The coupon ' . $deleted_post->post_title . ' with ID ' . $post_id . ' has been deleted';
		// Set the email content
		// It will add the coupon content, feel free to adjust
		$email_content = $deleted_post->post_content;
		// Set the email headers
		// This will let you set a FROM value
		// Please remember to adjust the youremail@example.com value to your needs
		$email_headers = 'From: James <youremail@example.com>' . "\r\n";
		wp_mail( $email_to, $email_subject, $email_content, $email_headers );
	}
}

Pretty please let me know how it goes. If this does not seem to work, I will do some debugging on your site until we get this sorted out. And many thanks for the feature suggestion that we extracted from your query: I am sure others will also find it very useful.

Regards

#292612

Hi Juan

Thank you for your email

Your understanding of what I am trying to achieve is correct. Only one other thing that I might add is I want the email to go to the email address of the logged in user or logged in author and bcc'ed to the admin's email address.

I have tried the code you have suggested. I put in the functions.php of the child theme but unfortunately it has not worked (I have checked the junk box of my email account)

Thanks

#292632

Juan
Supporter

Timezone: Europe/Madrid (GMT+02:00)

Hi James

I have reviewed your site using the cPanel credentials that you provided. I have modified slightly the code, tested and I can confirm that this is working. Let me stress what was wrong:
* First, you added the callback function, but did not add the two actions, so it could not be executed at all 🙂
* Second, it seems that ·deleted->post_content is empty because your coupons do not have content at all. WordPress can not and will not send empty emails 🙂

So this is the final code in your theme's functions.php file

add_action( 'before_delete_post', 'prefix_send_email_cred_delete_coupon' );
add_action( 'wp_trash_post', 'prefix_send_email_cred_delete_coupon' );

function prefix_send_email_cred_delete_coupon( $post_id ) {
    $deleted_post = get_post( $post_id );
    if ( 
    	$deleted_post->post_type == 'coupon' 
    	&& is_user_logged_in()
    ) {
    	$current_user = wp_get_current_user();
        $email_to = $current_user->user_email;
        // Set the email subject
        // Will be something like "The coupon Whatever with ID 234 has been deleted"
        $email_subject = 'The coupon ' . $deleted_post->post_title . ' with ID ' . $post_id . ' has been deleted';
        // Set the email content
        // It will add the coupon content, feel free to adjust
        // Note that $deleted_post->content is empty, so if you use it, then wp_mail() will NOT send anything
        $email_content = 'The coupon ' . $deleted_post->post_title . ' with ID ' . $post_id . ' has been deleted';
        // Set the email headers
        // This will let you set a FROM value and a CC value
        // Please remember to adjust youremail@example.com and zafersatilmis@yahoo.com to your needs
        $email_headers = 'From: James <jameshillezaf@gmail.com>' . "\r\n";
        $email_headers = 'CC: Admin <zafersatilmis@yahoo.com>' . "\r\n";
        wp_mail( $email_to, $email_subject, $email_content, $email_headers );
    }
}

Please note that:
* This will send an email to the current logged in user
* This will send an email FROM James jamesXXXXXXXXcom
* This will CC the email to zaferXXXXXXXXXXcom
* Both the email subject and body will be "The coupon XXXX' with ID YYYY has been deleted"

Please check that you are receiving them, I tested with email account of mine and it worked perfectly 🙂

Cheers!

#299697

Hi

I apologise for the delay in getting back to you.

Thank you so much for your help. It is working almost perfectly.

The only issue I have got now about the email notification is the $email_headers:

- emails are sent from "WordPress wordpress@turkeytravelhub.com"

I have also noticed that the same happens with the Add New Coupons CRED form as well when I create new coupons.

Thanks

#299826

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi James,

I'm happy to see that Juan was able to assist you with getting this working for you.

The reason the mails are being sent from "WordPress wordpress@turkeytravelhub.com" is that by default WordPress uses wordpress@your-domain.com as the email address and WordPress as From name.

In order to change this would require that you modify one of the wordpress core files since this is neither theme or plugin related.

The first way in which you can change this is by locating these lines within you pluggable.php file.
This file is located within you wp-includes folder after logging into your website using ftp.

Between the lines 362 - 379 you will see the following.

// From email and name
	// If we don't have a name from the input headers
	if ( !isset( $from_name ) )
		$from_name = 'WordPress';

	/* If we don't have an email from the input headers default to wordpress@$sitename
	 * Some hosts will block outgoing mail from this address if it doesn't exist but
	 * there's no easy alternative. Defaulting to admin_email might appear to be another
	 * option but some hosts may refuse to relay mail from an unknown domain. See
	 * https://core.trac.wordpress.org/ticket/5007.
	 */

	if ( !isset( $from_email ) ) {
		// Get the site domain and get rid of www.
		$sitename = strtolower( $_SERVER['SERVER_NAME'] );
		if ( substr( $sitename, 0, 4 ) == 'www.' ) {
			$sitename = substr( $sitename, 4 );
		}

		$from_email = 'wordpress@' . $sitename;
	}

What you need to do is change the $from_name = 'Wordpress' to whatever you want. Replacing the text 'Wordpress'.

For the $from_email = 'wordpress@' .$sitename; replacing the text with whatever you want it to be.

If you are not comfortable doing this, there is an easier alternative method by simply installing this plugin here : https://wordpress.org/plugins/wp-change-default-email/

It functions the same but gives you an interface to do it. Once you have installed the plugin go to Settings -> WP Change default Email and just fill in the appropriate information in the fields.

I have tested this plugin and it works as well.

Please let me know if you are having any issues after trying.