Wordpress管理员-需要自定义元检查


Wordpress Admin - required custom meta check

我正在wordpress admin中添加自定义用户元,我希望我的两个自定义字段是必需的,但我如何显示错误并告诉wordpress在出现错误时不要更新配置文件?

add_action('personal_options_update', 'sweety_admin_update_extra_profile_fields');
add_action('edit_user_profile_update', 'sweety_admin_update_extra_profile_fields');
function sweety_admin_update_extra_profile_fields($user)
{
    $error = false;
    if (is_super_admin())
    {
            if (!$_POST['country'] || !$_POST['timezone'])
            {
                $error = true;
            }
        update_user_meta($user, 'country_id', $_POST['country']);
        update_user_meta($user, 'timezone_string', $_POST['timezone']);
    }
}

我想你可以试试这个

add_action('user_profile_update_errors', 'validate_extra' 10, 3);
function validate_extra(&$errors, $update = null, &$user  = null)
{
    if (is_super_admin())
    {
        if (!$_POST['country'] || !$_POST['timezone'])
        {
            $errors->add('empty_country', "<strong>ERROR</strong>: Please select a country in the list");
        }   
        
    }
}
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)
{
    if (is_super_admin())
    {
        update_user_meta($user, 'country', $_POST['country']);
        update_user_meta($user, 'timezone_string', $_POST['timezone']);
    }
}