0

すべてのエキスパート WP テーマ開発者にとって、複数を使用するupdate_option()場合、更新のいずれかが機能しなかったかどうかをテストする方法はありますか (接続エラーまたは検証によって)、エラーがスローされますか? また、エラーが発生した場合、以前のすべてのupdate_option()コードを無視することは可能ですか?

みんなありがとう!

4

1 に答える 1

3

update_option()は、何らかの理由で更新が失敗した場合にfalseを返します。ただし、オプションが更新しようとしている値にすでに設定されている場合もfalseを返します。

したがって、最初にオプションを更新する必要があるかどうか、またはget_optionを使用して存在するかどうかを確認してから、更新する必要がある場合は更新することをお勧めします。

オプションが検証テストに失敗した場合は、使用している検証関数から抜け出します。Wp_Errorをスローすることもできますが、それはあまりにも侵襲的であるように思われます。私はadd_settings_errorを使用して、その方法でユーザーにエラーを表示する傾向があります。

以前のupdate_option呼び出しのいずれかをロールバックするには、以前の値を配列に格納する必要があります。その後、オプションを復元する必要がある場合は、それらを繰り返し処理します。

通常、私は1つのオプションテーブルエントリを使用して、テーマオプションやプラグインオプションなどを処理します。設定ごとに新しいオプションでオプションテーブルを汚染するテーマを取得することほど悪いことはありません。

編集:

以下は、テーマオプションとプラグインページのオプション検証を処理する方法です。そのクラスベースなので、手続き型アプローチを使用している場合は、いくつかの変数を交換する必要があります。

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;
}

テーマオプションUIに設定エラーを表示するには、オプションフォームを生成する場所に次の行を追加します。

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

そして、はい、オプションテーブルはすでに存在します。つまり、テーマオプションごとにオプションテーブルに新しいエントリを生成する代わりに、それらすべてを1つのオプションエントリにまとめます。これにより、オプションデータの検証も簡単になります。

于 2012-04-04T20:18:54.597 に答える