Wordpress Admin - required custom meta check

后端 未结 1 1534
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-06 09:44

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 ?

1条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-06 10:39

    I think you can try this

    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', "ERROR: 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']);
        }
    }
    

    0 讨论(0)
提交回复
热议问题