0

I am trying to add an error notice to an admin screen. Shows error message Like this. So. I have a the function which outputs the error:

public function my_error_notice()
    {
        $text = __( 'There has been an error. Error!', 'kea' );
        $output =  <<<EOT
        <div class="error notice">
            <p>$text</p>
        </div>
EOT;
    }

And, in the constructor for my plugin I have:

 add_action( 'admin_notices', array($this, 'my_error_notice' ));

This works and outputs the error message. But - I only want this error message to appear when a specific condition occurs. See below:

I am hooking into user_register and in my callback for that I am doing some extra database work. I have a logged in author. That author has been given the capability to add/register new users. They do this using the normal WP admin screen for an admin to add a new user. In my plugin I have;

add_action('user_register', array($this, 'link_user_to_tutor'));

and

 public function link_user_to_tutor($new_user_id)
    {

        global $current_user;
        $current_user_roles = $current_user->roles;
        $current_user_id = get_current_user_id();
  
        if (($current_user_id !== 0) && in_array('author', $current_user_roles))
        {
            error_log("non admin user adding" . $new_user_id);
            //int or false
            $result = $this->wpdb->insert($this->kea_table_name4, array(
                'tutor_idx' => $current_user_id,
                'student_id' => $new_user_id
            ), array( '%d', '%d')); 
        }
     
        if ($result === false)
        {
            $err = "link_user_to_tutor-" . $new_user_id . "-" . $current_user_id . " wp user was created but not linked to tutor";
            
            $this->mail_error($err);
            error_log($err);
            //I want to print a message to the author/admin here to say that adding the new user has not worked.

        }

    } 

Thanks

5
  • I guess the answer is going to involve AJAX and doing it client-side? Commented Sep 9 at 13:06
  • what kind of error and which message are you trying to handle? And who is it going to display for? If registration failed then there is no admin to show errors in as the user can't be logged in ( if they failed to register then there is no account for them to be logged into so they never make it past the login screen ). Likewise if you're adding someone else what's the error condition you're trying to expose?
    – Tom J Nowell
    Commented Sep 9 at 13:06
  • @TomJNowell - I have added a more detailed explanation Commented Sep 9 at 13:17
  • it looks like the notice you're trying to add is succesfully added to that requests output, but that request just happens to be a redirect and the new request doesn't have that, and user-new.php is passing along that failure as a URL argument in the redirect URL. Although looking at your code it doesn't actually prevent the addition of the user. I'd also move that last if condition inside the first as $result only exists if the first condition is true, you'll get a PHP warning otherwise. This is also something custom capabilities is meant for if ( current_user_can( 'link_tutor' ).. etc
    – Tom J Nowell
    Commented Sep 9 at 13:35
  • Yes. I looked in users.php and WP just uses wp_get_admin_notice to format an admin notice with the correct markup and then just echoes it on the output stream. Thanks for pointing out that the reason I don't see that is because the result is a redirect. As for your comments about the placement of the second if - yes, you are right. Thanks. (Still not sure how to solve my actual problem - but now I know why it doesn't work). Commented Sep 9 at 13:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.