2

I am trying to send out e-mail, SMS and IM notifications to group members of this WordPress website, whenever a WordPress page in their user group has been published/updated.

I figured using the action hook 'save_post' would be the best solution. However, I ran into some annoying factors along the way, and believe I have found a fix for them.

One of the annoying factors was every time the page was updated, it would perform the action twice. So I used the fix proposed here and it seems to have corrected the double saving issue. If you have a better solution to the double saving issue, I would be glad to read about it.

Now, the issue I am facing is I don't know exactly how to properly use the filter hook 'post_updated_messages' in coherence with action hook 'save_post'. When I place the reference to add_filter('post_updated_messages', array(&$this, 'message')); in the class constructor, it displays the updated message when I have only just arrived on the edit screen and haven't even clicked the update button yet.

After talking to @rarst in chat, he informed me that the 'post_updated_messages' filter was referenced as 'place' and not as 'event' which gave me the idea of moving the reference to add_filter('post_updated_messages', array(&$this, 'message')); to $this->save_post();.

After moving the filter to $this->save_post();, now I don't see the custom updated message displayed at all. I just see the typical post updated message. However, the group notification method still fires.

Where is the best place to make the filter reference. Or if this situation does not require this particular hook, what hook should be used, and where should it be placed?

I tagged this as related to plugin development, even though it's just a class file included by the functions.php file of this particular active WordPress theme.

Here is a rough idea of what I have:

<?php
if(!class_exists('tester_class')){
    class tester_class{
        /**
         * Initiate!
         *
         * @return void
         **/
        public function __construct() {
            add_action('save_post', array(&$this, 'save_post'));
        }

        /**
         * Display update message
         * 
         * Outputs a message using post_updated_messages, after the WP save_post action
         *
         * @return void
         **/
        public function message(){
            _e('<div class="updated"><p><strong>Post updated &amp; Notified all group members!</strong></p></div>');
        }

        /**
         * Hooks the WP save_post action
         * 
         * Perform class call after post is saved.
         *
         * @return void
         **/
        public function save_post(){
            global $flag;
            if($flag == 0){
                $this->send_group_notifications();
                add_filter('post_updated_messages', array(&$this, 'message'));
            }
            $flag = 1;
        }

        /**
         * Notifies group members
         * 
         * Sends email, SMS, and IM notifications to all group members.
         *
         * @return void
         **/
        public function send_group_notifications(){
            // example...
        }   

    }// EoF tester_class class

    // Initiate tester_class class
    $tester_class = new tester_class();

}// EoF tester_class class_exists
?>

2 Answers 2

5
+50

Updated:

First, you will need to return a bool value on your notifications method so we can reliably set a marker for the message method. Then, you will need to set a $_POST array element to pass on to the redirection filter.

public function save_post($post_id){
    //Add a $_POST key if you syndicated successfully
    if($this->send_group_notifications()) //return true from your notification method
        $_POST['syndicated'] = true;
}

The following filter works from your functions.php file. I couldn't get it to work properly from a plugin file. Basically, what's happening here is we sniff out the $_POST array element you set in the save_post action and see if we add a query string variable to the redirect.

add_filter('redirect_post_location', 'update_post_redirect');
function update_post_redirect($location){
    $syndicated = isset($_POST['syndicated']) ? $_POST['syndicated'] : 0;
    if($syndicated == true)
        $location .= '&syndicated=1';
    return $location;
}

Then finally, we sniff out the $_GET variable so we can determine if a message needs to be displayed.

public function message($messages){
    $syndicated = isset($_GET['syndicated']);
    if($syndicated)
         //Do something with the messages array here.
    return $messages;
}

Check out this post for a nice example of how to use this filter: Set custom messages for post update/save

Hope this helps you out!

8
  • It seems there should be an easier solution than your proposed idea. Something that maybe doesn't require using transients? Jul 21, 2012 at 12:53
  • @MichaelEcklund After some digging and frustrating testing. I got this to work without adding globals or transients. Check out my latest edit. Jul 22, 2012 at 4:20
  • Does not appear to be working? Maybe I'm doing something wrong. Could you reference your changes so that it's intertwined with the sample code I have provided? You can edit your answer or link to pastebin. Jul 23, 2012 at 15:29
  • @MichaelEcklund Are you returning a bool value from your send_group_notificatios method? Jul 23, 2012 at 15:55
  • No. It just executes a few other methods. One that handles SMS, another for email and one final one for IM. They don't return anything. Jul 23, 2012 at 16:44
0

I checked how ACF did it and tried it their way, worked for me.

class Directory_Post_Type
{
  public static $post_type = "directory";

  public function __construct()
  {
    add_action( 'init', array( $this, 'init' ) );

    add_action( 'admin_init', array( $this, 'admin_init' ) );

    add_filter('post_updated_messages', array( $this, 'post_updated_messages') );
    add_filter('bulk_post_updated_messages', array( $this, 'bulk_post_updated_messages'), 10, 2 );
  }

  ......

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.