• Skip to primary navigation
  • Skip to main content

RocketGeek

Home of WP-Members, The Original WordPress Membership Plugin

  • WordPress Plugins
    • WP-Members
      • FAQs
      • Quick Start
      • Documentation
      • Extensions
    • Advanced Options
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Download Protect
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Invite Codes
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • MailChimp Integration
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • PayPal Subscriptions
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Salesforce Web-to-Lead
    • Security
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Text Editor
      • Purchase the Plugin
      • Get the Pro Bundle
    • User List
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • User Tracking
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Memberships for WooCommerce
    • WordPass
  • Store
    • Cart
    • Checkout
  • Blog
    • Basics [Free]
    • Tips and Tricks
    • Filters
    • Actions
    • Code Snippets
    • Shortcodes
    • Design
    • Release Announcements
  • Contact
  • Sign In
  • Show Search
Hide Search
Home » Tips and Tricks » Email users when a new post is published – opt-in/opt-out version

Email users when a new post is published – opt-in/opt-out version

Chad Butler · Jun 9, 2013 ·

This code snippet is an extension of the email users when a new post is published snippet.  Like that snippet, this one comes from user requests.

The difference here is that we will only email users who have requested specifically to be notified of new posts, and we’ll allow them to opt-out of that process at a later date by updating their profile.  

This article is only available to WP-Members Support Subscribers. If you have an existing subscription, please login below. If you do not have a current support subscription, you can purchase a support subscription here.

Already a Member? Log In Here
   
Forgot password? Click here to reset

To gain full access to WP-Members premium content, a current support subscription is required. You can purchase an annual support subscription for as little as $59, which provides you with access to priority support, a customer support forum, access to code snippets, and more.

Why wait? Choose your subscription option here.

[
Why join?]

Tips and Tricks add_action, email, post_update, tips, wpdb, wpmem_admin_after_block_meta, wpmem_admin_after_block_save, wp_mail

Welcome to RocketGeek Interactive › Forums › Email users when a new post is published – opt-in/opt-out version

Tagged: add_action, email, post_update, tips, wpdb, wpmem_admin_after_block_meta, wpmem_admin_after_block_save, wp_mail

  • This topic has 23 replies, 5 voices, and was last updated 4 years, 7 months ago by Chad Butler.
Viewing 17 reply threads
  • Author
    Posts
    • June 9, 2013 at 2:11 pm #2723
      Chad Butler
      Keymaster

      This code snippet is an extension of the email users when a new post is published snippet.  Like that snippet, this one comes from user requests. The
      [See the full post at: Email users when a new post is published – opt-in/opt-out version]

    • June 10, 2013 at 4:04 pm #2728
      CarolineElisa
      Participant

      Wow thanks Chad, this is going to be just what I need! But unfortunately it isn’t working yet…

      When I change $user->ID to a number then it sends, but sends to all users.

       

    • June 10, 2013 at 4:50 pm #2729
      CarolineElisa
      Participant

      Ok, I seem to have got it working by using:

      $users = get_users('meta_value=yes&meta_key=post_update_notify');

      And then:

      foreach( $users as $user ) {
      wp_mail( $user->user_email, $subj, $body, $head );
      }

      Thanks!

      • April 29, 2014 at 7:02 pm #4801
        linxnme
        Participant

        Ok, I seem to have got it working by using:

        $users = get_users(‘meta_value=yes&meta_key=post_update_notify’);

        And then:

        foreach( $users as $user ) {
        wp_mail( $user->user_email, $subj, $body, $head );
        }

        Thanks!

        My code did not produce emails with the given code either. Where are you putting this in the code? Are you replacing some of the code with this snippet? or are you adding to it? Either way…where?

        • April 29, 2014 at 7:58 pm #4802
          linxnme
          Participant

          The code is now producing emails but every registered user is receiving them whether they have opted in or not. I replaced this:

          /**
          * Loop through the emails and send to each user
          */
          foreach( $users as $user ) {

          /**
          * This checks to see of the user has opted in
          */
          $opted_in = get_user_meta( $user->ID, ‘post_update_notify’, true );

          /**
          * If the user has opted in, send the update message
          */
          if( $opted_in == ‘yes’ ) {
          wp_mail( $user->user_email, $subj, $body, ” );

          }
          }
          with this

          /**
          * special code to make only user that opted in get the email
          */

          $users = get_users(‘meta_value=yes & meta_key=post_update_notify’);

          foreach( $users as $user ) {
          wp_mail( $user->user_email, $subj, $body, ” );
          }

          So I am half way there as I am receiving the emails being sent to two different email addresses but one is opted in and the other is not, but both are receiving the emails.

    • June 14, 2013 at 2:47 pm #2747
      CarolineElisa
      Participant

      Hi again Chad,

      I don’t suppose you could help me out with the encoding for the email subject? This is the code I am using to get the post title as the email subject:

      $subj = get_the_title( $post_ID );

      But that means that when there are special characters in the post title the email subject displays them as code, for example & shows the HTML character entity.

      Thanks!
      Caroline

    • June 15, 2013 at 9:05 am #2749
      Chad Butler
      Keymaster

      I would probably start with htmlspecialchars_decode which is a PHP function to convert HTML special characters to regular text (see: http://php.net/manual/en/function.htmlspecialchars-decode.php)

      That would look like this:

      $subj = htmlspecialchars_decode( get_the_title( $post_ID ) );

      Or…

      $subj = get_the_title( $post_ID );
      $subj = htmlspecialchars_decode( $subj );

      Let me know how that works out.

    • June 18, 2013 at 1:55 pm #2757
      CarolineElisa
      Participant

      Afraid that doesn’t help, still get HTML code in the email subject 🙁

      Note that get_the_title( $post_ID ); is fine in the email body without need for htmlspecialchars_decode

      Any more tips?

      Thanks!

    • July 20, 2013 at 3:38 pm #2822
      slamack
      Participant

      Should the code at line 39 read as $head = 'From: Updates ';

      instead of 

      $head = 'From: Upates ';

       

      Thanks Chad!

    • August 2, 2013 at 10:20 am #2899
      Chad Butler
      Keymaster

      Thanks @slamack! I’ve updated accordingly.

      CarolineElisa – you might try using wp_specialchars_decode(). I tried this in testing and it seems to work fine for me. You could use this as follows for the $subj:

      $subj = wp_specialchars_decode( get_the_title( $post_ID ), ENT_QUOTES );

    • August 9, 2013 at 12:55 am #2963
      slamack
      Participant

      Chad, I’m having some trouble getting this to send an email with a new post.  I copy and pasted the “snippet”, as you have it written, into my functions.php folder.  I made sure that the “Option Name: post_update-notify” and “stored value if checked: yes” were as you specified.  Any help with this would be very much appreciated.

      Kind Regards,

      slamack

    • August 9, 2013 at 2:27 am #2964
      slamack
      Participant

      Great idea, Chad!  Thank you CarolineElisa for sharing your code that allowed this to work for me as well!

      Kind Regards,

      Sherie Lamack

    • August 9, 2013 at 7:31 am #2971
      Chad Butler
      Keymaster

      Hi Sherie – I’m confused between the last two posts – do you have it working now?  If not, definitely let me know and we’ll work through it.  Looking at what you posted, my first guess would be the option name.

      The script is set to look for “post_update_notify” (with underscores) and you indicated “post_update-notify” (one underscore, one dash).  That would be what I would check first.

    • August 9, 2013 at 9:53 am #2974
      slamack
      Participant

      Good Morning Chad,

      I received your email.  I tried to respond but it failed.  I just wanted to clarify I WAS able to get the snippet to work after using the code CarolineElisa posted in her post# 2729. Thank you so much for following up to see if I was able to get it to work!  You are AWESOME!  Have a wonderful weekend!

      Kind Regards,

      Sherie Lamack

    • August 9, 2013 at 6:08 pm #2977
      CarolineElisa
      Participant

      Thanks so much Chad! That code got rid of the problem with HTML encoding in the subject line.

    • August 9, 2013 at 10:23 pm #2980
      Chad Butler
      Keymaster

      Glad to hear you both got it working!

    • July 8, 2016 at 11:05 pm #10767
      howermj
      Participant

      Chad,

      Old post – new followup…

      I’m getting ready to implement this. As a non-coder, how could I add an option to suppress the email on a new post if I don’t want to send one? Jetpack has this option for not sending subscriptions to subscribers, and I’ve found it useful in the past. I know I could just disable the code in my functions.php file, make the post, and then reenable, but that won’t help any of the non-admin folks who will be writing for me.

      I’m glad I found this tutorial. This approach will now be my default, as I’ve dropped Jetpack after they lost 4+ years of subscribers when I moved from wordpress.com to self-hosting….

      Any other recommended changes based on the 3+ additional years of development since you wrote this?

      • July 9, 2016 at 6:27 am #10768
        Chad Butler
        Keymaster

        Any other recommended changes based on the 3+ additional years of development since you wrote this?

        Has it really been that long? It sure doesn’t feel like it.

        Nothing has really changed in this area over the course of time for both WP and WP-Members.

        It’s definitely a good idea to come up with some kind of a toggle for opting to not send a new post notification. I’m sure I can come up with something. I had placed some action hooks into the process of where the WP-Members meta box is on the post editor screen (where you select to block/unblock a single post). Those could be used to add a checkbox to send/not send for this script.

        I’ll work something out on that and post it.

        • July 10, 2016 at 9:25 am #10777
          Chad Butler
          Keymaster

          I’ve added to the code snippets to include a toggle for the admin to send/not send the message as a new post notification.

          The additional code for the admin uses a couple of action hooks that are part of the WP-Members post meta box for block/unblock settings on the post.

          The main code snippet was changed to assume this additional code is used. If it is not used, then one just needs to set $send = true at the beginning and remove the code indicated in the comments.

    • July 10, 2016 at 11:22 am #10779
      howermj
      Participant

      Chad,

      The post meta box part works fine, but I’m getting nothing else…either with your original code nor the mods from CarolineElisa above.

      Thoughts?

      • July 10, 2016 at 10:48 pm #10780
        Chad Butler
        Keymaster

        The changes do work as I did some testing before posting. Make sure you have both snippets together – there was additional logic added to the original main snippet along with the admin pieces in the second snippet (I separated them primarily to discuss them, but also for making removing the admin part easier when not desired).

        If you’re not getting emails, check the section in the main snippet where the “from” email address is set in the header. In almost every case, this will likely need to be an address that at minimum uses your domain. It will be best if it is a real address in which the domain matches that of the site’s domain.

        • July 11, 2016 at 11:25 am #10788
          Chad Butler
          Keymaster

          In reviewing the post updates, I suspect the issue for you may have been the “from” address in the header. In the code, it was not properly displaying the code as I wrote it. The “<" and ">” wrapping the from address were rendering as html and so that wasn’t shown on the page even though it was there.

          The line was this:
          $head = 'From: You <youremail@yourdomain.com>';

          I’ve fixed it in the displayed code now and that should be copy/paste-able (although the name/address should be changed to something real).

    • July 14, 2016 at 9:23 pm #10876
      howermj
      Participant

      Chad,

      FYI – update with your changes works just fine.

    • July 15, 2016 at 10:20 am #10890
      Chad Butler
      Keymaster

      Awesome – glad to hear that worked out! I think the option of sending/not sending is a definite plus for the script.

  • Author
    Posts
Viewing 17 reply threads
  • You must be logged in to reply to this topic.
Log In

Ready to get started?

Join Today!

© 2021 · butlerblog.com · RocketGeek is built using WordPress, WP-Members, and the Genesis Framework

  • butlerblog.com
  • WP-Members Support Subscription
  • Terms of Service
  • Refund Policy