Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Format div in TinyMCE Merges with previous div

I have created a custom format in the editor with

array(
    'title' => 'Tab',
    'block' => 'div',
    'classes' => 'tab',
    'wrapper' => true,
    'exact' => true,
),

I can then use it by selecting some text and choosing my format to get:

<div class="tab">
   <h3>My Content heading 1</h3>
   <p>My first bit of content here...</p>
</div>

So far, so good. However when I select the following text and choose my format it extends the existing div rather than inserting a new one so I end up with:

<div class="tab">
  <h3>My Content heading 1</h3>
  <p>My first bit of content here...</p>
  <h3>My Content heading 2</h3>
  <p>My second bit of content here...</p>
</div>

When what I want is this:

<div class="tab">
  <h3>My Content heading 1</h3>
  <p>My first bit of content here...</p>
</div>
<div class="tab">
  <h3>My Content heading 2</h3>
  <p>My second bit of content here...</p>
</div>

Setting the "exact" parameter when creating the format doesn't seem to change this behaviour when I would expect it to. Help me, please.

like image 685
dark19 Avatar asked Sep 02 '17 23:09

dark19


People also ask

How do you get rid of Tinyed by power on TinyMCE?

Use the branding option to disable the “Powered by Tiny” link displayed in the status bar for product attribution. Important: Product attribution is required for free and open source users. For information on TinyMCE attribution requirements, see: Logo & attribution requirements.

Does TinyMCE support markdown?

By default, it has basic markdown patterns.

Is TinyMCE free for commercial use?

Is TinyMCE free? Yes. The TinyMCE core editor is free to use for commercial and noncommercial purposes.

How do you enter a source code on TinyMCE?

On the menu bar, open View > Source code. On the menu bar, open Tools > Source code. Click the Source code toolbar button.


1 Answers

Add these functions to your theme's 'functions.php' for registering new button in TinyMCE.

// init scripts
add_action( 'after_setup_theme', 'custom_tinymce_theme_setup' );

if ( ! function_exists( 'custom_tinymce_theme_setup' ) ) {
    function custom_tinymce_theme_setup(){
        add_action( 'admin_init', 'custom_tinymce_theme_add_editor_styles' );
        add_action( 'init', 'custom_tinymce_buttons' );
    }
}

// add editor custom css
if ( ! function_exists( 'custom_tinymce_theme_add_editor_styles' ) ) {
    function custom_tinymce_theme_add_editor_styles() {
        add_editor_style( 'custom-editor-style.css' );
    }
}

// add editor button filter
if ( ! function_exists( 'custom_tinymce_buttons' ) ) {
    function custom_tinymce_buttons() {
        if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
            return;
        }
        if ( get_user_option( 'rich_editing' ) !== 'true' ) {
            return;
        }

        add_filter( 'mce_external_plugins', 'custom_tinymce_add_buttons' );
        add_filter( 'mce_buttons', 'custom_tinymce_register_buttons' );
    }
}

// call editor custom js
if ( ! function_exists( 'custom_tinymce_add_buttons' ) ) {
    function custom_tinymce_add_buttons( $plugin_array ) {
        $plugin_array['wrap_tab'] = get_template_directory_uri().'/js/tinymce_buttons.js';
        return $plugin_array;
    }
}

// add button to editor
if ( ! function_exists( 'custom_tinymce_register_buttons' ) ) {
    function custom_tinymce_register_buttons( $buttons ) {
        array_push( $buttons, 'wrap_tab' );
        return $buttons;
    }
}

Then create a new JS file named 'tinymce_buttons.js' under your '[theme]/js' directory and add the following code.

(function() {
    tinymce.PluginManager.add( 'wrap_tab', function( editor, url ) {
        editor.addButton('wrap_tab', {
            title: 'Insert Tab',
            cmd: 'wrap_tab',
            text: 'Tab',
        });

        editor.addCommand('wrap_tab', function() {
            var selected_text = editor.selection.getContent({
                'format': 'html'
            });
            if ( selected_text.length === 0 ) {
                alert( 'Please select text that needs to be wrapped.' );
                return;
            }
            var return_content = '<div class="tab">' + selected_text + '</div>';
            editor.execCommand('mceReplaceContent', false, return_content);
            return;
        });
    });
})();

You can then include the CSS styles - create a new CSS file named 'custom-editor-style.css' under your theme folder with style definitions to your DIV element.

#tinymce .tab{ padding:10px; border: 1px #666 solid; }

Hope this helps and credits goes to https://madebydenis.com/adding-custom-buttons-in-tinymce-editor-in-wordpress/.

like image 180
Outsource WordPress Avatar answered Sep 24 '22 17:09

Outsource WordPress