Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have come across quite an odd problem when using the update_option function, and I am sure that I've tried everything I can think of to get it working, but unfortunately it isn't.

I'm using AJAX to submit a form (using the popular method described here) in the backend, on a custom options page. Everything is working, except for the update_option part. I just don't understand, is there an issue with using that function in an AJAX function callback?

    public function vmAddApp() {
        parse_str( stripslashes( $_POST[ 'data' ] ), $data );
        $nonce = $_POST[ 'nonce' ];
        $app_info = $data[ 'vm_fbconnect' ];
        $option = get_option( 'vm_fbconnect', array() );

        $appId = $data[ 'vm_fbconnect' ][ 'appId' ];
        $secret = $data[ 'vm_fbconnect' ][ 'secret' ];

        $app_response = wp_remote_get( 'https://graph.facebook.com/' . $appId );
        $response_body = json_decode( $app_response[ 'body' ] );
        $app_name = $response_body->name;

        $new_app = array(
            'name' => $app_name,
            'id' => $appId,
            'secret' => $secret
        );
        $option[ 'apps' ][] = $new_app;
        $data = update_option( 'vm_fbconnect', $option );

        // UPDATE:
        // If I put either:
        // $data = $option;  // <- this returns the option correctly, see below
        // or
        // $data = get_option( 'vm_fbconnect', array() );  // <- this returns 'Array' as a string
        // But regardless, both SAVE 'Array' as the option's value..

        header( "content-type: application/json" );
        $response = array( 'data' => $data );
        echo json_encode( $response ); // <- this is how I'm getting the response in the JS console
        exit;
    }

Edit 1: Debugging (I updated the code above, see comments)

This is what I get when I return the modified option from inside the function:

enter image description here

This is what I get when I use get_option again.

ajax

After the option has been saved and I am done with the form, sometimes I will go right into the page currently displayed and do die( var_dump( get_option( 'vm_fbconnect', array() ) ); and it says just string(5) 'Array'.

Also, I'm always careful to reset the option before trying each time, and careful to make sure I commented out the delete_option function when trying again! :)

share|improve this question
1  
have you tried doing a var_export or similar on the $option before it is updated to see if it's not happening before the update_option? –  Evan Mattson Jun 16 '12 at 17:02
    
Yes, it is an array before it is run through update_option, I have narrowed it down to that exact function. –  Jared Jun 16 '12 at 17:55

2 Answers 2

It sounds like your array isn't being serialized before updating the option for whatever reason. Perhaps due to ajax or just your particular situation.

Try this:

// ...

$option[ 'apps' ][] = $new_app;

if( ! is_serialized( $option ) )
    $option = maybe_serialize( $option );

$data = update_option( 'vm_fbconnect', $option );

// ...
share|improve this answer
    
Thanks for your input. If I add that, then $data returns false and the option doesn't update at all. –  Jared Jun 16 '12 at 17:35
    
@Jared - ah, ok. What if you tried type casting $option in your update call like this: $data = update_option( 'vm_fbconnect', (array) $option );? –  Evan Mattson Jun 16 '12 at 19:12
    
I did that as well, it gives me the Array string saved to the option. One thing I did notice though, if I echo $data = get_option( 'vm_fbconnect', array() ); right after I update it, and console.log that, it returns the correct values.. I wonder if the headers or something have to do with it? –  Jared Jun 16 '12 at 19:21
    
I take that back, I just tried it again, and even getting the option afterwards is just the string Array. –  Jared Jun 16 '12 at 19:25
    
@Jared - how are you checking the value of the saved option? –  Evan Mattson Jun 16 '12 at 19:37
up vote 0 down vote accepted

Turns out, my question didn't even have to do with AJAX, it had to do with the function used to validate the settings that was supplied with register_setting.

The problem with it was that it wasn't going deep enough into the array to even save the values. I needed to modify it to look like so:

    public function settingsValidate( $todo ) {
        $option = get_option( 'vm_fbconnect', array() );

        if( !empty( $todo ) && is_array( $todo ) ) {
            foreach( $todo as $name => $value ) {
                if( is_array( $value ) ) {
                    foreach( $value as $nam => $val ) {
                        if( is_array( $val ) ) {
                            foreach( $val as $n => $v ) {
                                $option[ $name ][ $nam ][ $n ] = wp_filter_nohtml_kses( $v );
                            }
                        } else {
                            $option[ $name ][ $nam ] = wp_filter_nohtml_kses( $val );
                        }
                    }
                } else {
                    $option[ $name ] = wp_filter_nohtml_kses( $value );
                }
            }
        }

        return $option;
    }

I know there's a better way to do this, will edit sometime later.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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