2012-04-04 32 views
0

對於所有專家WP主題開發者,使用多個update_option()時,是有辦法來測試是否有任何更新,沒有工作(無論是通過連接錯誤或驗證),所以它會拋出一個錯誤?如果出現錯誤,是否可以忽略所有以前的update_option()代碼?測試在WordPress的錯誤`update_option()`

謝謝你們!如果更新由於某種原因失敗

回答

3

update_option()返回false。但是,如果選項已設置爲您嘗試更新的值,則它也會返回false。

所以,你最好先檢查是否選擇需要更新或使用get_option,然後如果需要更新,更新它的存在。

如果你的選項失敗的驗證測試,然後就打破任何驗證功能,您使用的了。你可以拋出一個Wp_Error,但這看起來太侵入了。我傾向於使用add_settings_error並以這種方式向用戶顯示錯誤。

回滾任何以前的update_option通話將要求你以前的值存儲在數組中,如果再重複回到了他們,如果你需要恢復你的選擇。

一般來說,我使用一個選項表項處理事情像主題選項,或者插件的選項。沒有比使用每個設置的新選項來污染我的選項表的主題更糟糕。

編輯:

下面是我如何處理選項驗證我的主題選項和插件的網頁。其基於類,所以如果你使用程序方法,你將不得不換掉一些變數。

public function theme_options_validate($input) { 

    if ($_POST['option_page'] != $this->themename.'_options') { 
     add_settings_error($this->themename.'_theme_options', 'badadmin', __('<h3><strong>Smack your hands!</strong></h3> - You don\'t appear to be an admin! Nothing was saved.', $this->textDom), 'error'); 
     return $input; 
    } 

    if (empty($_POST) && !wp_verify_nonce($_POST[$this->themename.'_options'],'theme_options_validate')) { 
     add_settings_error($this->themename.'_theme_options', 'badadmin', __('<h3><strong>Smack your hands!</strong></h3> - You don\'t appear to be an admin! Nothing was saved.', $this->textDom), 'error'); 
     return $input; 
    } 

    //begin validation - first get existing options - if any 
    $init_themeoptions = get_option($this->themename.'_theme_options'); 
    //create an array to store new options in 
    $newInput = array(); 

    //check to see if the author param has been set 
    if($input[$this->shortname.'_meta-author'] !== '') { 
     $newInput[$this->shortname.'_meta-author'] = wp_filter_nohtml_kses($input[$this->shortname.'_meta-author]); 
    }else{ 
     add_settings_error($this->themename.'_theme_options', 'emptydata', __('Oops - Author cant be empty'.', $this->textDom), 'error'); 
    } 

    //finally we see if any errors have been generated 
    if(get_settings_errors($this->themename.'_theme_options')){ 
     //if there are errors, we return our original theme options if they exist, otherwise we return the input data 
     //this clause handles the situation where you have no theme options because its your themes first run 
     if($init_themeoptions != false) { 
      return $init_themeoptions; 
     }else{ 
      return $input; 
     } 
    } 

    //if there were no errors we return the sanitized $newInput array and indicate successful save of data using settings error class, but 
    //we don't use the error attribute, which isnt all that intuitiive 
    add_settings_error($this->themename.'_theme_options', 'updated', __('<em>Success! </em> '.ucfirst($this->themename).' Theme Options updated!', $this->textDom), 'updated'); 
    return $newInput; 
} 

要在主題選項中顯示設置錯誤,請將以下行添加到您生成選項表單的位置;

settings_errors($this->themename.'_theme_options'); 

是的,選項表已經存在。我的意思是,不是爲每個主題選項生成一個新的條目到選項表中,而是將它們全部包裝在一個選項條目中。這也使得更容易驗證選項數據。

+0

我剛剛在評論中學到了很多東西。這是我第一次創建一個主題選項頁面,所以我願意接受任何其他相關信息,這些信息可能會引起我的注意。你是否介意在最後一段中提到的有關選項表的內容?是不是已經有一個? – enchance 2012-04-04 20:47:58

+0

這是一個很好的理解設置api閱讀http://codex.wordpress.org/Settings_API – noponies 2012-04-05 21:00:33