Support

Account

Forum Replies Created

  • OK, here’s a better version. You have to find your ACF field, find the textarea within the editor, get that ID, then setContent using TinyMCE JS API:

    
    var description = "Some text!";
    var f = acf.getField("field_5d5231b7e8738");
    var tinyID = f.$el.find("textarea").attr("id");
    var tinyInstance = tinyMCE.editors[tinyID];
    tinyInstance.setContent(description);
    

    Worked well for me, let me know how it goes.

  • @paul_crookell – sort of! What seemed to work for me was to set the value using the ACF JS field API, and then use jQuery to “click” the code view tab. And then the text I was inserting would appear.
    That was good enough for this project so I didn’t dig further – whenever I tried programmatically selecting the wysiwyg view, the value would go blank again.
    My solution, in essence:
    var description = “This text should appear in the TinyMCE textarea!”;
    acf.getField(“field_5d5231b7e8738”).val(description);
    jQuery(“div.acf-field-5d5231b7e8738 button.switch-html”).click();
    Would love to know a more elegant way!

    Ignore all that, I sorted it – updating in one second, hold tight.

  • @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.

Viewing 3 posts - 1 through 3 (of 3 total)