0

Lots of times, I might check for the presence of a variable in the post data and then do something. I'm having a hard time understanding what is going on when the user creates or updates a post. I have no problem doing any kind of backend function, but when I want to output something, I guess the page is re-directed? How do you normally handle this common task? How do I create an admin notice based on the presence of a post variable, or any immediate action?

What is going on here:

<?php
/*
 Plugin Name: Echo something on presence of variable
 Plugin URI: http://wordpress-bdd.com/
 Description: Hooking into the redirect with post variables
 */

class WhatIsGoingOn{

    public function __construct(){
        //add meta box:
        add_action('load-post.php', array($this, 'doAddMetaBoxes'));
        add_action('load-post-new.php', array($this, 'doAddMetaBoxes'));
        $this->whyCantIechoThisNotice();
     }

    public function doAddMetaBoxes(){
        add_meta_box('metaWithInput', 'Hello', array($this, 'echoMetaBox'), 'post', 'side', 'high');
    }

    public function echoMetaBox(){
        echo("<input type = 'text' name = 'myInput' id = 'myInput' />");
    }

     public function whyCantIechoThisNotice(){

        //check for presence of variable and do something:
        if (isset($_POST['myInput']) && (!($_POST['myInput'] == ""))){
            //If this line is uncomented, the die fires
            //die('I am here!');
            //How do I get the notice to echo?
                echo ("
                        <div class='notice notice-success is-dismissible'>
                                <p>This is a notice</p>
                        </div>
                ");
        }
    }

}

$WhatIsGoingOn = new WhatIsGoingOn;
1

exit; or store in $_SESSION. then on each page load check is_admin() and if your session var is set, then if so print the message, and remove the message from $_SESSION.

|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.