Skip to content

Instantly share code, notes, and snippets.

@harisrozak
Last active July 21, 2016 07:03
Show Gist options
  • Save harisrozak/78600a7da51f6baeed6a2a489417f44b to your computer and use it in GitHub Desktop.
Save harisrozak/78600a7da51f6baeed6a2a489417f44b to your computer and use it in GitHub Desktop.
WordPress :: wp_editor on modal
<!-- On firefox, you must remove the tinymce on before modal close -->
<script type="text/javascript">
var harisWpEditor;
;(function($) {
harisWpEditor = {
config: {
loadingTime: 350,
loadingText: 'Loading...'
},
notFirstLoad: new Array(),
modalOpen: function(wp_editor_id) {
// remove tinymce on first load
if(! this.notFirstLoad[wp_editor_id]) {
tinymce.EditorManager.execCommand('mceRemoveEditor', true, wp_editor_id);
this.notFirstLoad[wp_editor_id] = true;
}
// show loading
$('#wp-' + wp_editor_id + '-wrap').css('visibility','hidden');
$('<p id="wp-' + wp_editor_id + '-wrap-early-loading">' + this.config.loadingText + '</p>')
.insertBefore('#wp-' + wp_editor_id + '-wrap');
// reinit tinymce
setTimeout(function(){
tinymce.EditorManager.execCommand('mceAddEditor', true, wp_editor_id);
$('#wp-' + wp_editor_id + '-wrap-early-loading').remove();
$('#wp-' + wp_editor_id + '-wrap').css('visibility','visible');
}, this.config.loadingTime);
},
modalClose: function(wp_editor_id) {
if($("#" + wp_editor_id).length && this.notFirstLoad[wp_editor_id]){
tinymce.EditorManager.execCommand('mceRemoveEditor', true, wp_editor_id);
}
},
getContent: function(wp_editor_id) {
var textContent = tinyMCE.get(wp_editor_id).getContent();
return textContent;
}
}
})(jQuery);
function on_modal_show() {
harisWpEditor.modalOpen('modaleditor');
}
function on_before_modal_close() {
harisWpEditor.modalClose('modaleditor');
}
function on_save() {
var textContent = harisWpEditor.getContent('modaleditor');
$('textarea[name="modal_editor"]').val(textContent);
}
</script>
<?php
$content = '';
$id = 'modaleditor';
$settings = array(
'textarea_name' => 'modal_editor',
'media_buttons' => false,
'quicktags' => false,
'editor_height' => 200
);
wp_editor( $content, $id, $settings );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment