0

I have added a textarea in my theme options page to have Google analytics code. It was working fine until today when I was developing it, it's showing this error inside the textarea box:

<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 />

This is my original code:

<?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>

Thanks for looking.

1 Answer 1

1

If get_option( 'blogkori_theme_options' ) doesn't have a value you're not going to get an array back, which means that $options['googanalytics'] isn't valid. You'll need to account for this possibility.

You could do that by checking if googleanalytics is set, and setting it to a new variable, or an empty string if it's not set:

$options = get_option( 'blogkori_theme_options' );
$googleanalytics = isset( $options['googleanalytics'] ) ? $options['googleanalytics'] : '';

Then you would just echo $googleanalytics in the textarea:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.