admin管理员组文章数量:1122832
I am trying to add an error notice to an admin screen. 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
本文标签: wp adminHow to add an error notice conditionally
版权声明:本文标题:wp admin - How to add an error notice conditionally 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736294529a1929375.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论