0

In my (admin) plugin I want to display a message using admin_notices action on successful sending of mail. Currently it is not displaying. I know this has something to do with the order of events, but I cannot figure out how to structure it so that the message displays.

Currently I'm adding the admin_notices action from within an init action callback. I'm not sure if this is allowed and therefore this may be the issue. However, if I add it to the __construct method, how can I get it to know that the mail has been sent?

Currently my Send_mail class looks something like:

class Send_mail {
    public function __construct() {
        add_action('init', array($this, 'send_mail'), 30 );
    }

    public function send_mail() {
        if (!isset($_POST['send-confirmation-true'])) {
            return;
        }

        // wp_mail() functionality
        // returns true on successful send

        if ($mail_sent == true) {
            add_action('admin_notices', array($this, 'show_admin_notice') );
        }
    }

    public function show_admin_notice() {
        // output admin notice html
    }
}

=== EDIT ===

After doing some testing, I've found out that actions called after the following if-statement:

if (!isset($_POST['send-confirmation-true'])) {
    return;
}

only work if they are hooked to an action that is either load-(page) (in my case: load-post.php) or before in the action order list. The action I need is admin_notices and therefore does not get added. What is causing this behaviour?

  • On what page are you calling this? Are you sure $mail_sent is true in the if-statement? The init action fires before the admin_notices action, so this should work. – engelen Jun 26 '14 at 6:29
  • $mail_sent definitely does return true, I've even tried moving it out of the if statement to test (therefore a direct descendent of send_mail()) and still the notice does not display. The page it is being called on is post.php. – tommyf Jun 26 '14 at 7:01
  • Am I correct in assuming that you want to display a notice after saving a post? – engelen Jun 26 '14 at 7:28
  • Yes - it saves then sends the mail. Only on a specific $_POST value being set (now added into the example as before I didn't think it was relevant), the $send_mail() proceeds. – tommyf Jun 26 '14 at 7:50
0

This workaround is far from ideal, but it gets the job done.

On $mail_sent == true set a post meta. In the __construct function add a callback to the admin_head action that checks whether the post meta has been set. If yes, delete the post meta and add the admin_notices action with the callback to the desired admin notice.

class Send_mail {
    public function __construct() {
        add_action( 'init', array($this, 'send_mail'), 30 );
        add_action( 'admin_head', array($this,'check_mail_sent') );
    }

    public function send_mail() {
        if (!isset($_POST['send-confirmation-true'])) {
            return;
        }

        // wp_mail() functionality
        // returns true on successful send

        if ($mail_sent == true) {
            update_post_meta($campaign_id, 'mail_sent', 'yes');
        }
    }

    public function check_mail_sent() {
        global $post;
        $mail_sent = get_post_meta( $post->ID, 'mail_sent', true );

        if($mail_sent == 'yes') {
            delete_post_meta($post->ID, 'mail_sent');
            add_action('admin_notices', array($this, 'show_admin_notice') );
        } else if ($mail_sent == 'no') {
            // Can handle a mail fail notice here if needed
        }
    }

    public function show_admin_notice() {
        // output admin notice html
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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