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

在WordPress中,通过添加行动钩子`user_profile_update_errors`,可以实现自定义用户资料字段的验证。当超级管理员未填写特定的国家或时区时,会触发错误并阻止配置文件更新,同时显示错误信息提示用户。这个解决方案利用了`personal_options_update`和`edit_user_profile_update`钩子来保存和验证数据。
摘要由CSDN通过智能技术生成

我在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', "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']);

}

}

标签:php,wordpress,admin

来源: https://codeday.me/bug/20190902/1789988.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值