admin管理员组

文章数量:1312956

I am writing a plugin. When an item (painting) is clicked for deleting while a list of them is shown at the dashboard, I want to delete it and then redirect the user to the previous page shown. So the action "delete" is catched and then goto action "show"

public function display_paintings_page() {
  $request = $this->get_request();
  switch ($request["action"]) {

  case "delete":
    ob_clean();
    ob_start();
    Myplugin_Painting_Info::delete($request["painting_id"]);
    wp_safe_redirect(wp_get_referer());
    break;

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

   /*
   / ...
   */

  }
}

This is the best I could manage. But the action messages (error, success) from the "delete" method are not shown, as the output buffer has to be cleaned to prevent a "headers already sent" error.

Is there any way to do this besides ajax or displaying an intermediate page with an "item removed; click to return"?

I am writing a plugin. When an item (painting) is clicked for deleting while a list of them is shown at the dashboard, I want to delete it and then redirect the user to the previous page shown. So the action "delete" is catched and then goto action "show"

public function display_paintings_page() {
  $request = $this->get_request();
  switch ($request["action"]) {

  case "delete":
    ob_clean();
    ob_start();
    Myplugin_Painting_Info::delete($request["painting_id"]);
    wp_safe_redirect(wp_get_referer());
    break;

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

   /*
   / ...
   */

  }
}

This is the best I could manage. But the action messages (error, success) from the "delete" method are not shown, as the output buffer has to be cleaned to prevent a "headers already sent" error.

Is there any way to do this besides ajax or displaying an intermediate page with an "item removed; click to return"?

Share Improve this question asked Nov 17, 2016 at 4:39 anonymous2090248anonymous2090248 111 silver badge2 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

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;
}

1) Add parameter (e.g. &msg=xxxxxxxxxxxx) to the URL. Redirect to that.

2) Place this in functions.php

if ($_GET['msg']) {
    wc_add_notice($_GET['msg'], 'notice' );
}

本文标签: redirectredirecting and showing notice from previous action