{"id":129,"date":"2013-03-18T19:36:26","date_gmt":"2013-03-18T19:36:26","guid":{"rendered":"http:\/\/www.webninjataylor.com\/library\/?p=129"},"modified":"2014-06-24T21:47:12","modified_gmt":"2014-06-24T21:47:12","slug":"wordpress-custom-styling-via-the-tinymce-editor","status":"publish","type":"post","link":"https:\/\/webninjataylor.com\/library\/wordpress-custom-styling-via-the-tinymce-editor\/","title":{"rendered":"Customize WordPress Content Editor"},"content":{"rendered":"<p>Add this to your theme&#8217;s functions.php file&#8230;<\/p>\n<pre>\/******** ADD CUSTOM STYLES TO TINYMCE EDITOR ********\/\r\nadd_filter('mce_buttons_2', 'my_mce_buttons_2');\r\nfunction my_mce_buttons_2($buttons) {\r\n    array_unshift($buttons, 'styleselect');\r\n    return $buttons;\r\n}\r\nadd_filter('tiny_mce_before_init', 'my_mce_before_init');\r\nfunction my_mce_before_init($settings) {\r\n    $style_formats = array(\r\n        array(\r\n            'title' =&gt; 'Button',\r\n            'selector' =&gt; 'a',\r\n            'classes' =&gt; 'button'\r\n        ),\r\n        array(\r\n            'title' =&gt; 'Callout Box',\r\n            'block' =&gt; 'div',\r\n            'classes' =&gt; 'callout',\r\n            'wrapper' =&gt; true\r\n        ),\r\n        array(\r\n            'title' =&gt; 'Bold Red Text',\r\n            'inline' =&gt; 'span',\r\n            'styles' =&gt; array(\r\n                'color' =&gt; '#f00',\r\n                'fontWeight' =&gt; 'bold'\r\n            )\r\n        )\r\n     );\r\n     $settings['style_formats'] = json_encode($style_formats);\r\n     return $settings;\r\n }\r\n add_action('admin_init', 'add_my_editor_style');\r\n function add_my_editor_style() {\r\n     add_editor_style();\r\n }<\/pre>\n<p><a href=\"http:\/\/alisothegeek.com\/2011\/05\/tinymce-styles-dropdown-wordpress-visual-editor\/\">Full article\u00a0and array reference.<\/a><\/p>\n<p>This is also fairly simple once you know the syntax. All we\u2019re doing is defining the formats, then encoding them as\u00a0JSON so they\u2019re TinyMCE-friendly, then assigning them to the TinyMCE settings.<\/p>\n<p>Let\u2019s break apart the array and look at the different options.<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>title<\/strong>\u00a0[required]<\/td>\n<td>label for this dropdown item<\/td>\n<\/tr>\n<tr>\n<td><strong>selector<\/strong>\u00a0|\u00a0<strong>block<\/strong>\u00a0|<strong>inline<\/strong>\u00a0[required]<\/td>\n<td>\n<ul>\n<li><strong>selector<\/strong>\u00a0limits the style to a specific HTML tag, and will apply the style to an existing tag instead of creating one<\/li>\n<li><strong>block<\/strong>\u00a0creates a new block-level element with the style applied, and will replace the existing block element around the cursor<\/li>\n<li><strong>inline<\/strong>\u00a0creates a new inline element with the style applied, and will wrap whatever is selected in the editor, not replacing any tags<\/li>\n<\/ul>\n<\/td>\n<\/tr>\n<tr>\n<td><strong>classes<\/strong>\u00a0[optional]<\/td>\n<td>space-separated list of classes to apply to the element<\/td>\n<\/tr>\n<tr>\n<td><strong>styles<\/strong>\u00a0[optional]<\/td>\n<td>array of inline styles to apply to the element (two-word attributes, like\u00a0<em>font-weight<\/em>, are written in Javascript-friendly camel case:\u00a0<em>fontWeight<\/em>)<\/td>\n<\/tr>\n<tr>\n<td><strong>attributes<\/strong>\u00a0[optional]<\/td>\n<td>assigns attributes to the element (same syntax as\u00a0<strong>styles<\/strong>)<\/td>\n<\/tr>\n<tr>\n<td><strong>wrapper<\/strong>\u00a0[optional, default = false]<\/td>\n<td>if set to\u00a0<strong>true<\/strong>, creates a new block-level element around any selected block-level elements<\/td>\n<\/tr>\n<tr>\n<td><strong>exact<\/strong>\u00a0[optional, default = false]<\/td>\n<td>disables the \u201cmerge similar styles\u201d feature, needed for some CSS inheritance issues<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Note that while\u00a0<code>classes<\/code>\u00a0and\u00a0<code>styles<\/code>\u00a0are both optional, one of the two should be present in each array. (Otherwise, why are you adding this to the dropdown anyway?)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Add this to your theme&#8217;s functions.php file&#8230; \/******** ADD CUSTOM STYLES TO TINYMCE EDITOR ********\/ add_filter(&#8216;mce_buttons_2&#8217;, &#8216;my_mce_buttons_2&#8217;); function my_mce_buttons_2($buttons) { array_unshift($buttons, &#8216;styleselect&#8217;); return $buttons; } add_filter(&#8216;tiny_mce_before_init&#8217;, &#8216;my_mce_before_init&#8217;); function my_mce_before_init($settings) { $style_formats = array( array( &#8216;title&#8217; =&gt; &#8216;Button&#8217;, &#8216;selector&#8217; =&gt; &#8216;a&#8217;, &#8216;classes&#8217; =&gt; &#8216;button&#8217; ), array( &#8216;title&#8217; =&gt; &#8216;Callout Box&#8217;, &#8216;block&#8217; =&gt; &#8216;div&#8217;, &#8216;classes&#8217; =&gt; &#8216;callout&#8217;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[144],"tags":[19,5],"class_list":["post-129","post","type-post","status-publish","format-standard","hentry","category-web-shots","tag-css","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/comments?post=129"}],"version-history":[{"count":7,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/129\/revisions"}],"predecessor-version":[{"id":3395,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/129\/revisions\/3395"}],"wp:attachment":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/media?parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/categories?post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/tags?post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}