微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – WordPress Admin – 需要自定义元检查

我在wordpress管理员添加自定义用户元素,我希望我的两个自定义字段是必需的,但我如何显示错误并告诉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' );
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']);
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐