WordPress管理员-必需的自定义元检查 [英] Wordpress Admin - required custom meta check

查看:77
本文介绍了WordPress管理员-必需的自定义元检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm adding custom user meta in wordpress admin, and I would like my two custom fields are required, but how I show error and tell wordpress to not update profile if error ?

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']);
    }
}


推荐答案

I想您可以尝试一下

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']);
    }
}

这篇关于WordPress管理员-必需的自定义元检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆