2012-03-30 58 views
3

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

这里有什么问题了吗? – 2012-03-30 15:48:13

+0

如果$ error为true,我想在页面顶部显示错误消息,如“请在列表中选择一个国家...”,并且仅在没有错误时更新配置文件 – Miracle 2012-03-30 15:50:27

回答

5

我想你可以试试这个

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