php – Modify get_option array values and update_option


So I want to get the saved database option and then update it with the API return and then save the option in the database again, but I’m having some issues.

So I have the following method:

public static function refresh_access_token(string $user_id, string $access_token): bool
    if (!$authenticated_users = get_option('instagram_authenticated_users')) {
        return false;

    // The code for the cURL request is here (It's not needed for this question).

    $array = json_decode($curl_exec);

    foreach ($authenticated_users as $key => $user) {
        if ($user['user_id'] === (int) $user_id) {
            $user[$key]['access_token'] = $array->access_token;
            $user[$key]['access_token_expiration'] = time() + $array->expires_in;
            $user[$key]['last_updated'] = (string) time();

    return update_option('instagram_authenticated_users', $user);

The cURL response $array gives me the following:

$array = {stdClass} [3]
   token_type = "bearer"
   expires_in = {int} 5099901

Now within $authenticated_users, I have an array of arrays which outputs:

$authenticated_users = {array} [2]
 [0] = {array} [5]
    username = "semi_test1"
    user_id = {int} 17841449642220098
    access_token = "IGQVJWMDJnblBnUGMtMTVFa1NpUGdValNBVUZAyZAWM2OTdSSkRFdmNUbnVOQXJqeFhwbDVmT0c3aXJfamFYdnZANSlpXc3Mwc05PS0tMSzNsbXhES0tkTzNoOEY3RFRIb3dsblBiTXN3"
    access_token_expiration = {int} 1651281005
    last_updated = {int} 1646181108
 [1] = {array} [5]
    username = "semi_test2"
    user_id = {int} 17841400835712753
    access_token = "IGQVJVN3VOaUJrU2NzdGxWVTlwaXFLT2h1bnpFU3FKaEpOUGNPeWh2SkpjdHpnRXkyaGJ3NDZArXzJvRWFHdVRqZAEFfN0RodjV4cHQ2YTliSmhyVThUSjlCc1paLV9Fd2dqbzI1b25B"
    access_token_expiration = {int} 1651281136
    last_updated = {int} 1646181136

Now I’m looking at my $user_id param and using the foreach to compare them to the user_id inside an array, and if it matches, update the values inside the array and then update the options, but also retain the other array that hasn’t been changed.

I’ve tried everything and it doesn’t seem like it’s working and the update_option() retains the same data, can someone spot that I might be doing wrong?

Sema 6 months 2022-03-01T19:44:15-05:00 0 Answers 0 views 0

Leave an answer