• steve92

    (@steve92)


    I found this feature, I think you can put it in your plugin so that when you edit a post, it automatically notifies all subscribers of that post. With this method, there is no need to click the Resend Notification button.

    You can adapt it to the plugin:

    function my_project_updated_send_email( $post_id ) {
     
        // If this is just a revision, don't send the email.
        if ( wp_is_post_revision( $post_id ) ) {
            return;
            }
     
        $post_title = get_the_title( $post_id );
        $post_url = get_permalink( $post_id );
        $subject = 'A post has been updated';
     
        $message = "A post has been updated on your website:\n\n";
        $message .= $post_title . ": " . $post_url;
     
        // Send email to admin.
        wp_mail( 'admin@example.com', $subject, $message );
    }
    add_action( 'save_post', 'my_project_updated_send_email' );
Viewing 1 replies (of 1 total)
  • @steve92

    To be honest I really don’t like this as a concept. Adding this function will mean correcting a typo in a published post will generate a new notification that will look essentially identical to the recipient of the email.

    However, the code below takes into account more of the plugin preferences and the post status before calling the plugin code that triggers the email. It is untested in any way so if used please use caution.

    function my_project_updated_send_email( $post_id, $post, $update ) {
    	// If this is just a revision, don't send the email.
    	if ( $update || wp_is_post_revision( $post_id ) ) {
    		return;
        }
    
    	global $mysubscribe2;
    	if ( 'yes' === $mysubscribe2->subscribe2_options['pages'] ) {
    		$s2_post_types = array( 'page', 'post' );
    	} else {
    		$s2_post_types = array( 'post' );
    	}
    	$s2_post_types = apply_filters( 's2_post_types', $s2_post_types );
    	if ( ! in_array( $post->post_type, $s2_post_types, true ) ) {
    		return $post;
    	}
    
    	if ( 'yes' === $mysubscribe2->subscribe2_options['private'] ) {
    		$s2_status = array( 'publish', 'private' );
    	} else {
    		$s2_status = array( 'publish' );
    	}
    
    	if ( ! in_array( $post->post_status, $s2_status, true ) ) {
    		return;
    	}
    
    	$mysubscribe2->publish( $post );
    }
    
    add_action( 'save_post', 'my_project_updated_send_email', 10, 3 );
Viewing 1 replies (of 1 total)

The topic ‘Send automatic email when editing in post’ is closed to new replies.