Support

Account

Home Forums ACF PRO insert values into fields programmatically Reply To: insert values into fields programmatically

  • For tinyMCE you need to know the ID of the editor and then you need to call a tinyMCE function to get the content. I’m not completely familiar with everything tinyMCE, but I recently needed to figure out how to get the selected content in a specific field since there are many of them when using ACF.

    This is some of the code I used, but you’ll need to do some digging to figure out how to get all of the content, I was focusing on getting what is selected.

    
    // the editor ID is set by ACF, so you can get this from one of the
    // acf field wrappers
    var $my_editor = 'your-editor-it-here';
    
    if (typeof tinyMCE !== 'undefined') {
      var $editor = false;
      for (i=0; i<tinyMCE.editors.length; i++) {
        if ($my_editor == tinyMCE.editors[i].id) {
          $editor = tinyMCE.editors[i];
        }
      }
      if (!$editor || $editor.hidden) {
        // get content from text area
        var $textarea = document.getElementById($my_editor);
        var $start = $textarea.selectionStart;
        var $end = $textarea.selectionEnd;
        mce_selection = $textarea.value.substring($start, $end);
      } else {
        mce_selection = $editor.selection.getContent();
      }
    }
    

    When I say I don’t know much about tinyMCE, it took me about 4 hours to figure out those 15 or 20 lines of code.

    What you need help with here is how to the reverse, insert content into an editor, and that I can’t really help you with. I was working on making changes to a plugin that already does that part and I was fixing a bug in it that is caused by the existence of multiple editors when using ACF.