Adding schema support to TinyMCE (partial solution)
-
So, like many other SEO-minded people who are hard-coding their schema into their posts, I’ve found that the TinyMCE editor automatically strips schema markup when switching between Text and Visual post editor views, which is frankly incredible in 2016.
After some searching, I found this partial solution, which is apparently taken from here. As one of the commentators on the latter page points out, there is a small error in the code, which is corrected as follows (with some code added):
// Prevent TinyMCE from stripping out schema.org metadata function schema_TinyMCE_init( $in ) { /** * Edit extended_valid_elements as needed. For syntax, see * https://www.tinymce.com/wiki.php/Configuration:valid_elements * * NOTE: Adding an element to extended_valid_elements will cause TinyMCE to ignore * default attributes for that element. * Eg. a[title] would remove href unless included in new rule: a[title|href] */ if( ! empty( $in['extended_valid_elements'] ) ) { $in['extended_valid_elements'] .= ','; } $in['extended_valid_elements'] .= '@[id|class|style|title|content|itemscope|itemtype|itemprop|datetime|rel],div,dl,ul,ol,dt,dd,h2,h3,h4,li,link,meta,span,a[rev|charset|href|lang|tabindex|accesskey|type|name|href|target|title|class|onfocus|onblur]'; return $in; } add_filter('tiny_mce_before_init', 'schema_TinyMCE_init' );
Now, I should also mention that in my search, I found this workaround to simply add a Custom Field with the desired schema markup. However, this is not a proper solution for me, because it doesn’t allow you to enter the markup directly into the post body.
All schema elements seem to work for me so far, except for the <meta> element. I’m a bit dumbfounded why this element alone fails to work (with or without the optional closing “/”), so I was wondering if anyone could enlighten me as to why this is, and hopefully even provide a solution.
Thanks for your time!
- The topic ‘Adding schema support to TinyMCE (partial solution)’ is closed to new replies.