I\'m trying to add a new option to a theme options panel 隐藏/显示主题模板文件中的注释链接。
我可以选择显示在面板中,但有一个错误,它不起作用,并且没有php错误。我在下面的Main options code 我将选项添加为数组,并对其进行清理和验证。
和at the bottom 是根据选项设置显示/隐藏注释链接的php。
这是php逻辑问题吗?还有想法?
Main options code:
/**
* 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;
}
And I\'m using this in my template files to show/hide the comments template:
<?php if ( \'true\' == $vertigo_theme_options[\'disable_comments_link\'] ) { ?>
<?php comments_template( \'\', true ); ?>
<?php } ?>
This is what the options look like in the DB called 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";}
Edit 7/25/11: This works in template file; need to call options first:
<?php $vertigo_theme_options = get_option( \'vertigo_theme_options\' ); ?>
<?php if ( \'false\' == $vertigo_theme_options[\'disable_comments_link\'] ) { ?>
<?php comments_template( \'\', true ); ?>
<?php } ?>
最合适的回答,由SO网友:Chip Bennett 整理而成
回答问题的第一部分:您的设置窗体已具有checkbox
涵盖的案例;因此,如果添加新的复选框输入,you don\'t need to add anything to the switch. 此代码将应用于您添加的所有复选框输入:
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.
我对你问题的措辞有点困惑:
我可以选择通过阵列显示在面板中
vs。
我在下面记下了。。。我需要将它添加到构建表单的php中的“创建表单”部分。
因此:does the new checkbox currently appear in the Settings page form, or does it not appear?
EDIT
由于设置字段在设置表单中正确显示,我将尝试解决以下两个问题:选项未正确保存和选项未正确输出。
第一件事第一:is the option saving its value properly in the DB, based on the settings form field selection?
如果是not saving correctly, 那么问题可能是你没有正确地检查它。对于复选框,如果未启用该复选框,则会从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 );
?>
这能让我们更接近吗?
EDIT 2
因此,我们知道该选项正在数据库中正确保存。现在我们只需要让它在模板中正确输出。
下一个问题:are the options stored in the DB discretely, or as an array?
从设置表单标记中,选项似乎存储为array, 所以我们需要打电话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
; e、 g.:
<?php if ( \'true\' == $vertigo_theme_options[\'disable_comments_link\'] ) { ?>
<?php comments_template( \'\', true ); ?>
<?php } ?>
这样我们就到了吗?
EDIT 3
wp\\u options下的option\\u name是vertigo\\u theme\\u options
然后尝试以下操作:
<?php
$vertigo_theme_options = get_option( \'vertigo_theme_options\' );
?>
<?php if ( \'true\' == $vertigo_theme_options[\'disable_comments_link\'] ) { ?>
<?php comments_template( \'\', true ); ?>
<?php } ?>
我几乎百分之百肯定这应该可以做到。