[e-Newsletter] Allow for tracking of UNsubscriptions

Ref: https://premium.wpmudev.org/forums/topic/e-newsletter-improvements-to-subscribe-and-unsubscribe-processes

There’s no report to show subscribe/unsubscribe activity. Please add this, and/or provide a hook so that we can take action on these events. It’s important to know when we lose someone, and what they were reading when they made the decision.

For a hook I think we would just need the user ID, email address (if they’re not a member), and the Newsletter ID which led to the UNsubscribe request. From there we can use a custom function to get a timestamp, log other similar events, establish patterns of subscriptions and cancellations, etc.

I will look at the code as time permits, but it would help if a developer could point to existing hook, and suggest the ideal place to add a do_action for a custom hook for this, until a formal enhancement is added (if ever).


  • Ash
    • WordPress Hacker

    Hello Tony G

    There’s no report to show subscribe/unsubscribe activity.

    That’s a good point, I am marking this as a feature request. Let’s see how other members think about this.

    About the hook, I have talked to our SLS developer Panos and he will check and reply you very soon if he finds any useful hook :slight_smile:

    Have a nice day!



  • Panos
    • SLS

    Hi Tony,

    Could you please try following:

    add_filter( 'update_user_metadata', function( $null, $object_id, $meta_key, $meta_value, $prev_value ){

    if( 'email_newsletter_unsubscribe_code' == $meta_key &&
    $meta_value != $prev_value &&
    'unsubscribed' == $meta_value
    //Do Something
    return $null;
    }, 10, 5 );

    Please let us know if this works for you :slight_smile:


  • Tony G
    • Mr. LetsFixTheWorld

    Thanks for the note, @panoskatws. I hooked that filter (not action). There is too much going on here for me to continue but I’ll quickly note a couple issues:

    1) In e-newsletter.php I believe you want the following in function unsubscribe_by_code:

    $result = $wpdb->query( $wpdb->prepare( "UPDATE {$this->tb_prefix}enewsletter_members SET unsubscribe_code = '' WHERE unsubscribe_code = '%s'", $unsubscribe_code ) );
    update_user_meta( $member['member_id'], 'email_newsletter_unsubscribe_code', 'unsubscribed' ); // new
    return array('action' => 'unsubscribed', 'error' => false, 'message' => __( 'You are unsubscribed!', 'email-newsletter' ));

    2) Also down in delete_members in that same file:

    foreach( ( array ) $members_id as $member_id ) {
    $wpdb->query( $wpdb->prepare( "DELETE FROM {$this->tb_prefix}enewsletter_member_group WHERE member_id = %d", $member_id ) );
    $wpdb->query( $wpdb->prepare( "DELETE FROM {$this->tb_prefix}enewsletter_members WHERE member_id = %d", $member_id ) );
    update_user_meta( $member_id, 'email_newsletter_unsubscribe_code', 'unsubscribed' ); //new

    3a) It looks like the only time a new subscribe code is generated is when someone edits the user’s profile page. That doesn’t apply to a site with existing members – it’s not realistic to assume all profile pages for members will be visited after every change to newsletter membership. See e-newsletter.php, function edit_user_update_member, which is only executed with actions edit_user_profile_update and profile_options_update (current line 148-149). I believe the unsubscribe code should be added to the user meta whenever a user is added to a newsletter or member group.

    3b) Looking through the switch in admin_init, I don’t see any user_meta update with a new unsubscribe_code. All of the gen_unsubscribe_code() codes go to the enewsletter_members table. So maybe rather than updating user_meta at all, you might just want to keep these codes in the custom tables?

    4) All of that is somewhat irrelevant because the confirmed underlying problem is that there is only one unsubscribe code for the user, not for each group. This specific part of the plugin needs to be re-designed, with unique codes for each group.

    I can’t spend more time poking at this now but will be happy to come back to this when an update is issued.

    I hate to say this but for now I can’t use this plugin and I have no idea how anyone else does.

    PS: I’m reporting a security issue through chat – another reason why I don’t want to use this otherwise fine plugin yet.


Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.