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

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

关于php - Wordpress 管理员 - 需要自定义元检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9945757/

相关文章:

php - url 重定向到根文件夹后的 Laravel 斜线

java - Java 在单独的机器上写入/读取文件的最佳方式?

php - PHP代码在哪里解释?

PHP 捕获行号?

Wordpress:使用 wp_insert_post() 在帖子中插入帖子格式

php - mySQL 中的复杂查询(在多个表中搜索用户)

php - 在 Woocommerce 中禁用编辑特定的管理员自定义字段

php - 试图在 PHP 中获取用户 IP、引荐来源数据 - 为什么这段代码不起作用?

php - PHP 中发布的冷却时间

php - 在 Woocommerce 中隐藏一些结帐字段问题