Comment ajouter un bouton shortcode à l'éditeur TinyMCE?
-
-
Ajoutez une capture d'écran du résultat que vous souhaitez obtenir.Ce que vous voulezn'estpas clair.Add a screen shot of the result you want to get. It is not clear what you want.
- 0
- 2012-11-13
- fuxia
-
Jepense que vous souhaitez créer unplugin qui ajoute unbouton TinyMCE à l'éditeur quiinsère un shortcode WordPress,non?I think you want to create a plugin that adds a TinyMCE button to the editor that inserts a WordPress shortcode, right?
- 0
- 2012-11-13
- developdaly
-
2 réponses
- votes
-
- 2012-11-13
Pour ajouternotrebouton à l'éditeur TinyMCE,nous devonsfaireplusieurs choses:
- Ajouternotrebouton à labarre d'outils
- Enregistrer unplugin TinyMCE
- Créez ceplug-in TinyMCE quiindique à TinyMCE ce qu'il doitfaire lorsque vous cliquez surnotrebouton.
Étapes 1et 2
Dans ces étapes,nousenregistronsnotreplug-in TinyMCE qui vivra dans unfichierjavascript à
'path/to/shortcode.js'
(voirwpse72394_register_tinymce_plugin()
ci-dessous )// init process for registering our button add_action('init', 'wpse72394_shortcode_button_init'); function wpse72394_shortcode_button_init() { //Abort early if the user will never see TinyMCE if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') && get_user_option('rich_editing') == 'true') return; //Add a callback to regiser our tinymce plugin add_filter("mce_external_plugins", "wpse72394_register_tinymce_plugin"); // Add a callback to add our button to the TinyMCE toolbar add_filter('mce_buttons', 'wpse72394_add_tinymce_button'); } //This callback registers our plug-in function wpse72394_register_tinymce_plugin($plugin_array) { $plugin_array['wpse72394_button'] = 'path/to/shortcode.js'; return $plugin_array; } //This callback adds our button to the toolbar function wpse72394_add_tinymce_button($buttons) { //Add the button ID to the $button array $buttons[] = "wpse72394_button"; return $buttons; }
Étapen ° 3
Nous devonsmaintenant créernotreplug-in TinyMCE. Celaira dans unfichier
'path/to/shortcode.js'
(comme spécifié dans lespremières étapes).jQuery(document).ready(function($) { tinymce.create('tinymce.plugins.wpse72394_plugin', { init : function(ed, url) { // Register command for when button is clicked ed.addCommand('wpse72394_insert_shortcode', function() { selected = tinyMCE.activeEditor.selection.getContent(); if( selected ){ //If text is selected when button is clicked //Wrap shortcode around it. content = '[shortcode]'+selected+'[/shortcode]'; }else{ content = '[shortcode]'; } tinymce.execCommand('mceInsertContent', false, content); }); // Register buttons - trigger above command when clicked ed.addButton('wpse72394_button', {title : 'Insert shortcode', cmd : 'wpse72394_insert_shortcode', image: url + '/path/to/image.png' }); }, }); // Register our TinyMCE plugin // first parameter is the button ID1 // second parameter must match the first parameter of the tinymce.create() function above tinymce.PluginManager.add('wpse72394_button', tinymce.plugins.wpse72394_plugin); });
To add our button to the TinyMCE editor we need to do several things:
- Add our button to the toolbar
- Register a TinyMCE plugin
- Create that TinyMCE plug-in which tells TinyMCE what to do when our button is clicked.
Steps #1 and #2
In these steps we register our TinyMCE plug-in which will live inside a javascript file at
'path/to/shortcode.js'
(seewpse72394_register_tinymce_plugin()
below)// init process for registering our button add_action('init', 'wpse72394_shortcode_button_init'); function wpse72394_shortcode_button_init() { //Abort early if the user will never see TinyMCE if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') && get_user_option('rich_editing') == 'true') return; //Add a callback to regiser our tinymce plugin add_filter("mce_external_plugins", "wpse72394_register_tinymce_plugin"); // Add a callback to add our button to the TinyMCE toolbar add_filter('mce_buttons', 'wpse72394_add_tinymce_button'); } //This callback registers our plug-in function wpse72394_register_tinymce_plugin($plugin_array) { $plugin_array['wpse72394_button'] = 'path/to/shortcode.js'; return $plugin_array; } //This callback adds our button to the toolbar function wpse72394_add_tinymce_button($buttons) { //Add the button ID to the $button array $buttons[] = "wpse72394_button"; return $buttons; }
Step #3
Now we need to create our TinyMCE plug-in. This will go in a file
'path/to/shortcode.js'
(as specified in the early steps).jQuery(document).ready(function($) { tinymce.create('tinymce.plugins.wpse72394_plugin', { init : function(ed, url) { // Register command for when button is clicked ed.addCommand('wpse72394_insert_shortcode', function() { selected = tinyMCE.activeEditor.selection.getContent(); if( selected ){ //If text is selected when button is clicked //Wrap shortcode around it. content = '[shortcode]'+selected+'[/shortcode]'; }else{ content = '[shortcode]'; } tinymce.execCommand('mceInsertContent', false, content); }); // Register buttons - trigger above command when clicked ed.addButton('wpse72394_button', {title : 'Insert shortcode', cmd : 'wpse72394_insert_shortcode', image: url + '/path/to/image.png' }); }, }); // Register our TinyMCE plugin // first parameter is the button ID1 // second parameter must match the first parameter of the tinymce.create() function above tinymce.PluginManager.add('wpse72394_button', tinymce.plugins.wpse72394_plugin); });
-
À l'étape 1,changer le hook `init`en hook` admin_init`pourrait également économiser un légertraitement supplémentaire sur lefront-end.In step 1, changing the `init` hook to the `admin_init` hook could also save some slight extra processing on the front-end.
- 2
- 2016-11-16
- Tim Malone
-
Cela dépend,vouspouvez également avoir TinyMCE sur lefront-end.Mais oui,si cen'est que côté administrateur,`admin_init` seraitpréférable.It depends, you can have TinyMCE on the front-end too. But yes, if this is admin-side only, `admin_init` would be preferable.
- 0
- 2016-11-17
- Stephen Harris
-
Donc `shortcode.js` doit êtreplacéici` wp-includes/js/tinymce/js/shortcode.js`et aussi leboutonn'apas d'icôneSo `shortcode.js` needs to be placed here `wp-includes/js/tinymce/js/shortcode.js` and also button has no icon
- 0
- 2020-08-08
- Jadeye
-
- 2012-11-13
Il y atrop de chosespourmettretoute la réponseici,alors consultez ceguide: http://wp.smashingmagazine.com/2012/05/01/wordpress-shortcodes-complete-guide/
Vous devez créer unfichier Javascript qui agit àpartir dubouton que vousenregistrez via WordPress quiinsère lebouton TinyMCE dans l'éditeur.
There's too much to put the whole answer here so checkout this guide: http://wp.smashingmagazine.com/2012/05/01/wordpress-shortcodes-complete-guide/
You have to create a Javascript file that takes action from the button you register through WordPress that inserts the TinyMCE button into the editor.
-
Utilise query_posts doncpeut-êtrepas lemeilleurexemple vers lequel établir un lien.Uses query_posts so maybe not the best example to link to.
- 4
- 2014-07-11
- Brad Dalton
Comment créer uneicône deplugin dans wordpresspost?Le code queje veuxinsérer dans le code dupluginet apparaîtra dans labarre depublication [wp-admin/post.php].
Comme cetteimage:
Sortie: Sije clique sur l'icône,cela écrit automatiquement
[plugin]
dans le contenu de l'article comme ceci: