admin_notices hook is not working when the request method is POST

Question

Trying to write a plugin and I have suddenly came to this problem and not sure what wrong I am doing. I know there must be something wrong but I cannot figure it out.. admin_notices work as usual, but when I put that into a if statement like the code below, it does not work 🙁 Can someone please help? Here is the code..

class BWFAQ
{
    public function __construct()
    {
        add_action('init', [$this, 'import_data']);
    }

    public function import_data()
    {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            add_action('admin_notices', function () {
                echo '<div class="notice notice-success is-dismissible">
                        <p>This notice is not being shown...</p>
                    </div>';
            });
        }
        add_action('admin_notices', function () {
            echo '<div class="notice notice-error is-dismissible">
                    <p>There was an error processing your file. You may want to try again. [Error 1000]</p>
                </div>';
        });
    }
}

$unused = new BWFAQ();

The notice that is outside of the if works well, but not the one that is within the if statement. I expect that one to be shown when the request method is POST (Like adding / updating a POST or PAGE). Eventually I will write some more code where my plugin will submit a form.

0
, Rakesh Mehta 4 years 2020-05-24T05:11:50-05:00 0 Answers 57 views 0

Leave an answer

Browse
Browse