TinyMCE Replace Parent Node

I’m developping a WordPress plugin which adds a button to the TinyMCE editor. When that button is clicked the desired behaviour should be:
1. If the selected text is not part of a short (or is the short) then the shortcode should be inserted into the DOM.
2 If not, the shortcode should be replaced with the new one.

The first requirement is fufilled, but the second not. When inserted, it show up in the visual editor but it not saved or displayed when saving or switching to text. This leads me to believe that im not directly editting the object underneath the editor, only that which is displayed. What is the correct way to go about it. Please see the following function:

Read More
...

  insertContent: function(){
    var atts  = '';
    var node  = tinyMCE.activeEditor.selection.getNode();
    var edit  = $(node).is('div.wp-arv-faq');

    jQuery('./$p/-sc-input').each(function() {
      atts = atts + jQuery(this).attr('name') + '="' + $(this).val() + "" ";
    });

    var caption   = 'F.A.Q. ' + $('./$p/-sc-input[name="tax"] option:selected').text();
    var style     = $('./$p/-sc-input[name="style"] option:selected').text();
    atts += ' caption="' + caption + ' (' + style + ')"';

    if (edit){
      $(node).empty();

      tinyMCE.activeEditor.selection.setContent('[arv-faq ' + atts + '][/arv-faq]');      




    /$p/faqMCE.closeModal();
  },
...

Please note, that the ‘/$p/’ is unique to my build process and should be ignored. Thanks in advance

Related posts

1 comment

  1. From your question it is not perfectly clear what you are trying to do, but i guess you are trying to insert html code into the editor.

    Be sure to have the selection set right after calling $(node).empty();

    Note: All your tags and attributes need to be configured as allowed tags and attributes using valid_elements and valid_children configuration paramters.

Comments are closed.