redirecting and showing notice from previous action

opinion: I like ajax interactions for things like this. DELETEs are cheap, fast things to accomplish server side, loading the whole page all over again takes too long. /opinion

If you’re going to use redirects and want to display error messages, you have a couple of options.

1. Use the redirect url itself to pass information to the next request.

Something like this:

wp_safe_redirect('your_plugin_url?msg=' . $message_code);

In the view:

$messages = array(
    '1' => 'Success!',
    '2' => 'Something went wrong :('
);

$msg_id = isset($_GET['msg']) ? $_GET['err'] : 0;
if (array_key_exists($msg_id, $messages)) {
    echo $messages[$msg_id];
}

2. Use the $_SESSION to pass around data.

Put session_start() somewhere in the early initialization of your plugin, to make sure it’s run before any page output is sent.

Then record the error or success messages in the session:

$_SESSION['painting_delete_msg'] = 'success! woot!';
wp_safe_redirect('your_plugin_url');

Then in the page:

if (isset($_SESSION['painting_delete_msg'])) {
    echo $_SESSION['painting_delete_msg'];
    unset($_SESSION['painting_delete_msg']);
}

The unset makes sure that the error is not repeated.

3. Don’t use redirects.

  case "delete":
    ob_clean();
    ob_start();
    $status = Myplugin_Painting_Info::delete($request["painting_id"]);
    include_once( "partials/myplugin-admin-paintings.php" );
    break;

  case "show":
    include_once( "partials/myplugin-admin-paintings.php" );
    break;


// myplugin-admin-paintings.php
if (isset($status)) {
    echo $status->message;
}