1

I want to use HTML (schema.org) in my TinyMCE Editor. I use the following script in my custom.php

// Prevent TinyMCE from stripping out schema.org metadata
function schema_TinyMCE_init($in)
{
    /**
     *   Edit extended_valid_elements as needed. For syntax, see
     *   http://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|itemscope|itemtype|itemprop|datetime|rel],div,dl,ul,dt,dd,li,span,meta,content,a|rev|charset|href|lang|tabindex|accesskey|type|name|href|target|title|class|onfocus|onblur],div[*],meta[*],span[*],meta[itemprop],meta[content]';

    return $in;
}
add_filter('tiny_mce_before_init', 'schema_TinyMCE_init' );

The code works for some elements like <span itemprop="name">but nor for <meta itemprop="uploadDate" content="2015-12-15T10:10:04.000Z"/>

Why doesn't it work for meta itemprop="..." content="..."

1
  • Set selector options parameter to extend valid elements for this TinyMce javascript
    – Amit Geek
    Commented Nov 15, 2021 at 4:45

1 Answer 1

0

I dug this solution up, which adds meta to valid_children (untested):

// Prevent TinyMCE from stripping out schema.org metadata
// https://snipt.net/jamesw/prevent-tinymce-from-stripping-schemaorg-attributes-in-wordpress/
function schema_TinyMCE_init( $in ) {
    /**
     *   Edit extended_valid_elements as needed. For syntax, see
     *   http://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|itemscope|itemtype|itemprop|datetime|rel],div,dl,ul,dt,dd,li,span,meta[!content],a|rev|charset|href|lang|tabindex|accesskey|type|name|href|target|title|class|onfocus|onblur]',

    if ( !empty( $in['valid_children'] ) ) {
        $in['valid_children'] .= ',';
    }
    $in['valid_children'] .= '+body[meta],+div[meta]';

    return $in;
}
add_filter('tiny_mce_before_init', 'schema_TinyMCE_init' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.