Home Forums Backend Issues (wp-admin) Fill field with remote data?


Fill field with remote data?

  • I have a field that I’d like to fill with data that I fetch from a remote URL. For example, one field will have an ID, and I want to fill another field with data I need to fetch from a remote API.

    For example: hxxps://

    I need to parse the json and pluck a value from it and then stuff that in a field. Is this possible? Not even sure where to start looking…



  • To do this on the server you would need to use one of the filters that ACF provides acf/load_field, acf/prepeare_field, or acf/load_value and the you would need to make a CURL or some other type of request to the API to get the result an populated to value.

    On the client site you would need to use JavaScript to make the request using AJAX and use the ACF JavaScript API to populate the value.

  • @sporklab for a quick and dirty solution to this, I use jQuery and the acf field ID. Here’s an example below where a metabox is added to a certain post type, and when the user enters an ID in a field and fetches data from a remote API, it fills out some ACF fields:

    function my_metabox_render() { ?>
      <p>Enter the ID:</p>
      <input type="text" id="the_id_for_remote_api" />
      <button id="my_import_button" class="button button-primary button-large">Import</button>  
        jQuery("#my_import_button").on("click", function(e) {
          var theID = jQuery("#the_id_for_remote_api").val();
          var apiEndpoint  = "" + theID + ".json?api_key=some_nifty_key";
          jQuery.get(apiEndpoint, function(data) {
    <?php }
    function register_my_metabox() {
            'Example Metabox Title',
    add_action( 'add_meta_boxes', 'register_my_metabox' );

    Like I said, this is just the rough outline. But it works pretty well for the use case you described. Note that I tend to keep the “remote” field separate from ACF, siloed in the metabox, it just simplifies things.

  • @etling – that is pretty cool. Also I like the idea that it’s sort of a one-shot and the user is free to later alter it. Hmmm…

    – I will look at this as well, I need to think about what approach is easiest for the end user (and also think about how often they might want to override, if ever).

  • Not sure if this is the correct usage, but I hit on “acf/update_value” and that seems to be working well. In the function I created, I only do anything if the value comes in empty. So for a new post, if the user has not opted to paste in a custom URL, I see a blank value and then go and fetch and decode the json from vimeo to get the video’s thumbnail URL and then set it.

    Subsequent edits of the page will similarly hit my “is it defined?” check and skip this. If the user wants to force a check for a new thumbnail for some reason they can blank out the URL and hit “submit” and the blank form will trigger another fetch. This seems pretty sane I think?

  • Oh, also just going to post my code in case it’s helpful or if someone wants to yell at me for being wrong:

    	// Fetch vimeo thumbnail on save - takes video ID as input
    	function rf_acf_update_vimeo_thumb( $value, $post_id, $field ) {
    		// only set a new value if the field is empty
    		if ( $value == '') {
    			// get video here
    			$vimeo_video_id = get_field('vimeo_video_id');
    			// fetch thumb url(s)
    			$vimeo_meta = file_get_contents("$vimeo_video_id.json");
    			$vimeo_meta = json_decode($vimeo_meta);
    			$vimeo_thumb = $vimeo_meta[0]->thumbnail_large;
    			// set value
    			$value = $vimeo_thumb;
    			return $value;
    		} else {
    			return $value;
    	add_filter('acf/update_value/name=thumbnail_url', 'rf_acf_update_vimeo_thumb', 10, 3);
Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Fill field with remote data?’ is closed to new replies.