Title: Fix for Undefined Variable $custom_fields in JSON_API_User_Controller Class
Last modified: August 19, 2024

---

# Fix for Undefined Variable $custom_fields in JSON_API_User_Controller Class

 *  Resolved [sunstyle](https://wordpress.org/support/users/sunstyle/)
 * (@sunstyle)
 * [1 year, 9 months ago](https://wordpress.org/support/topic/fix-for-undefined-variable-custom_fields-in-json_api_user_controller-class/)
 * Hello Ali,
 * I noticed an issue in the `JSON_API_User_Controller` class related to the `$custom_fields`
   variable.
 * Currently, on line 731, the code looks like this:
 *     ```wp-block-code
       if(is_array($custom_fields)){    foreach ($custom_fields as $field => $value) {                if(in_array($field, $disallowed) ){            $json_api->error("This meta_key '".$field."' is not allowed.");        }        if ($field == 'cookie')            continue;        if (is_array($value)) {            $values = array_map('trim', $values);        } else {            $values = trim($value);        }        if(!in_array($field, $disallowed) ){            foreach($disallowed as $d){                $field = str_replace($d, 'disallowed', $field);             }            $result[$field]['updated'] = update_user_meta($user_id, $field, $values);        }    }}
       ```
   
 * However, if the `$custom_fields` variable is not initialized, an error occurs:
 * >  Undefined variable $custom_fields in C:\wordpress\wp-content\plugins\json-
   > api-user\controllers\User.php:731
 * To prevent this, I suggest adding a check for the existence of the `$custom_fields`
   variable. The modified code would look like this:
 *     ```wp-block-code
       if(!empty($custom_fields) && is_array($custom_fields)){    foreach ($custom_fields as $field => $value) {                if(in_array($field, $disallowed) ){            $json_api->error("This meta_key '".$field."' is not allowed.");        }        if ($field == 'cookie')            continue;        if (is_array($value)) {            $values = array_map('trim', $values);        } else {            $values = trim($value);        }        if(!in_array($field, $disallowed) ){            foreach($disallowed as $d){                $field = str_replace($d, 'disallowed', $field);             }            $result[$field]['updated'] = update_user_meta($user_id, $field, $values);        }    }}
       ```
   
 * A similar adjustment is also needed on line 198:
 *     ```wp-block-code
       if (!empty($custom_fields) && is_array($custom_fields)) {
       ```
   
 * Please consider implementing these changes to avoid potential errors.
 * Thank you!

Viewing 1 replies (of 1 total)

 *  Plugin Author [Ali Qureshi](https://wordpress.org/support/users/parorrey/)
 * (@parorrey)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/fix-for-undefined-variable-custom_fields-in-json_api_user_controller-class/#post-18039036)
 * Updated, Thanks.

Viewing 1 replies (of 1 total)

The topic ‘Fix for Undefined Variable $custom_fields in JSON_API_User_Controller
Class’ is closed to new replies.

 * ![](https://ps.w.org/json-api-user/assets/icon-256x256.png?rev=1965790)
 * [JSON API User](https://wordpress.org/plugins/json-api-user/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/json-api-user/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/json-api-user/)
 * [Active Topics](https://wordpress.org/support/plugin/json-api-user/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/json-api-user/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/json-api-user/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [Ali Qureshi](https://wordpress.org/support/users/parorrey/)
 * Last activity: [1 year, 8 months ago](https://wordpress.org/support/topic/fix-for-undefined-variable-custom_fields-in-json_api_user_controller-class/#post-18039036)
 * Status: resolved