Support

Account

Home Forums ACF PRO Getting data from JSON – ACF Plugin

Solved

Getting data from JSON – ACF Plugin

    • Arata

    • November 28, 2018 at 3:14 am

    Is there anyway to get data for ACF fields from JSON?

    I mean for example you have a new post where you have your ACF fields there, how can you fill those fields by giving the JSON url to one of the ACF fields and then it gets data and put it on the fields of that post? On the first pictures you can see the field were the url of JSON goes in and on the second images you can see the ACF fields that JSON data goes there and if we have problems with JSON we can write it manually.

    THE PICTURES ARE JUST MOCKUPS

    The ACF fields that JSON data goes in and if we don't JSON we're able to write in manually

    I know that I need to use ACF custom fields types but I don’t know how I can grab data from JSON and put them on ACF Fields!

    It would be great if you guys can make a really really simple example for this one! 🙂 thx a lot

    Sorry if this question was answered before, I couldn’t find any results so I’m asking it.

    • flipty

    • December 14, 2018 at 6:10 am

    I think am attempting to do something somewhat similar, at least behind-the-scenes.

    I have an ACF select field that needs its options to be populated via JSON from an external URL’s API return. I have had no luck finding that kind of functionality in the bazillion searches I’ve tried. I’ll follow this thread in case anyone comes up with something.

    • Arata

    • January 3, 2019 at 5:09 pm

    any ideas ? :)))

    • flipty

    • January 4, 2019 at 1:42 am

    Unfortunately I was unable to come up with a solution so I had to make a workaround. Ideally I still need this to work so I will be trying to figure something out.

    There could theoretically be a way to manipulate the custom field’s acf-json file via an automated PHP function like a chron job or something but that will still require the WP admin to sync the fields and do a bunch of other things that are far too complicated for the task I’m trying to accomplish.

    • Arata

    • January 4, 2019 at 2:56 am

    @flipty Well I came up with this solution that you can put JSON data into a field and then on theme file decode it and use it. (the decoded data won’t be in mysql) but I prefer to not put the JSON data directly because there would be lot of data that I won’t use it and also I prefer to put them into mysql as well.

  • It would depend on the complexity of the fields involved.

    Could something like this be done? Yes.

    The basics would be to:
    1) Created an acf/save_post filter
    2) Check to see if the radio button is set to JSON
    3) Get the value of the JSON field
    4) decode the JSON, Import the data using update_field() calls using field keys
    5) Update the radio to “Manual”

    The problem here is in the importing, depending on your field complexity and that you need to use field keys because I’m assuming that this is for a new post that does not have values in all the other fields already.

    • Arata

    • January 4, 2019 at 7:42 pm

    @hube2 Thanks for the answer! I could understand somethings but still need to think more I guess.

    Is it possible to give a really simple example since I’m still confused how to work with update_field with field keys.

    something like this:
    getting title and body data from here and then decode it and then putting it into the “title_field” and “body_field” fields when the publish button has been clicked.

    also the answer that you said is it gonna be into a separate plugin for ACF or also can be used into theme files?

  • A really simple example isn’t really possible because there are a lot of moving parts, I might be able to give a better outline.

    
    // create an acf/save_post action
    add_action('acf/acf_save_post', 'import_json_for_field_group_XX', 2);
    function import_json_for_field_group_XX($post_id) {
      // first to a check of the post type
      if (get_post_type($post_id)) != 'your-post-type') {
        // not our post type
        return;
      }
      // check the radio field to see if it is set to the manual value
      if (get_field('your-radio-field-name', $post_id) == 'manual') {
        // not doing import
        return;
      }
      
      // you may need to set up a field name => key reference array
      // when importing you need to use the field keys
      // either that of the file you are importing from
      // must use the field keys in the JSON
      $fields = array(
        'field-name-1' => 'field_123456',
        'field-name-1' => 'field_234567',
        'field-name-1' => 'field_345678',
        'field-name-1' => 'field_456789'
      );
      
      // doing an import
      // get the JSON field
      $json = get_field('json-input-field', $post_id);
    
      // at this point I'm not sure what to do
      // it depends on where the json file is stored
      // you need to use PHP and maybe some WP functions
      // find, open and get the contents of the file
      // and store the content is a variable, I'll use $contents
    
      $values = json_decode($contents, true);
      
      // loop thought the fields in $values and update them
      if ($values) {
        foreach ($values as $field => $value) {
          // update the field
          // note that I am using the field key reference array I created above
          // if you are importing with field keys this would not be needed
          if (isset($fields[$field])) {
            update_field($fields[$field], $value, $post_id);
          }
        } // end foreach $value
      } // end if $values
    
      // finally alter the radio field to manual
      update_field(('your-radio-field-name', 'manual', $post_id);
    }
    
    • Arata

    • January 6, 2019 at 6:04 pm

    @hube2 Thanks for the answer!
    I’ve tried to mess around with it and I was thinking about the part that you said how and where the JSON is stored and I came up with this:

    $json_url = get_field('field_of_json_url', $post_id);
        
     $request = wp_remote_get( $json_url );
    if( is_wp_error( $request ) ) {
            return false;
        }
    
        $contents = wp_remote_retrieve_body( $request );

    but now the problem I have is where and how I have to put and run the code you gave me? I’ve tried a lot but nothing happened. I’ve event read the update_field() document over and over but couldn’t understand it. 🙁

    • Arata

    • January 6, 2019 at 10:01 pm

    @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');
           }
Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.