{"id":47,"date":"2024-02-28T02:07:53","date_gmt":"2024-02-28T02:07:53","guid":{"rendered":"https:\/\/kwwd.co.uk\/blog\/?p=47"},"modified":"2024-02-28T02:09:05","modified_gmt":"2024-02-28T02:09:05","slug":"add-your-own-code-formatting-in-the-classic-editor-with-tinymce","status":"publish","type":"post","link":"https:\/\/www.kwwd.co.uk\/blog\/add-your-own-code-formatting-in-the-classic-editor-with-tinymce\/","title":{"rendered":"Add Your Own Code Formatting In The Classic Editor With TinyMCE"},"content":{"rendered":"<p>If you want to add custom formatting to your content but don&#8217;t want to have to go into the HTML view and manually type it each time, this code snippet is for you!<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-48\" src=\"https:\/\/kwwd.co.uk\/blog\/wp-content\/uploads\/2024\/02\/firefox_Q0Axs2g51n.png\" alt=\"TinyMCE Code Example\" width=\"852\" height=\"398\" \/><\/p>\n<p>Using the following code snippets you&#8217;ll be able to create your own button for the TinyMCE editor and add your &lt;pre&gt;&lt;code&gt; tags including associated classes to style for the language of your choice<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-49\" src=\"https:\/\/kwwd.co.uk\/blog\/wp-content\/uploads\/2024\/02\/firefox_8uu4SwaIYR.png\" alt=\"Code Insert drop down menu\" width=\"264\" height=\"387\" \/><\/p>\n<p>Choosing the above &#8220;PHP with Line Numbers&#8221; will insert the following code where your cursor is:<\/p>\n<pre><code class=\"language-html\">&lt;pre&gt;&lt;code class=\"language-php line-numbers\"&gt;\/\/ Code&lt;\/code&gt;&lt;\/pre&gt;<\/code><\/pre>\n<p>You can then replace the &#8220;\/\/code&#8221; text with your own custom code.<\/p>\n<p>So, how do we achieve this?<\/p>\n<p>In your functions.php file add the following code which will create a new TinyMCE button with an id of &#8220;codeinsert&#8221;<\/p>\n<pre><code class=\"language-php line-numbers\">\r\n\/** TINYMCE STUFF **\/\r\n\r\nadd_action( 'init', 'my_custom_tinymce_init' );\r\n\r\n\/\/ Only Run This If We' have a user with a role that can edit posts\r\nfunction my_custom_tinymce_init() {\r\n  if ( ! current_user_can( 'edit_posts' ) &amp;&amp; ! current_user_can( 'edit_pages' ) ) {\r\n    return;\r\n  }\r\n\r\n  add_filter( 'mce_external_plugins', 'my_custom_tinymce_plugins' );\r\n  add_filter( 'mce_buttons', 'my_custom_tinymce_buttons' );\r\n}\r\n\r\n\/\/initialise our javascript code and load it\r\nfunction my_custom_tinymce_plugins( $plugins ) {\r\n  $plugins['codeinsert'] = esc_url(get_stylesheet_directory_uri()) . '\/js\/my-tinymce.js'; \/\/ Replace with your JS file path\r\n  return $plugins;\r\n}\r\n\r\n\/\/ Add the button to the beginning of the toolbar\r\nfunction my_custom_tinymce_buttons( $buttons ) {\r\n  array_unshift( $buttons, 'codeinsert' ); \r\n  return $buttons;\r\n}\r\n\/** END TinyMCE Code **\/\r\n<\/code><\/pre>\n<p>We then need to create the code that will create the dropdown menu. In the above example, our child theme has a folder called &#8220;js&#8221; which contains a javascript file called &#8220;my-tinymce.js&#8221;. This JavaScript file contains all of the styles we will insert from the dropdown:<\/p>\n<pre><code class=\"language-html line-numbers\">(function () {\r\ntinymce.PluginManager.add('codeinsert', function (editor) {\r\neditor.addButton('codeinsert', {\r\ntype: 'listbox',\r\ntext: 'Insert Code',\r\nicon: false,\r\nonselect: function (e) {\r\neditor.insertContent(this.value());\r\n},\r\nvalues: [\r\n{ text: 'PHP with Line Numbers', value: '&lt;pre&gt;&lt;code class=\"language-php line-numbers\"&gt;\/\/ Code&lt;\/code&gt;&lt;\/pre&gt;' },\r\n{ text: 'CSS with Line Numbers', value: '&lt;pre&gt;&lt;code class=\"language-css line-numbers\"&gt;\/\/ Code&lt;\/code&gt;&lt;\/pre&gt;' },\r\n{ text: 'SQL with Line Numbers', value: '&lt;pre&gt;&lt;code class=\"language-sql line-numbers\"&gt;\/\/ Code&lt;\/code&gt;&lt;\/pre&gt;' },\r\n{ text: 'HTML with Line Numbers', value: '&lt;pre&gt;&lt;code class=\"language-html line-numbers\"&gt;\/\/ Code&lt;\/code&gt;&lt;\/pre&gt;' },\r\n{ text: 'Plain PHP', value: '&lt;pre&gt;&lt;code class=\"language-php\"&gt;\/\/ Code&lt;\/code&gt;&lt;\/pre&gt;' },\r\n{ text: 'Plain CSS', value: '&lt;pre&gt;&lt;code class=\"language-css\"&gt;\/\/ Code&lt;\/code&gt;&lt;\/pre&gt;' },\r\n{ text: 'Plain SQL', value: '&lt;pre&gt;&lt;code class=\"language-sql\"&gt;\/\/ Code&lt;\/code&gt;&lt;\/pre&gt;' },\r\n{ text: 'Plain HTML', value: '&lt;pre&gt;&lt;code class=\"language-html\"&gt;\/\/ Code&lt;\/code&gt;&lt;\/pre&gt;' },\r\n\/\/ Add more code options as needed\r\n]\r\n});\r\n});\r\n})();<\/code><\/pre>\n<p>Note that for the above code to function correctly you MUST have the <a href=\"https:\/\/wordpress.org\/plugins\/classic-editor\/\">Classic Editor plugin<\/a> enabled &#8211; this will remove all Guttenberg block functionality from the Posts\/Page edit pages.<\/p>\n<p>The above code allows you to add styles into the editor so you can style content that will be formatted by the <a href=\"https:\/\/prismjs.com\/download.html#themes=prism&amp;languages=markup+css+clike+javascript\">Prism.js code snippet styling framework<\/a>, although you can amend the &#8220;values&#8221; in the JavaScript file to add any valid html to the editor.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you want to add custom formatting to your content but don&#8217;t want to have to go into the HTML view and manually type it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[19,16,15,9,17,18,12],"class_list":["post-47","post","type-post","status-publish","format-standard","hentry","category-wordpress","tag-classic-editor","tag-formatting","tag-javascript","tag-php","tag-prism-js","tag-tinymce","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts\/47","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=47"}],"version-history":[{"count":0,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts\/47\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=47"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=47"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}