Support » Plugin: Notification - Custom Notifications and Alerts for WordPress » Double Notificaton email on ‘Post Update’

  • Resolved vkj1215

    (@vkj1215)


    I’m using this plugin for notification on post update. Each time the post is updated 2 emails are being received mostly at the same time ( very rarely relayed by 1-2 mins). Using the latest version or wordpress and plugin (5.3.2). Please help.

    • This topic was modified 3 years, 6 months ago by vkj1215.
Viewing 15 replies - 1 through 15 (of 21 total)
  • Plugin Author Kuba Mikita

    (@kubitomakita)

    Hmm, and you have only on notification added with single recipient?

    Thread Starter vkj1215

    (@vkj1215)

    Yes, I’m testing only for 1 recipient

    Plugin Author Kuba Mikita

    (@kubitomakita)

    I’m pretty sure this is a 3rd party plugin conflict. Do you use many plugins which saves the data in the post?

    Are you able to test by disabling all other plugins and updating the post?

    Plugin Author Kuba Mikita

    (@kubitomakita)

    Closing due to inactivity. If you do have any other input, please let me know.

    I’m also having this problem. I have two notifications, one is supposed to be triggered when post (CPT named “Trip”) is sent for review, but nothing is sent. I changed it to trigger on “Trip added” and that results in two emails.

    I have another notification set up to trigger on “Trip published” and that works fine – only one email is sent.

    Plugin Author Kuba Mikita

    (@kubitomakita)

    @jodzeee isn’t the CPT added programmatically? Or you create this post via wp-admin interface?

    I’m not sure I understand your question. The CPT is coded in the theme. The post is created on the front end by a user (a member who must be logged in).

    Plugin Author Kuba Mikita

    (@kubitomakita)

    And this frontend creation is a problem here. Do you use any plugin to do this or this is a custom coded thing?

    It’s custom coded.

    Plugin Author Kuba Mikita

    (@kubitomakita)

    Well, then we have a problem with actions probably. It all depends on how you do it. Can you share the code how the form is handled?

    Hey Kuba,

    I helped Jodi with the site in question. We used ACF for the form:

    
    $args = array(
    	'id'                    => 'trip-submission',
    	'post_id'               => 'new_post',
    	'new_post'              => array(
    		'post_type'   => 'trip',
    		'post_status' => 'draft' // Draft now, so no notification is sent. When post is updated in mnrovers-plugin it changes to Pending, so Notifications plugin triggers an email.
    	),
    	'field_groups'          => array( 'group_5a21b9c8ee2d0' ),
    	'fields'                => false,
    	'post_title'            => false,
    	'post_content'          => false,
    	'submit_value'          => __( 'Submit Trip', 'mnrovers'),
    	'return'                => get_permalink( 11923 ),
    	'updated_message'       => __( 'Thanks! Your trip has been submitted', 'mnrovers'),
    	'html_updated_message'	=> '<div id="message" class="callout">%s</div>',
    );
    acf_form( $args );
    

    Hmmm. This is the post_status change:

    
    add_action( 'acf/save_post', array( $this, 'update_trip_post_data' ), 20 );
    /**
     * Update the post title with the activity/location/state name.
     * Set trip year as custom taxonomy term so it's easier to query by date later on.
     */
    function update_trip_post_data( $post_id ){
    
    	if ( 'trip' !== get_post_type( $post_id ) ) {
    		return;
    	}
    
    	// Set the post data
    	$data = array(
    		'ID' => $post_id,
    	);
    
    	/**
    	 * If post isn't published, force it to remain pending.
    	 * This triggers the email Notification each time!
    	 */
    	if ( 'publish' !== get_post_status( $post_id ) ) {
    		$data['post_status'] = 'pending';
    	}
    
    	// "Skiing in the Moonlight - Mountain Creek - New Jersey"
    	$post_title = mnr_get_trip_title( $post_id );
    	if ( ! empty( $post_title ) ) {
    		$data['post_title'] = $post_title;
    	}
    
    	$post_content = get_post_meta( $post_id, 'trip_description', true );
    	if ( ! empty( $post_content ) ) {
    		$data['post_content'] = $post_content;
    	}
    
    	// Remove the hook to avoid infinite loop.
    	// remove_action( 'save_post_trip', array( $this, 'update_trip_post_data' ), 20 );
    	remove_action( 'acf/save_post',  array( $this, 'update_trip_post_data' ), 20 );
    
    	// Update the post
    	wp_update_post( $data );
    
    	$start_date = get_post_meta( $post_id, 'trip_start_date', true );
    	if ( $start_date ) {
    		$timestamp = strtotime( $start_date );
    		$year      = date( 'Y', $timestamp );
    		wp_set_object_terms( $post_id, (string) $year, 'trip_year', false );
    	}
    
    	// Add the hook back
    	// add_action( 'save_post_trip', array( $this, 'update_trip_post_data' ), 20 );
    	add_action( 'acf/save_post',  array( $this, 'update_trip_post_data' ), 20 );
    
    }
    
    Plugin Author Kuba Mikita

    (@kubitomakita)

    Hi @jivedig, aha! The custom workflow is causing this.

    I’d suggest you create your own action after the form is saved and hook it into the “Trip sent for review” trigger.

    How? Add this after all your custom data handling:

    [...]
    
    // Add the hook back
    // add_action( 'save_post_trip', array( $this, 'update_trip_post_data' ), 20 );
    add_action( 'acf/save_post',  array( $this, 'update_trip_post_data' ), 20 );
    
    do_action( 'trip_saved_notification', 'pending', 'old_status', get_post( $post_id ) );

    And then add it to the correct trigger:

    add_action( 'notification/trigger/registered', function( $trigger ) {
    
    	// Check if registered Trigger is the one we need.
    	// Adjust the "trip" if this is not your CPT slug.
    	if ( 'wordpress/trip/pending' !== $trigger->get_slug() ) {
    		return;
    	}
    
    	// Add your action.
    	$trigger->add_action( 'trip_saved_notification', 10, 3 );
    
    } );

    This should do the job!

    K thanks!

    Do I use old_status or put in whatever I want, like from this status to pending?

    And what do we do with the setting/trigger in in the UI/Settings of Notifications?

    Just trying to understand how/what is all happening here/now.

    Plugin Author Kuba Mikita

    (@kubitomakita)

    The old_status is the previous post status, not used in this case so I wrote this. The pending param could be actual post status I think instead of a static string.

    The notification with “Trip sent for review trigger” should work right away.

Viewing 15 replies - 1 through 15 (of 21 total)
  • The topic ‘Double Notificaton email on ‘Post Update’’ is closed to new replies.