0

I am writing a Wordpress plugin that uses an array as an option but the new value is never set when I run update_option function. The first time this runs last_updated is a new element in the array but when I output the before update and after update values to the error_log I can see that the element last_updated has not been added to the option. Any help into what I am doing wrong here would be greatly appreciated! Thanks!

$coupon_grab_options = get_option( 'coupon_grab_option' );

// Output the current version of coupon_grab_option
error_log( print_r( $coupon_grab_options, true ), 0);

$last_updated = $coupon_grab_options['last_updated'];

$request = wp_remote_get('http://codes.clipncrazy.com/lastupdated.php');
$response = wp_remote_retrieve_body( $request );

// Update the coupon_grab_option with the latest last_updated value
if( $last_updated != $response ) {
    $coupon_grab_options['last_updated'] = $response;
    update_option( 'coupon_grab_option', $coupon_grab_options );
}

$coupon_grab_options = get_option( 'coupon_grab_option' );

// Output the updated version of coupon_grab_option
error_log( print_r( $coupon_grab_options, true ), 0);
  • Too simple to deserve "answer" points: Is $response supposed to be $request, or vice versa? – CK MacLeod Jan 14 '17 at 17:26
  • Thanks CK but I just neglected to add the response line when creating the question. I have edit the code in this post to include it. The response is working and I do enter the if condition but the update still doesn't work. – Nick Powers Jan 14 '17 at 17:29
  • Can I presume that you've already made sure that you're getting good values from wp_remote_get and wp_remote_retrieve_body? Or that the code functions (or doesn't) without the conditional? – CK MacLeod Jan 14 '17 at 17:38
  • Yes, I added error_log( "inside if, response is $response", 0) ; into the if condition block and I get "inside if, response is 1483750397" inside the error_log indicating it is in the if and that response has a valid value. – Nick Powers Jan 14 '17 at 17:49
  • I don't see anything obviously wrong with the syntax (though I'm not sure why you want a loose != rather than a strict !== ), so, unless we're both blocked, I would focus first on ascertaining that every value being tested is what you expect it to be at every point in the process as actually implemented: in other words be sure you're not running into a caching problem or some such with the remote get, for example. You might try writing to the log at every point or using a debugger that makes it easier to see every value at every point as processed. – CK MacLeod Jan 14 '17 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.