• psychosomatic

    (@psychosomatic)


    I am trying to add in schema markup to my wp pages, which involve things like: <div itemscope itemtype=”https://schema.org/Event”&gt;
    <div itemprop=”name”>Name</div>
    <span itemprop=”description”>asdf</span>..

    But these are always removed by WP after I save. I have tried to:

    1) add functions in functions.php w/ things like
    “remove_filter( ‘the_content’, ‘wpautop’ );”
    “remove_filter( ‘wp_replace_in_html_tags’ );”

    2) add in the custom tags I need into the kses.php file in the “$allowedposttags” array, with no luck.

    I think I have tried a few different ways to figure this out, but every time I try to add the tag’s I mentioned they are removed :/
    Many thanks in advance ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • Okoth1

    (@okoth1)

    Put these lines in your theme’s function.php and check if it does the job.

    // 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|itemscope|itemtype|itemprop|datetime|rel],div,dl,ul,ol,dt,dd,li,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' );

    I tried this function and it works perfectly !

    Thanks

    Greetings guys! This is an awesome piece of advice! I have the same problem with it stripping out the “target” attributes of anchors that I have placed within the Description of my Product Categories. Any idea how to keep it from taking away the “target” attribute in this situation?

    Many thanks!

    I thought I had this solved using something similar but I cannot stop WP from stripping out <meta> tags in content for those times when they are needed. Has anyone found a solution for this?

    After some more searching, the reason why I had this working and now it does not is due to the up of the version of tinyMCE being used in WP. I found the answer here https://stackoverflow.com/questions/17491373/how-to-enable-meta-tag-in-tinymce-4-0

    using the code posted previously and adding the change from what I posted you get something like this:

    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|itemscope|itemtype|itemprop|datetime|rel],meta,div,dl,ul,ol,dt,dd,li,span,a|rev|charset|href|lang|tabindex|accesskey|type|name|href|target|title|class|onfocus|onblur]';
    
        $in['valid_children'] = '+body[meta],+div[h2|span|meta|object],+object[param|embed]';
        return $in;
    }
    add_filter('tiny_mce_before_init', 'schema_TinyMCE_init' );

    and now we can add meta tags again.

    Last note, I don’t do this the way I’ve posted. Some time ago I created a plugin to allow schema.org markup in the editor and I was looking for an answer to why meta tags were working before and why they’re not now. https://github.com/Hube2/wp-schema-org-in-editor

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Disable WP from removing unique html tags for schema’ is closed to new replies.