Advertisement
5ally

TinyMCE 4 + wpForo: Adding the Code Sample plugin

Feb 21st, 2019
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.79 KB | None | 0 0
  1. <?php
  2. /*
  3.  * The following code will add the Code Sample plugin (https://www.tiny.cloud/docs-4x/plugins/codesample/)
  4.  * to the WordPress TinyMCE editor. But first, WordPress does not include the plugin
  5.  * by default, so you need to download it (http://download.tiny.cloud/tinymce/community/tinymce_4.8.0.zip)
  6.  * and upload it to your site - in the ZIP file, it's placed in js/tinymce/plugins/.
  7.  * Note: You should upload the entire "codesample" folder and not just the plugin.min.js or plugin.js file.
  8.  *
  9.  * You can get a custom Prism JS and CSS files via http://prismjs.com/download.html.
  10.  *
  11.  * Tried and tested working on WordPress 5.0.3 with TinyMCE 4.8.0 and wpForo 1.5.5.
  12.  */
  13. ?>
  14. <?php
  15. // Enqueue Prism CSS for use on the front-end. (not in the TinyMCE editor)
  16. add_action( 'wp_enqueue_styles', function(){
  17.     wp_enqueue_style( 'prism-custom', 'URL to the Prism CSS file', [], null );
  18. } );
  19.  
  20. // Enqueue Prism JS for use on the front-end. (not in the TinyMCE editor)
  21. add_action( 'wp_enqueue_scripts', function(){
  22.     wp_enqueue_script( 'prism-custom', 'URL to the Prism JS file', [], null );
  23. } );
  24.  
  25. // Add the external codesample TinyMCE plugin.
  26. add_filter( 'mce_external_plugins', function( $plugins ){
  27.     $plugins['codesample'] = 'URL to the TinyMCE codesample/plugin.min.js';
  28.     return $plugins;
  29. }, 16 ); // the priority should be at least 16 for the code to work with wpForo
  30.  
  31. // Add the codesample button to TinyMCE - mce_buttons doesn't work with wpForo.
  32. add_filter( 'wp_editor_settings', function( $settings ){
  33.     if ( empty( $settings['tinymce'] ) || ! is_array( $settings['tinymce'] ) )
  34.         $settings['tinymce'] = [];
  35.  
  36.     if ( isset( $settings['tinymce']['toolbar1'] ) )
  37.         $settings['tinymce']['toolbar1'] .= ',codesample';
  38.     else
  39.         $settings['tinymce']['toolbar1'] = 'codesample';
  40.  
  41.     return $settings;
  42. } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement