Case 2: Base on input (meta input) of user, I want to show error message instead success message, using save_post_at_order
action, redirect_post_location
filter, admin_notices
action and using a variable to identify error code ($this->message_number).
- In
save_post_at_order
action, assign $this->message_number
when error condition occurs (=null means no error).
function savePostAtOrder($post_ID, $post, $update)
{
if ($error) {
$this->message_number = 20;
}
}
- In
redirect_post_location
filter (the filter occurs after save_post_at_order
action), replace message=1
to message=message_number
if $this->message_number != null
function redirectPostLocation($location, $post_id)
{
if ($this->message_number)
$location = str_replace('message=1', 'message=' . $this->message_number, $location);
return $location;
}
- In
admin_notices
action (actually, it occurs before save_post_at_order
action, but I make it is just triggered when message
parameter >= 20, means error), base message
value will show corresponding message text
public function __construct()
{
if (isset($_GET['message']) && intval($_GET['message']) >= 20)
add_action('admin_notices', [&$this, 'adminNotice']);
}
function adminNotice()
{
$number = intval($_GET['message']);
switch ($number) {
case 20:
$message = __('Error code is 20.', 'my_domain');
break;
}
echo '<div class="error"><p>'.$message.'</p></div>';
}