0

UPDATE: This error came from WP_DEBUG...of course, embedded_url didn't exist yet. However, when I set the value of $options[embedded_url] using the form, the $options array magically adds both the new value of embedded_url AND somehow remembers an old value of text_string. I've added

unset($options['text_string']);

to my code, but as soon as I press the Save Settings button, it's right back in the $options again. text_string does not exist anywhere in my code anymore. It's totally gone. Where is this text_string ghost?!


I followed this tutorial almost word for word to create a custom admin/settings page for a plugin I created. I tried to change the option name text_string to something else – embedded_url. I have WP_DEBUG enabled. When I loaded my plugin’s settings page, right above my text input field is this error:

Notice: Undefined index: embedded_url in ….

and then it points to the echo line of this function:

function plugin_setting_url() {
$options = get_option('plugin_options');
echo "<input id='plugin_url' name='plugin_options[embedded_url]' size='40' type='text' value='{$options['embedded_url']}' />";
}

Since this is the only place that text_string appears in the tutorial other than the validation method, I do not understand why embedded_url is not defined in the same manner as text_string was. What am I doing incorrectly?

3
  • $plugin_options[embedded_url] - you're missing the $. and if it's an array... you better do name='" $plugin_options['embedded_url'] . "'
    – gdaniel
    Commented Jul 29, 2014 at 18:45
  • Thanks for the response, gdaniel - The tutorial does not use a $ in front of the name in their echo <input....>. Nonetheless, I did as you recommended and I am still receiving the same warning AND a new warning saying plugin_options is an undefined variable.
    – L. Becker
    Commented Jul 29, 2014 at 19:02
  • Then it was probably correct before. Sry. I thought that was a variable being passed by php, but it looks like it's just a regular array.
    – gdaniel
    Commented Jul 29, 2014 at 19:04

1 Answer 1

0

I think you need to set a default value when you use get_option

function plugin_setting_url() { $options = get_option('plugin_options', array('embedded_url'=>'')); echo ""; }

Unless plugin_options already exists, then you would need to use wp_parse_args to merge two arrays.

function plugin_setting_url() { $options = wp_parse_args(get_option('plugin_options',array()),array('embedded_url'=>'')); echo ""; }

2
  • Thanks DaveDunn. I added the line you recommended. And my text_string ghost is also gone now that I ran a command to delete 'plugin_options'. Seems like I'm up and running again! Thanks!
    – L. Becker
    Commented Jul 29, 2014 at 19:52
  • No problem. I just had a look at the tutorial you linked to. I think the text_string hung around because the validation function plugin_options_validate uses get_option and then added your new index before saving.
    – DaveDunn
    Commented Jul 29, 2014 at 19:59

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.