Support

Account

Home Forums ACF PRO Getting data from JSON – ACF Plugin Reply To: Getting data from JSON – ACF Plugin

  • @hube2 first of all thanks a lot!!
    and with a little more thinking I came up with this solution😅, not sure if it’s good but at least it works! also I would be happy if you could tell me they ways to improve it more.

    function acf_json_ipm($json_acf_url_field, $acf_radio_field) {
            
            //Check if data field is set to Manual
            if (get_field($json_acf_url_field) == 'manual') {
                // Stop importing from JSON
                return;
            }
            
                //Make a request
                $json_request = wp_remote_get( $json_acf_url_field );
                if( is_wp_error( $json_request ) ) {
                    return false; // Bail early
                }
                
                // Get JSON and Decode
                $json_body = wp_remote_retrieve_body( $json_request );
                $json_data = json_decode( $json_body );
               
                // Import values from JSON
                $values = array (
                    
                    'field_1234' => $json_data->value1, 
                    'field_5678' => array (
                        'field_8901' => $json_data->value2, 
                        'field_2345' => $json_data->value3, 
                        'field_6789' => $json_data->value4 
                    ),
                    'field_0123' => $json_data->value5, 
                );
    
                //Update Group field and save1
                update_field('acf_group_field_key', $values);
                
                //Put the checkbox to manual
                update_field($acf_radio_field, 'manual');
           }