Home › Forums › Backend Issues (wp-admin) › Fill field with remote data? › Reply To: Fill field with remote data?
@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>
<script>
jQuery("#my_import_button").on("click", function(e) {
e.preventDefault();
var theID = jQuery("#the_id_for_remote_api").val();
var apiEndpoint = "https://some.api.com/someendpoint/" + theID + ".json?api_key=some_nifty_key";
jQuery.get(apiEndpoint, function(data) {
console.log(data);
jQuery("input#acf-field_abc123xyz456").val(data.coolInfo);
jQuery("input#acf-field_zxy987qwe123").val(data.sweetData);
});
});
</script>
<?php }
function register_my_metabox() {
add_meta_box(
'my_meta_box_id',
'Example Metabox Title',
'my_metabox_render',
'your_custom_post_type_here',
'side',
'default'
);
}
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.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.