2012-03-30 57 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']); 
    } 
}