jquery - How do I dynamically load tinymce editors in a wordpress plugin? -
i have searched lot , found many answers question, none of them seem work me.
i making wordpress plugin. when go add/edit page/post there new metabox 2 buttons. 1 deleting editors have been created, works fine. other allows create new editors through bit of jquery , php. issue here html created wp_editor being created , added page fine, tinymce never takes effect. understand because scripts need re-initiated , such, no solution have come across has fixed issue.
here php used generate editor html.
$blocks = get_post_meta( $post_id, '_mbp-blocks', true ); $id = sanitize_title( $name ); $box_html = ""; if( ! is_array( $blocks ) ) $blocks = array(); $blocks[ $id ] = array( 'name' => $name, 'type' => 'editor' ); update_post_meta( $post_id, '_mbp-blocks', $blocks ); $box_html .= '<div id="block_'.$id.'">'; $box_html .= '<p><input id="checked_mbp" type="checkbox" name="checked_mbp[]" value="'.$id.'"><strong>' . $name . '</strong></p>'; ob_start(); wp_editor( get_post_meta( $post_id, '_mbp-' . $id, true ), 'editor_' . $id ); $box_html .= ob_get_contents(); ob_end_clean(); $box_html .= '</div>'; $return_array = array("html" => $box_html, "editorid" => $id); return json_encode($return_array);
and javascript used, minus of fixes have tried.
jquery( function( $ ) { $('#addblock').click(function(e){ e.preventdefault(); var blockname = $("#blockname").val(); var blockpostid = $("#blockpostid").val(); $.ajax({ url: 'http://www.casinovault.com/wp-content/plugins/blockpages/ajax_functions.php', type: 'post', datatype: "json", data: { 'name': blockname, 'action': 'register', 'post_id': blockpostid }, success: function(result){ $('#mbp_blocks').append(result['html']); } }); }); $('#deleteblock').click(function(e){ e.preventdefault(); var blockpostid = $("#blockpostid").val(); confirm('are sure want delete checked blocks?'); var checked_mbp = $('#checked_mbp:checked').map(function(i,n) { return $(n).val(); }).get(); $.ajax({ url: 'http://www.casinovault.com/wp-content/plugins/blockpages/ajax_functions.php', type: 'post', data: { 'action': 'delete', 'delete_mbp[]': checked_mbp, 'post_id': blockpostid }, success: function(result){ var checked = result.split(','); $.each(checked, function() { $('#block_'+this).remove(); }); } }); });
});
i have been trying work adding fixes ajax success.
my problem little more complex had dynamically load form whenever user click on edit button of item.
use following code if have id.
function editorfix( qtid ) { if ( typeof quicktags !== 'undefined' ) { try { if ( typeof qtags.instances[qtid] !== 'undefined' ) { qtags.instances = {}; tinymce.execcommand( 'mceremoveeditor', false, qtid ); } quicktags( tinymcepreinit.qtinit[qtid] ); if ( ! window.wpactiveeditor ) { window.wpactiveeditor = qtid; } switcheditors.go(qtid, 'html'); } catch(e){}; } }
in case don't have id or want show multiple editors
function editorfix() { if ( typeof quicktags !== 'undefined' ) { ( var qtid in tinymcepreinit.qtinit ) { try { if ( typeof qtags.instances[qtid] !== 'undefined' ) { qtags.instances = {}; tinymce.execcommand( 'mceremoveeditor', false, qtid ); } quicktags( tinymcepreinit.qtinit[qtid] ); if ( ! window.wpactiveeditor ) { window.wpactiveeditor = qtid; } // wordpress switch editor switcheditors.go(qtid, 'html'); } catch(e){}; } } }
Comments
Post a Comment