• 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

Email users when a new post is published

Chad Butler · Mar 19, 2013 ·

This tip is a request from a user.  I like user requests – that makes it easier to deliver the kind of tutorials users are looking for instead of things I just come up with on my own.

This particular tip will address how you can automatically send an email to all users when a post is published or updated. 

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, tips, wpdb, wp_mail

Welcome to RocketGeek Interactive › Forums › Email users when a new post is published

Tagged: add_action, email, tips, wpdb, wp_mail

  • This topic has 20 replies, 6 voices, and was last updated 6 years, 9 months ago by Chad Butler.
Viewing 14 reply threads
  • Author
    Posts
    • March 19, 2013 at 10:15 am #2285
      Chad Butler
      Keymaster

      This tip is a request from a user.  I like user requests – that makes it easier to deliver the kind of tutorials users are looking for instead of thin
      [See the full post at: Email users when a new post is published]

    • March 19, 2013 at 5:39 pm #2288
      SummitCompass
      Participant

      Thank you for such a quick response. I tested it today and it worked great.

      I’m trying to customize it a little more and I’m new to php so please excuse the simple nature of the questions.

      So based on the code from this post, if I wanted to add a custom “from” email address and name, would I just add this code: $headers[] = ‘From: Me Myself <me@example.net>’;  above where you have  $subj = $blogname . ” has a new post”;

      I’m assuming there is no way to add the “from address & name” using the email tab on the wp-members settings for this email function correct? That’s only for the registration, forgot password, etc emails?

      Last question…is there any way to pull in some of the content of the actual post as opposed to just a link to the post?  If somehow it could pull in the content that shows up before the #more tag, that would be even better.

      Thanks again for answering this so fast!!!

      Bobby

    • March 20, 2013 at 2:40 pm #2291
      Chad Butler
      Keymaster

      No problem Bobby – most users are new to php, so I know things can get confusing.  Your questions are actually very good ones as they push the envelope a little further.

      For the headers, it is actually good to have a from: header as this will improve your opposition from over-zealous spam filters.  You are on the right track, but you wouldn’t need the brackets in your variable (i.e. $headers  not $headers[]).

      The wp_mail function call is four parameters – receiver’s email, subject, body, headers.  In the example, I sent through empty headers which allows any defaults to kick in.  But you could define them here just as well.

      Your value can go directly in the function call, but it is more readable to use a variable.  So, if you add $headers = something; where the body and subject are, you can replace the last empty parameter in the wp_mail call with $headers.

      You are correct about the from name from the plugin settings – it is only used for the plugin’s mail functions.  That is by design so that if someone wants a specific email for registration, but might be using something else for some other part of the site that it doesn’t unintentionally overlap.

      If you define it in the email headers, you should be fine.  But you could also apply a filter for that.  There is an article at my other blog on this subject: http://butlerblog.com/2011/07/08/changing-the-wp_mail-from-address-in-wordpress-without-a-plugin/

      Lastly, yes you could get the post content and use some or all of it in the email.  The function does already have the post ID, so getting the content could be done with get_post (see: http://codex.wordpress.org/Function_Reference/get_post) which retrieves the post object.  Then your object would have the post_content and post_excerpt.  Something like:

      $thepost = get_post( $post_ID );
      $thecontent = $thepost->post_content;
      $theexcerpt = $thepost->post_excerpt;

      You could manipulate things a variety of different ways for adding that to the body of your email.

      I may have to do a second post here for an advance method 😉

    • March 21, 2013 at 1:29 pm #2294
      SummitCompass
      Participant

      Thanks for the quick response. I tried adding the email header first before I attempted the post content. Here’s what my code looks like.

      */

      $subj = $blogname . ” has a new post”;

      $body = $blogname . ” has published a new post. Read it here: ” .

      get_permalink( $post_ID );

      $headers = ‘From: Bill Zimmerman <bill@foridahorealtors.com>’;

      /**

      * Loop through the emails and send to each user

      */

      foreach( $users as $user )

      wp_mail( $user->user_email, $subj, $body, $headers );

      /**

      All the other code is just like you have it above. I’m getting an error message saying something was wrong with my functions.php file on line 74. I’m not sure what I did wrong. Any ideas?

      Thanks again!

      BP

    • March 21, 2013 at 3:38 pm #2297
      Chad Butler
      Keymaster

      That looks pretty good to me.  What is the error message and what is the code at line 74?

    • March 21, 2013 at 3:57 pm #2298
      SummitCompass
      Participant

      Parse error: syntax error, unexpected ‘:’ in /home/content/13/9435413/html/foridahorealtors.com/wp-content/themes/manifesto/functions/user/functions.php on line 74

      As far as the code on line 74, I have no idea. I don’t have 74 lines on my .php file. My file looks just like your code above, but with the additions I showed in my last post.

    • March 21, 2013 at 4:26 pm #2299
      Chad Butler
      Keymaster

      Do you have this somewhere that I can take a look at it?  If so, email some credentials (WP admin for sure, FTP would be better) to chad@butlerblog.com and I can take a look this evening.

    • March 28, 2013 at 2:24 pm #2322
      furio
      Participant

      I hope you don’t mind if I jump in with an additional question… 🙂

      What would we have to change from your sample code in order to only send out emails when a post from a particular custom post type has been published?

    • June 7, 2013 at 4:23 pm #2714
      CarolineElisa
      Participant

      Any way this could be customised to only send to users who have agreed to receive emails? Whether they are part of a group or just tick a box that passes a value… anything that they option to opt out of later?

      That would be AMAZING! 🙂

    • June 8, 2013 at 10:09 pm #2719
      Chad Butler
      Keymaster

      That’s definitely do-able.  You could set up a checkbox as part of the registration process to be notified of new post updates and that could be edited by the user when updating their info.  Getting the value of the user meta field in the foreach loop at the end where the email is sent would allow you to send to only opt-in users.

      I’ll probably need to put together some sample code to demonstrate this concept.

    • June 9, 2013 at 2:12 pm #2724
      Chad Butler
      Keymaster

      Put together an opt-in/opt-out sample snippet here:

      https://rocketgeek.com/tips-and-tricks/email-users-when-a-new-post-is-published-opt-inopt-out-version/

    • June 10, 2013 at 1:47 pm #2727
      SummitCompass
      Participant

      Hi Chad,

      Thank you for your help previously. The email functionality in mentioned here works very well. However, I have not been able to get the “preview” text to show up based on the code provided. It would give our customers a great value to see the preview text to get an idea of the post content and decide if they want to click to login and see the rest.

      Is this something you could revisit? We want our site to go live this week so time is of the essence.

      Again, thank you for your help so far…if we could just get the preview text showing up in the email, that would be perfect!

      Bobby

    • January 6, 2014 at 12:13 am #3745
      sevagv
      Participant

      Hi Chad,
      It seems like this is sending out a notification if a post is published or UPDATED. Is it possible to only send a notification for new post publication?

      Thanks,

      Tamar

    • January 6, 2014 at 10:19 am #3749
      Chad Butler
      Keymaster

      Hi Tamar,

      I updated the script accordingly, wrapping the process in a conditional check that will determine if this is a newly published post.

      • January 6, 2014 at 3:41 pm #3752
        sevagv
        Participant

        THANK YOU!

      • May 13, 2014 at 4:06 pm #4855
        SummitCompass
        Participant

        Hi Chad!

        Where is the script available with the conditional check for it being a newly published post? Do I just need to update to the newest version of wp-members? This would be great to add for my emails as well.

        Thx!
        Bobby

        • May 13, 2014 at 8:44 pm #4856
          Chad Butler
          Keymaster

          It’s this part:

          if( ( $_POST['post_status'] == 'publish' ) &&
              ( $_POST['original_post_status'] != 'publish' ) ) {
          
          //if you are here, it's a new post
          
          }
    • April 23, 2014 at 10:26 pm #4745
      linxnme
      Participant

      Hi I copied the code and it works well but I really want to display the full content in email. I see the codes posted above but where exactly do I insert the code for full content and exactly which of these do I use:

      $thepost = get_post( $post_ID );
      $thecontent = $thepost->post_content;
      $theexcerpt = $thepost->post_excerpt;

      Thanks for helping.

      • April 23, 2014 at 10:40 pm #4746
        Chad Butler
        Keymaster

        You could replace this:

        $body = 
        	$blogname . " has published a new post. Read it here: " 
        	. get_permalink( $post_ID );
        

        with this:

        $thepost = get_post( $post_ID );
        $body = $blogname . " has published a new post: \n\r" 
        	. $thepost->post_content;

        Of course, you could just make the body of the email the post content alone:

        $thepost = get_post( $post_ID );
        $body = $thepost->post_content;

        Hope that helps.

        • April 23, 2014 at 11:38 pm #4749
          linxnme
          Participant

          Thanks so much. For some reason, it would not work when I was posting the code I posted the code within WP panel. I didn’t get an error but I got a blank screen when I tried to save it. I had to access the php file via FTP and after some spacing issues, it worked fine.

          I appreciate your help.

          • April 24, 2014 at 11:07 am #4756
            Chad Butler
            Keymaster

            No problem – glad you got it working.

            The WP panel can be OK for most small things, but personally I find that FTP is best when implementing various code changes – that way if I break something, I can back out of it quickly by undoing my changes and FTPing a clean file back up.

  • Author
    Posts
Viewing 14 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