Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
?>
share|improve this question

1 Answer 1

up vote 5 down vote accepted
+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!

share|improve this answer
    
It seems there should be an easier solution than your proposed idea. Something that maybe doesn't require using transients? –  Michael Ecklund Jul 21 '12 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. –  Brian Fegter Jul 22 '12 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. –  Michael Ecklund Jul 23 '12 at 15:29
    
@MichaelEcklund Are you returning a bool value from your send_group_notificatios method? –  Brian Fegter Jul 23 '12 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. –  Michael Ecklund Jul 23 '12 at 16:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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