admin_notices hook is not working when the request method is POST
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.
Leave an answer