New custom theme option (text input) giving index error

I am using the theme-options.php file from ThemeShaper:
http://themeshaper.com/2010/06/03/sample-theme-options/

I can succesfully add new dropdown options, however when I add a new text input I receieve this error when I view it in the admin area (I added the THEME_DIRECTORY):

Read More
<br /> <b>Notice</b>:  Undefined index:  cpt_sm_dribbble_handle in <b>/[THEME_DIRECTORY]/theme-options.php</b> on line <b>359</b><br />

When I change the input value to something else and save it, it works fine and never gives an error again. However I don’t want this error message to appear to users. I would rather it just appear blank since there is no value inputted yet.

I suspect that I’m getting this error because the database doesn’t recognize this new option. And I think I need some sort of verification in my code to say if this doesn’t exist set the value to null. Though I’m not sure if this really is the problem and if so how to fix it.

Here’s the minimized code for my options file and for a new text option (I’ve noted the line that is throwing the error):

add_action('admin_init', 'cpt_options_init');
add_action('admin_menu', 'theme_options_add_page');

function cpt_options_init() {
    register_setting('cpt_options', 'cpt_theme_options', 'theme_options_validate');
}

function theme_options_add_page() {
    add_theme_page('Theme Options', 'Theme Options', 'edit_theme_options', 'theme_options', 'theme_options_do_page');
}

function theme_options_do_page() {
    global $sm_select_options;

    if (!isset($_REQUEST['settings-updated']))
        $_REQUEST['settings-updated'] = false;
?>

    <form method="post" action="options.php">
        <?php settings_fields('cpt_options'); ?>
        <?php $options = get_option('cpt_theme_options'); ?>

        <tr valign="top">
            <th scope="row">Dribbble</th>
            <td>
                <label class="description" for="cpt_theme_options[cpt_sm_dribbble_handle]">Dribbble Username</label>
                <input id="cpt_theme_options[cpt_sm_dribbble_handle]" class="regular-text" type="text" name="cpt_theme_options[cpt_sm_dribbble_handle]" value="<?php esc_attr_e($options['cpt_sm_dribbble_handle']); ?>" /> // THIS IS THE LINE THAT IS THROWING THE ERROR (line 359)
            </td>
        </tr>

    </form>

<?php
}

function theme_options_validate($input) {
    global $sm_select_options;

    // Say our text option must be safe text with no HTML tags
    $input['cpt_sm_dribbble_feed'] = wp_filter_nohtml_kses($input['cpt_sm_dribbble_feed']);

    return $input;
}

Any ideas on what I’m doing wrong?

Thanks for your time.

Related posts

Leave a Reply

1 comment

  1. Wrap the call in an isset() conditional in this line:

    <input id="cpt_theme_options[cpt_sm_dribbble_handle]" class="regular-text" type="text" name="cpt_theme_options[cpt_sm_dribbble_handle]" value="<?php esc_attr_e($options['cpt_sm_dribbble_handle']); ?>" />
    

    Try something like this:

    $cpt_sm_dribbble_handle = ( isset( $options['cpt_sm_dribbble_handle']) ? $options['cpt_sm_dribbble_handle']) : '' );
    
    <input id="cpt_theme_options[cpt_sm_dribbble_handle]" class="regular-text" type="text" name="cpt_theme_options[cpt_sm_dribbble_handle]" value="<?php esc_attr_e($cpt_sm_dribbble_handle); ?>" />
    

    EDIT

    Note: your other alternative is to set default options on init, so that this option is set to some default value, e.g. an empty string.