問題描述
我試圖在主題選項面板中新增一個新選項,以隱藏/顯示主題模板檔案中的註釋連結。
我可以選擇在面板中顯示,但有一些錯誤,它不起作用,沒有 php 錯誤。在下面的主要選項程式碼中,我已經注意到我已經將選項新增為陣列,並將其清理和驗證。
底部是根據選項設定顯示/隱藏評論連結的 php 。
這是 PHP 的邏輯問題嗎?和想法?
主要選項程式碼:
/**
* Describe the available options
**/
$vertigo_options_template = array(
array(
'name' => __( 'Accent Color', 'vertigo' ),
'desc' => __( 'Change the accent color by entering a HEX color number. (ie: <code>EE3322</code>)', 'vertigo' ),
'id' => 'accent_color',
'std' => 'ee3322',
'type' => 'colorpicker'
),
array(
'name' => __( 'Font', 'vertigo' ),
'desc' => __( 'Enable Hitchcock custom font (Note: this font only supports basic Latin uppercase letters, numerals, and some punctuation.)', 'vertigo' ),
'id' => 'vertigo_font',
'std' => ( '1' == get_option( 'lang_id' ) ) ? 'true' : 'false',
'type' => 'checkbox'
),
/** My new Option **/
array(
'name' => __( 'Comments', 'vertigo' ),
'desc' => __( 'Disable comment links)', 'vertigo' ),
'id' => 'disable_comments_link',
'std' => ( '1' == get_option( 'lang_id' ) ) ? 'true' : 'false',
'type' => 'checkbox'
),
);
/**
* Calculate default option values
*
* @return array
**/
function vertigo_get_default_options() {
global $vertigo_options_template;
$default_options = array();
foreach ( $vertigo_options_template as $option )
$default_options[$option['id']] = $option['std'];
return $default_options;
}
/**
* Create the options form
**/
function vertigo_theme_options_do_page() {
global $vertigo_options_template;
if ( ! isset( $_REQUEST['settings-updated'] ) )
$_REQUEST['settings-updated'] = false;
?>
<div class="wrap">
<?php screen_icon(); echo "<h2>" . get_current_theme() . ' ' . __( 'Theme Options', 'vertigo' ) . "</h2>"; ?>
<?php if ( false !== $_REQUEST['settings-updated'] ) : ?>
<div class="updated fade"><p><strong><?php _e( 'Options saved.', 'vertigo' ); ?></strong></p></div>
<?php endif; ?>
<form method="post" action="options.php">
<?php settings_fields( 'vertigo_options' ); ?>
<?php $vertigo_options = vertigo_get_theme_options(); ?>
<table class="form-table">
<?php foreach ( $vertigo_options_template as $option ) {
// Use default value if no option exists
$value = ( isset ( $vertigo_options[$option['id']] ) && !empty( $vertigo_options[$option['id']] ) ? $vertigo_options[$option['id']] : $option['std'] );
?>
<tr valign="top">
<th scope="row">
<?php echo $option['name']; ?>
</th>
<td>
<?php switch ( $option['type'] ) {
case 'colorpicker':
?>
<input type="text" name="vertigo_theme_options[<?php echo esc_attr( $option['id'] ); ?>]" id="<?php echo esc_attr( $option['id'] ); ?>" value="<?php echo esc_attr( $value ); ?>" class="color { pickerPosition:'right' }" />
<?php break;
case 'checkbox':
?>
<input type="checkbox" name="vertigo_theme_options[<?php echo esc_attr( $option['id'] ); ?>]" id="<?php echo esc_attr( $option['id'] ); ?>" value="true" <?php echo ( 'true' == $value ) ? 'checked="checked"' : ''; ?> />
<?php break;
default:
break;
} // END switch ?>
<label class="description" for="<?php echo esc_attr( $option['id'] ); ?>">
<?php echo $option['desc']; ?>
<?php if ( 'vertigo_font' == $option['id'] ) { ?>
<img src="<?php echo get_template_directory_uri(); ?>/inc/images/hitchcock.gif" alt="Hitchcock" id="hitchcock-sample"/>
<?php } ?>
</label>
</td>
</tr>
<?php } // END foreach ?>
</table>
<p class="submit">
<?php submit_button( __( 'Save Options', 'vertigo' ), 'primary', 'submit', false ); ?>
<?php submit_button( __( 'Reset Options', 'vertigo' ), 'secondary', 'vertigo_theme_options[reset]', false, array( 'id' => 'reset' ) ); ?>
</p>
</form>
</div><!-- .form-wrap -->
<?php
}
/**
* Sanitize and validate form input
*
* @param array options
* @return array sanitized options
**/
function vertigo_theme_options_validate( $input ) {
global $vertigo_options_template;
$defaults = vertigo_get_default_options();
// Check accent color input format
// Valid = hexadecimal 3 or 6 digits
$accent_color = preg_replace( '/[^0-9a-fA-F]/', '', $input['accent_color'] );
if ( 6 == strlen( $accent_color ) || 3 == strlen( $accent_color ) )
$input['accent_color'] = $accent_color;
else
$input['accent_color'] = $defaults['accent_color'];
// Check that Vertigo font checkbox value is either true or false
if ( ! isset( $input['vertigo_font'] ) )
$input['vertigo_font'] = ( $input['vertigo_font'] == 'true' ? 'true' : 'false' );
// My New Option: Check that Disable Comment Links checkbox value is either true or false
if ( ! isset( $input['disable_comments_link'] ) )
$input['disable_comments_link'] = ( $input['disable_comments_link'] == 'true' ? 'true' : 'false' );
// Reset to default options
if ( ! empty( $input['reset'] ) ) {
$defaults = vertigo_get_default_options();
foreach ( $input as $field => $value ) {
if ( isset( $defaults[$field] ) )
$input[$field] = $defaults[$field];
else
unset( $input[$field] );
}
}
return $input;
}
我在我的模板檔案中使用這個來顯示/隱藏註釋模板:
<?php if ( 'true' == $vertigo_theme_options['disable_comments_link'] ) { ?>
<?php comments_template( '', true ); ?>
<?php } ?>
這是 DB 中名為 vertigo_theme_options 的選項:
a:3:{s:12:"accent_color";s:6:"EE3322";s:12:"vertigo_font";s:4:"true";s:21:"disable_comments_link";s:5:"true";}
編輯 7/25/11:這在模板檔案中工作; 需要首先呼叫選項:
<?php $vertigo_theme_options = get_option( 'vertigo_theme_options' ); ?>
<?php if ( 'false' == $vertigo_theme_options['disable_comments_link'] ) { ?>
<?php comments_template( '', true ); ?>
<?php } ?>
最佳解決方案
回答您問題的第一部分:您的設定表已經覆蓋了 checkbox 案例; 所以如果你新增一個新的核取方塊輸入,你不需要新增任何東西到交換機。此程式碼將適用於您新增的所有核取方塊輸入:
case 'checkbox':
?>
<input type="checkbox" name="vertigo_theme_options[<?php echo esc_attr( $option['id'] ); ?>]" id="<?php echo esc_attr( $option['id'] ); ?>" value="true" <?php echo ( 'true' == $value ) ? 'checked="checked"' : ''; ?> />
<?php break;
更多關於 switch 的 PHP.net manual 和 w3schools 。
我有點困惑你的問題的措辭:
I’ve got the option to show up in the panel via the array
與
I’ve noted below… where it appears I need to add it in the “create form” section in the php that builds the form.
那麼:新的核取方塊當前是否顯示在 「設定」 頁面中,或者不顯示?
編輯
由於設定欄位在設定表單中正確顯示,我將嘗試解決以下兩個問題:該選項不正確儲存,並且該選項無法正確輸出。
第一件事情是:根據設定表單欄位選擇,在資料庫中正確儲存其值的選項?
如果它不正確儲存,那麼問題可能是您沒有正確檢查它。使用核取方塊,如果未啟用核取方塊,則 POST 資料中將省略整個引數。在這種情況下,為了在 DB 中儲存其值,您需要檢查正在設定的選項值,例如:
<?php
$vertigo_options['disable_comments_link'] = ( isset( $_POST['disable_comments_link'] ) ? true : false );
?>
注意:您需要調整,但您正在清理表單資料。如果您使用的是設定 API,那麼它會更像是這樣:
<?php
$output['disable_comments_link'] = ( isset( $input['disable_comments_link'] ) ? true : false );
?>
這是否使我們更接近?
編輯 2
所以我們知道該選項在 DB 中正確儲存。現在我們只需要在模板中正確輸出。
下一個問題:儲存在 DB 中的選項是離散的還是陣列?
從您的設定表單標記中,看起來選項儲存為陣列,因此我們需要在陣列上呼叫 get_option(),然後使用該陣列中的值:
<?php
$vertigo_theme_options = get_option( 'vertigo_options' );
?>
注意:要查詢實際的資料庫條目名稱,請參閱 vertigo_get_theme_options()函式。你也可以呼叫這個功能:
<?php
$vertigo_theme_options = vertigo_get_theme_options();
?>
無論如何,現在您應該可以在 $vertigo_theme_options 中參考您的選項; 例如。:
<?php if ( 'true' == $vertigo_theme_options['disable_comments_link'] ) { ?>
<?php comments_template( '', true ); ?>
<?php } ?>
那是否讓我們在那裡?
編輯 3
option_name under wp_options is vertigo_theme_options
然後嘗試這樣:
<?php
$vertigo_theme_options = get_option( 'vertigo_theme_options' );
?>
<?php if ( 'true' == $vertigo_theme_options['disable_comments_link'] ) { ?>
<?php comments_template( '', true ); ?>
<?php } ?>
我只有 100%肯定這應該做。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。