Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress: How does 'Insert Read More tag' works?

I want to add a button to tinyMCE editor on new post page. With this toturial I managed to get the button work perfectly, but there is something I couldn't figure out. When you insert a "More" tag, an image will appended to the html with appropriate 'background-image'. See screenshot bellow: more tag

But when you switch to 'Text' mode there is a html comment like this: <!--more-->. enter image description here

I could add the image in the html but on 'Text' mode there is an img tag. enter image description hereenter image description here

I want to have something like this: <!--my-custom-tag-->

How wordpress manage to do this? Or how could I append a custom tag on tinyMCE editor?

like image 489
Pedram Behroozi Avatar asked May 04 '14 12:05

Pedram Behroozi


1 Answers

I found the answer. You need to add BeforeSetContent and PostProcess events on the editor object (As I mentioned earlier, first follow this toturial to add your button):

tinymce.create('tinymce.plugins.MyPlugin', {
    init: function(editor, url) {
        // Code to add the button...

        // Replace tag with image
        editor.on( 'BeforeSetContent', function( e ) {
            if ( e.content ) {
                if ( e.content.indexOf( '<!--my-custom-tag-->' ) !== -1 ) {
                    e.content = e.content.replace( '<!--my-custom-tag-->', '<img src="' + tinymce.Env.transparentSrc + '" ' + 'class="wp-my-custom-tag mce-wp-my-custom-tag" title="My Tag..." data-mce-resize="false" data-mce-placeholder="1" />');
                }
            }
        });
        // Replace image with tag
        editor.on( 'PostProcess', function( e ) {
            if ( e.content ) {
                if ( e.content.indexOf( '<!--my-custom-tag-->' ) !== -1 ) {
                    e.content = e.content.replace( '<!--my-custom-tag-->', '<img src="' + tinymce.Env.transparentSrc + '" ' + 'class="wp-my-custom-tag mce-wp-my-custom-tag" title="My Tag..." data-mce-resize="false" data-mce-placeholder="1" />';
                }
            }
        });

    }
});
like image 159
Pedram Behroozi Avatar answered Nov 28 '22 20:11

Pedram Behroozi