我在我的主题选项页面中添加了一个文本区域,以获得谷歌分析代码。直到今天我开发它时,它一直工作正常,在文本区域框中显示了这个错误:
<br />
<b>Warning</b>: Illegal string offset \'googanalytics\' in <b>C:\\xampp\\htdocs\\tamal\\wp-content\\themes\\blogkori\\theme-options.php</b> on line <b>85</b><br />
这是我的原始代码:
<?php
add_action( \'admin_init\', \'theme_options_init\' );
add_action( \'admin_menu\', \'theme_options_add_page\' );
/**
* Init plugin options to white list our options
*/
function theme_options_init(){
register_setting( \'blogkori_options\', \'blogkori_theme_options\', \'theme_options_validate\' );
}
/**
* Load up the menu page
*/
function theme_options_add_page() {
add_theme_page( __( \'BlogKori Options\', \'blogkori\' ), __( \'BlogKori Options\', \'blogkori\' ), \'edit_theme_options\', \'theme_options\', \'theme_options_do_page\' );
}
/**
* Create the options page
*/
function theme_options_do_page() {
global $select_options, $radio_options;
if ( ! isset( $_REQUEST[\'settings-updated\'] ) )
$_REQUEST[\'settings-updated\'] = false;
?>
<div class="wrap">
<?php echo "<h2>". __( \'BlogKori Options\', \'blogkori\' ) . "</h2>"; ?>
<?php if ( false !== $_REQUEST[\'settings-updated\'] ) : ?>
<div class="updated fade"><p><strong><?php _e( \'Options saved\', \'blogkori\' ); ?></strong></p></div>
<?php endif; ?>
<form method="post" action="options.php">
<?php settings_fields( \'blogkori_options\' ); ?>
<?php $options = get_option( \'blogkori_theme_options\' ); ?>
...
<div class="row">
<div style="" class="left">
<h3>Google Analytics</h3>
<p>This is where you should add Google Analytics or other tracking scripts</p>
<textarea id="blogkori_theme_options[googanalytics]" class="large-text" cols="50" rows="5" name="blogkori_theme_options[googanalytics]" onclick="this.focus();this.select()"><?php echo esc_textarea( $options[\'googanalytics\'] ); ?></textarea>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e( \'Save Changes\', \'blogkori\' ); ?>" />
</p>
</div>
感谢您的关注。
SO网友:Jacob Peattie
如果get_option( \'blogkori_theme_options\' )
没有一个值,你不能得到一个数组,这意味着$options[\'googanalytics\']
无效。你需要考虑这种可能性。
你可以通过检查googleanalytics
,并将其设置为新变量,如果未设置,则设置为空字符串:
$options = get_option( \'blogkori_theme_options\' );
$googleanalytics = isset( $options[\'googleanalytics\'] ) ? $options[\'googleanalytics\'] : \'\';
那你就回音
$googleanalytics
在文本区域:
<textarea id="blogkori_theme_options[googanalytics]" class="large-text" cols="50" rows="5" name="blogkori_theme_options[googanalytics]" onclick="this.focus();this.select()"><?php echo esc_textarea( $googanalytics ); ?></textarea>