wordpress仪表板在自定义条件下显示自定义错误消息

时间:2014-03-20 07:15:47

标签: wordpress

在更新元信息之前,在wordpress仪表板的edit-user.php中,我正在检查一个条件,当条件失败时,我想显示错误消息。我尝试用更新的类回显div,并尝试了WP admin_notices钩子但没有运气

 add_action( 'personal_options_update', 'save_extra_profile_fields' );
 add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );  

function save_extra_profile_fields( $user_id ) {
global $wpdb;        

    if(CONDITION TRUE) {
    update_usermeta( ........... );
    }
    else {
     WANT TO DISPLAY ERROR MESSAGE
     }
}

1 个答案:

答案 0 :(得分:2)

有一个用于验证用户额外字段的钩子。在更新用户详细信息之前,此挂钩将调用。

您可以显示如下错误消息: -

add_action( 'user_profile_update_errors', 'validate_extra' );
function validate_extra(&$errors, $update = null, &$user  = null)
{
    if (!$_POST['YOUR_FIELD'])
    {
        $errors->add('YOUR_FIELD', "<strong>ERROR</strong>: YOUR ERROR MESSAGE.");
    }
}

add_action( 'personal_options_update', 'save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );
function save_extra_profile_fields( $user_id )
{
    global $wpdb;

    update_usermeta( ........... );
}
相关问题