├── LICENSE ├── README.md ├── functions.php ├── inc └── js-wp-editor.php └── js ├── js-wp-editor.js └── js-wp-editor.min.js /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Ante Primorac 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | JavaScript WordPress editor 2 | ============ 3 | 4 | This can be used to add WordPress editor(php: wp_editor() - http://codex.wordpress.org/Function_Reference/wp_editor) with jQuery. Maybe after click, ajax success, etc. 5 | 6 | **Version:** 1.1 7 | **Requires:** WordPress 3.9+ 8 | 9 | ####If you need support for previous versions, you can use the version 1.0 10 | 11 | ###Instalation: 12 | 1. Copy into theme dir: 13 | - inc folder 14 | - js folder 15 | 2. Add code from functions.php to your theme functions.php 16 | 17 | ###Usage: 18 | 1. If you don't have any other WordPress editor on page, you must call js_wp_editor() function. 19 | 2. Add textarea element. Textarea must have id attribute. 20 | 3. Call jQuery plugin: 21 | ``` 22 | jQuery('textarea').wp_editor(); 23 | ``` 24 | 25 | ###Changelog: 26 | * ####1.1: 27 | - Support for WordPress 3.9+ only 28 | 29 | * ####1.0: 30 | - Initial version 31 | -------------------------------------------------------------------------------- /functions.php: -------------------------------------------------------------------------------- 1 | $post 52 | ) ); 53 | } 54 | 55 | _WP_Editors::editor_settings( 'apid', $set ); 56 | 57 | $ap_vars = array( 58 | 'url' => get_home_url(), 59 | 'includes_url' => includes_url() 60 | ); 61 | 62 | wp_register_script( 'ap_wpeditor_init', get_template_directory_uri() . '/js/js-wp-editor.js', array( 'jquery' ), '1.1', true ); 63 | wp_localize_script( 'ap_wpeditor_init', 'ap_vars', $ap_vars ); 64 | wp_enqueue_script( 'ap_wpeditor_init' ); 65 | } -------------------------------------------------------------------------------- /js/js-wp-editor.js: -------------------------------------------------------------------------------- 1 | /* 2 | * JavaScript Wordpress editor 3 | * Author: Ante Primorac 4 | * Author URI: http://anteprimorac.from.hr 5 | * Version: 1.1 6 | * License: 7 | * Copyright (c) 2013 Ante Primorac 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * Usage: 26 | * server side(WP): 27 | * js_wp_editor( $settings ); 28 | * client side(jQuery): 29 | * $('textarea').wp_editor( options ); 30 | */ 31 | 32 | ;(function( $, window ) { 33 | $.fn.wp_editor = function( options ) { 34 | 35 | if( !$(this).is('textarea') ) 36 | console.warn('Element must be a textarea'); 37 | 38 | if( typeof tinyMCEPreInit == 'undefined' || typeof QTags == 'undefined' || typeof ap_vars == 'undefined' ) 39 | console.warn('js_wp_editor( $settings ); must be loaded'); 40 | 41 | if( !$(this).is('textarea') || typeof tinyMCEPreInit == 'undefined' || typeof QTags == 'undefined' || typeof ap_vars == 'undefined' ) 42 | return this; 43 | 44 | var default_options = { 45 | 'mode': 'html', 46 | 'mceInit' : { 47 | "theme": "modern", 48 | "skin": "lightgray", 49 | "language": "en", 50 | "formats": { 51 | "alignleft": [ 52 | { 53 | "selector": "p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li", 54 | "styles": {"textAlign":"left"}, 55 | "deep": false, 56 | "remove": "none" 57 | }, 58 | { 59 | "selector": "img,table,dl.wp-caption", 60 | "classes": ["alignleft"], 61 | "deep":false, 62 | "remove":"none" 63 | } 64 | ], 65 | "aligncenter": [ 66 | { 67 | "selector": "p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li", 68 | "styles": {"textAlign":"center"}, 69 | "deep": false, 70 | "remove": "none" 71 | }, 72 | { 73 | "selector": "img,table,dl.wp-caption", 74 | "classes": ["aligncenter"], 75 | "deep": false, 76 | "remove": "none" 77 | } 78 | ], 79 | "alignright": [ 80 | { 81 | "selector": "p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li", 82 | "styles": {"textAlign":"right"}, 83 | "deep": false, 84 | "remove": "none" 85 | }, 86 | { 87 | "selector": "img,table,dl.wp-caption", 88 | "classes": ["alignright"], 89 | "deep": false, 90 | "remove": "none" 91 | } 92 | ], 93 | "strikethrough": {"inline":"del","deep":true,"split":true} 94 | }, 95 | "relative_urls": false, 96 | "remove_script_host": false, 97 | "convert_urls": false, 98 | "browser_spellcheck": true, 99 | "fix_list_elements": true, 100 | "entities": "38,amp,60,lt,62,gt", 101 | "entity_encoding": "raw", 102 | "keep_styles": false, 103 | "paste_webkit_styles": "font-weight font-style color", 104 | "preview_styles": "font-family font-size font-weight font-style text-decoration text-transform", 105 | "wpeditimage_disable_captions": false, 106 | "wpeditimage_html5_captions": false, 107 | "plugins": "charmap,hr,media,paste,tabfocus,textcolor,fullscreen,wordpress,wpeditimage,wpgallery,wplink,wpdialogs,wpview,image", 108 | "content_css": ap_vars.includes_url + "css/dashicons.css?ver=3.9," + ap_vars.includes_url + "js/mediaelement/mediaelementplayer.min.css?ver=3.9," + ap_vars.includes_url + "js/mediaelement/wp-mediaelement.css?ver=3.9," + ap_vars.includes_url + "js/tinymce/skins/wordpress/wp-content.css?ver=3.9", 109 | "selector": "#apid", 110 | "resize": "vertical", 111 | "menubar": false, 112 | "wpautop": true, 113 | "indent": false, 114 | "toolbar1": "bold,italic,strikethrough,bullist,numlist,blockquote,hr,alignleft,aligncenter,alignright,link,unlink,wp_more,spellchecker,fullscreen,wp_adv", 115 | "toolbar2": "formatselect,underline,alignjustify,forecolor,pastetext,removeformat,charmap,outdent,indent,undo,redo,wp_help", 116 | "toolbar3": "", 117 | "toolbar4": "", 118 | "tabfocus_elements": ":prev,:next", 119 | "body_class": "apid" 120 | } 121 | }, id_regexp = new RegExp('apid', 'g'); 122 | 123 | if(tinyMCEPreInit.mceInit['apid']) { 124 | default_options.mceInit = tinyMCEPreInit.mceInit['apid']; 125 | } 126 | 127 | var options = $.extend(true, default_options, options); 128 | 129 | return this.each(function() { 130 | 131 | if( !$(this).is('textarea') ) 132 | console.warn('Element must be a textarea'); 133 | else { 134 | var current_id = $(this).attr('id'); 135 | $.each( options.mceInit, function( key, value ) { 136 | if( $.type( value ) == 'string' ) 137 | options.mceInit[key] = value.replace(id_regexp, current_id); 138 | } ); 139 | options.mode = options.mode == 'tmce' ? 'tmce' : 'html'; 140 | 141 | tinyMCEPreInit.mceInit[current_id] = options.mceInit; 142 | 143 | $(this).addClass('wp-editor-area').show(); 144 | var self = this; 145 | if( $(this).closest('.wp-editor-wrap').length ) { 146 | var parent_el = $(this).closest('.wp-editor-wrap').parent(); 147 | $(this).closest('.wp-editor-wrap').before($(this).clone()); 148 | $(this).closest('.wp-editor-wrap').remove(); 149 | self = parent_el.find('textarea[id="' + current_id + '"]'); 150 | } 151 | 152 | var wrap = $('
'), 153 | editor_tools = $(''), 154 | editor_tabs = $(''), 155 | switch_editor_html = $('Text'), 156 | switch_editor_tmce = $('Visual'), 157 | media_buttons = $(''), 158 | insert_media_button = $(' Add Media'), 159 | editor_container = $(''), 160 | content_css = /*Object.prototype.hasOwnProperty.call(tinyMCEPreInit.mceInit[current_id], 'content_css') ? tinyMCEPreInit.mceInit[current_id]['content_css'].split(',') :*/ false; 161 | 162 | insert_media_button.appendTo(media_buttons); 163 | media_buttons.appendTo(editor_tools); 164 | 165 | switch_editor_html.appendTo(editor_tabs); 166 | switch_editor_tmce.appendTo(editor_tabs); 167 | editor_tabs.appendTo(editor_tools); 168 | 169 | editor_tools.appendTo(wrap); 170 | editor_container.appendTo(wrap); 171 | 172 | editor_container.append($(self).clone().addClass('wp-editor-area')); 173 | 174 | if( content_css != false ) 175 | $.each( content_css, function() { 176 | if( ! $('link[href="' + this + '"]').length ) 177 | $(self).before(''); 178 | } ); 179 | 180 | $(self).before(''); 181 | 182 | $(self).before(wrap); 183 | $(self).remove(); 184 | 185 | new QTags(current_id); 186 | QTags._buttonsInit(); 187 | switchEditors.go(current_id, options.mode); 188 | 189 | $(wrap).on( 'click', '.insert-media', function( event ) { 190 | var elem = $( event.currentTarget ), 191 | editor = elem.data('editor'), 192 | options = { 193 | frame: 'post', 194 | state: 'insert', 195 | title: wp.media.view.l10n.addMedia, 196 | multiple: true 197 | }; 198 | 199 | event.preventDefault(); 200 | 201 | elem.blur(); 202 | 203 | if ( elem.hasClass( 'gallery' ) ) { 204 | options.state = 'gallery'; 205 | options.title = wp.media.view.l10n.createGalleryTitle; 206 | } 207 | 208 | wp.media.editor.open( editor, options ); 209 | }); 210 | } 211 | }); 212 | } 213 | })( jQuery, window ) -------------------------------------------------------------------------------- /js/js-wp-editor.min.js: -------------------------------------------------------------------------------- 1 | /* 2 | * JavaScript Wordpress editor 3 | * Author: Ante Primorac 4 | * Author URI: http://anteprimorac.from.hr 5 | * Version: 1.1 6 | * License: 7 | * Copyright (c) 2013 Ante Primorac 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * Usage: 26 | * server side(WP): 27 | * js_wp_editor( $settings ); 28 | * client side(jQuery): 29 | * $('textarea').wp_editor( options ); 30 | */ 31 | 32 | !function(e,t){e.fn.wp_editor=function(t){if(e(this).is("textarea")||console.warn("Element must be a textarea"),("undefined"==typeof tinyMCEPreInit||"undefined"==typeof QTags||"undefined"==typeof ap_vars)&&console.warn("js_wp_editor( $settings ); must be loaded"),!e(this).is("textarea")||"undefined"==typeof tinyMCEPreInit||"undefined"==typeof QTags||"undefined"==typeof ap_vars)return this;var i={mode:"html",mceInit:{theme:"modern",skin:"lightgray",language:"en",formats:{alignleft:[{selector:"p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li",styles:{textAlign:"left"},deep:!1,remove:"none"},{selector:"img,table,dl.wp-caption",classes:["alignleft"],deep:!1,remove:"none"}],aligncenter:[{selector:"p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li",styles:{textAlign:"center"},deep:!1,remove:"none"},{selector:"img,table,dl.wp-caption",classes:["aligncenter"],deep:!1,remove:"none"}],alignright:[{selector:"p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li",styles:{textAlign:"right"},deep:!1,remove:"none"},{selector:"img,table,dl.wp-caption",classes:["alignright"],deep:!1,remove:"none"}],strikethrough:{inline:"del",deep:!0,split:!0}},relative_urls:!1,remove_script_host:!1,convert_urls:!1,browser_spellcheck:!0,fix_list_elements:!0,entities:"38,amp,60,lt,62,gt",entity_encoding:"raw",keep_styles:!1,paste_webkit_styles:"font-weight font-style color",preview_styles:"font-family font-size font-weight font-style text-decoration text-transform",wpeditimage_disable_captions:!1,wpeditimage_html5_captions:!1,plugins:"charmap,hr,media,paste,tabfocus,textcolor,fullscreen,wordpress,wpeditimage,wpgallery,wplink,wpdialogs,wpview,image",content_css:ap_vars.includes_url+"css/dashicons.css?ver=3.9,"+ap_vars.includes_url+"js/mediaelement/mediaelementplayer.min.css?ver=3.9,"+ap_vars.includes_url+"js/mediaelement/wp-mediaelement.css?ver=3.9,"+ap_vars.includes_url+"js/tinymce/skins/wordpress/wp-content.css?ver=3.9",selector:"#apid",resize:"vertical",menubar:!1,wpautop:!0,indent:!1,toolbar1:"bold,italic,strikethrough,bullist,numlist,blockquote,hr,alignleft,aligncenter,alignright,link,unlink,wp_more,spellchecker,fullscreen,wp_adv",toolbar2:"formatselect,underline,alignjustify,forecolor,pastetext,removeformat,charmap,outdent,indent,undo,redo,wp_help",toolbar3:"",toolbar4:"",tabfocus_elements:":prev,:next",body_class:"apid"}},s=new RegExp("apid","g");tinyMCEPreInit.mceInit.apid&&(i.mceInit=tinyMCEPreInit.mceInit.apid);var t=e.extend(!0,i,t);return this.each(function(){if(e(this).is("textarea")){var i=e(this).attr("id");e.each(t.mceInit,function(a,n){"string"==e.type(n)&&(t.mceInit[a]=n.replace(s,i))}),t.mode="tmce"==t.mode?"tmce":"html",tinyMCEPreInit.mceInit[i]=t.mceInit,e(this).addClass("wp-editor-area").show();var a=this;if(e(this).closest(".wp-editor-wrap").length){var n=e(this).closest(".wp-editor-wrap").parent();e(this).closest(".wp-editor-wrap").before(e(this).clone()),e(this).closest(".wp-editor-wrap").remove(),a=n.find('textarea[id="'+i+'"]')}var r=e(''),l=e(''),o=e(''),d=e('Text'),p=e('Visual'),c=e(''),m=e(' Add Media'),h=e(''),w=!1;m.appendTo(c),c.appendTo(l),d.appendTo(o),p.appendTo(o),o.appendTo(l),l.appendTo(r),h.appendTo(r),h.append(e(a).clone().addClass("wp-editor-area")),0!=w&&e.each(w,function(){e('link[href="'+this+'"]').length||e(a).before('')}),e(a).before(''),e(a).before(r),e(a).remove(),new QTags(i),QTags._buttonsInit(),switchEditors.go(i,t.mode),e(r).on("click",".insert-media",function(t){var i=e(t.currentTarget),s=i.data("editor"),a={frame:"post",state:"insert",title:wp.media.view.l10n.addMedia,multiple:!0};t.preventDefault(),i.blur(),i.hasClass("gallery")&&(a.state="gallery",a.title=wp.media.view.l10n.createGalleryTitle),wp.media.editor.open(s,a)})}else console.warn("Element must be a textarea")})}}(jQuery,window); --------------------------------------------------------------------------------