1

The script below is called when saving my app's options page. All options are stored in an array in $options.

I'm getting a debug error "undefined index, id" on the line with the comment below. Any ideas how I can fix the script?

        foreach ($options as $value) 
        {
            if( isset( $value['id'] ) && isset( $_REQUEST[$value['id']] ) ) 
            {
                update_option( $value['id'], stripslashes($_REQUEST[$value['id']])  ); 
            }
            else
            {
                update_option( $value['id'], ""); //Error Here
            }
        }

1 Answer 1

2

Your if{} segment precludes the code in your else{} segment from working.

In other words:

In your if block, you ask: "Does $value['id'] exist?"

If it does not, your code executes your else block, which then attempts to reference the non-existent variable.

You'll need to set the array key before you can update it.

Your update_option function should simply check to see if the variable exists, and set it instead of updating it, if it does not.

Sign up to request clarification or add additional context in comments.

2 Comments

using this as a basis, the OP should think about updating update_option() to check if the array key exists before updating that key, and if the key != exist, create it first
Hmm, that was my intention. I guess it wasn't clear. Thanks for clarifying.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.