• Resolved flashjunkie

    (@flashjunkie)


    I’m running W3 Total cache alongside a couple of other plugins, one of which is yoast.

    Yoast outputs the following:

    <script type=”application/ld+json” class=”yoast-schema-graph yoast-schema-graph–main”>……

    This contains a ‘CDATA’ element to associate our sites with our social accounts etc etc.

    Now with ‘Minify HTML – inline CSS & inline JS’ switched OFF – this works fine, however once I switch ON ‘Minify HTML – inline CSS & inline JS’ i’m receiving the following error in my Console.

    [Facebook Pixel] – Unable to parse JSON-LD tag. Malformed JSON found: ‘/*<![CDATA[*/{“@context”:”https://schema.org&#8221;,”@graph”:[{“@type”:”Organization”,”@id”:”……

    It seems Minify is block commenting out the CDATA and causing the error.

    Any ideas?

    Jay

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Contributor Marko Vasiljevic

    (@vmarko)

    Hello,

    In wp-content/plugins/w3-total-cache/lib/Minify/Minify/HTML.php replace lines 346 and 347 with:
    $m[2] = preg_replace( '/([a-z0-9]=)\'([^"\'\\s=]+[^"\'\\s=\\/])\'(\\s(?!\\/)|>)/i', '$1$2$3', $m[2] );

    $m[2] = preg_replace( '/([a-z0-9]=)"([^"\'\\s=]+[^"\'\\s=\\/])"(\\s(?!\\/)|>)/i', '$1$2$3', $m[2] );
    See if the issue persists.

    Thread Starter flashjunkie

    (@flashjunkie)

    OK thanks, trying now!

    (Fast support! Thanks!)

    Thread Starter flashjunkie

    (@flashjunkie)

    Still the same. Here is the code in its entirety.

    Minify HTML (Inline JSS & CSS) switched ON

    <script type="application/ld+json" class="yoast-schema-graph yoast-schema-graph--main">/*<![CDATA[*/{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://tpet.co.uk/#organization","name":"Teacher's Pet","url":"https://tpet.co.uk/","sameAs":["https://facebook.com/TeachersPetUK","https://instagram.com/tpetuk","https://youtube.com/TeachersPetUK","https://pinterest.com/teacherspetuk","https://twitter.com/TeachersPetUK"],"logo":{"@type":"ImageObject","@id":"https://tpet.co.uk/#logo","url":"https://tpet.co.uk/wp-content/uploads/2019/05/dexter-teachers-pet.png","width":500,"height":428,"caption":"Teacher's Pet"},"image":{"@id":"https://tpet.co.uk/#logo"}},{"@type":"WebSite","@id":"https://tpet.co.uk/#website","url":"https://tpet.co.uk/","name":"Teacher's Pet","publisher":{"@id":"https://tpet.co.uk/#organization"},"potentialAction":{"@type":"SearchAction","target":"https://tpet.co.uk/?s={search_term_string}","query-input":"required name=search_term_string"}},{"@type":"WebPage","@id":"https://tpet.co.uk/#webpage","url":"https://tpet.co.uk/","inLanguage":"en-GB","name":"Teacher's Pet » The home of high quality classroom resources for displays, activities & more!","isPartOf":{"@id":"https://tpet.co.uk/#website"},"about":{"@id":"https://tpet.co.uk/#organization"},"image":{"@type":"ImageObject","@id":"https://tpet.co.uk/#primaryimage","url":"wp-content/uploads/tpet-uploads/gary-home-1.png"},"primaryImageOfPage":{"@id":"https://tpet.co.uk/#primaryimage"},"datePublished":"2015-09-16T11:07:53+00:00","dateModified":"2019-06-10T16:20:13+00:00","description":"Welcome to Teacher's Pet your one stop shop for all of the very best in classroom resources including, posters, labels, games, activities, challenge cards, bunting and so, so much more!"}]}/*]]>*/</script>

    Minify HTML (Inline JSS & CSS) switched OFF

    <script type="application/ld+json" class="yoast-schema-graph yoast-schema-graph--main">{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://tpet.co.uk/#organization","name":"Teacher's Pet","url":"https://tpet.co.uk/","sameAs":["https://facebook.com/TeachersPetUK","https://instagram.com/tpetuk","https://youtube.com/TeachersPetUK","https://pinterest.com/teacherspetuk","https://twitter.com/TeachersPetUK"],"logo":{"@type":"ImageObject","@id":"https://tpet.co.uk/#logo","url":"https://tpet.co.uk/wp-content/uploads/2019/05/dexter-teachers-pet.png","width":500,"height":428,"caption":"Teacher's Pet"},"image":{"@id":"https://tpet.co.uk/#logo"}},{"@type":"WebSite","@id":"https://tpet.co.uk/#website","url":"https://tpet.co.uk/","name":"Teacher's Pet","publisher":{"@id":"https://tpet.co.uk/#organization"},"potentialAction":{"@type":"SearchAction","target":"https://tpet.co.uk/?s={search_term_string}","query-input":"required name=search_term_string"}},{"@type":"WebPage","@id":"https://tpet.co.uk/#webpage","url":"https://tpet.co.uk/","inLanguage":"en-GB","name":"Teacher's Pet » The home of high quality classroom resources for displays, activities & more!","isPartOf":{"@id":"https://tpet.co.uk/#website"},"about":{"@id":"https://tpet.co.uk/#organization"},"image":{"@type":"ImageObject","@id":"https://tpet.co.uk/#primaryimage","url":"wp-content/uploads/tpet-uploads/gary-home-1.png"},"primaryImageOfPage":{"@id":"https://tpet.co.uk/#primaryimage"},"datePublished":"2015-09-16T11:07:53+00:00","dateModified":"2019-06-10T16:20:13+00:00","description":"Welcome to Teacher's Pet your one stop shop for all of the very best in classroom resources including, posters, labels, games, activities, challenge cards, bunting and so, so much more!"}]}</script>

    After a more thorough look it appears minify is adding the ‘/*<![CDATA[*/’ itself…?

    Plugin Contributor Marko Vasiljevic

    (@vmarko)

    Hello,

    Are you using any other caching solution besides W3 Total Cache?

    Thread Starter flashjunkie

    (@flashjunkie)

    I have CDN configured through AWS and W3TC but currently the CDN is disabled during development.

    No other Cache plugins are installed.

    I also use Clearfy to disable some unneeded WP options etc ( I am not using any of their ‘Minify’ options )

    Plugin Contributor Marko Vasiljevic

    (@vmarko)

    Hello,
    Thank you for the provided info. We’ll check this and provide you with an answer.

    Thread Starter flashjunkie

    (@flashjunkie)

    Awesome thanks, more than happy to add some code to my site-specific-plugin if needed to fix this issue as I understand it’s more than likely bespoke to my site.

    For now, i’ve disabled HTML Minify.

    Many thanks.

    Plugin Contributor Marko Vasiljevic

    (@vmarko)

    Hello @flashjunkie,
    Thank you for your patience.
    W3 Total Cache HTML minify does add CDATA for specific script types for security reasons.
    There’s an ampersand (&) in the script content activities & more and that causes the plugin to think it needs to wrap the script content in CDATA.
    If you change that to activities and more, there’s no problem with minify anymore.

    Thread Starter flashjunkie

    (@flashjunkie)

    Great catch! I’ll make the change.

    Thanks for looking into this for me!

    Fantastic support ??

    Plugin Contributor Marko Vasiljevic

    (@vmarko)

    Hello @flashjunkie,
    Thank you very much.
    Make sure to rate our plugin if convenient!

    Thread Starter flashjunkie

    (@flashjunkie)

    I’ve made the following changes to the Yoast file “inc->class-wpseo-utils.php”

    /**
    * Returns a script tag with Schema blob.
    *
    * @param array  $graph The Schema graph array to output.
    * @param string $class The (optional) class to add to the script tag.
    *
    * @return false|string A schema blob with script tags.
    */
    public static function schema_tag( $graph, $class = 'yoast-schema-graph' ) {
    	if ( ! is_array( $graph ) || empty( $graph ) ) {
    		return false;
    	}
    
    	$output = array(
    		'@context' => 'https://schema.org',
    		'@graph'   => $graph,
    	);
    	//HOTFIX Find "& #39;" and replace with a '
    	//HOTFIX Find "&raquo;" and replace with a -
    	//return "<script type='application/ld+json' class='" . esc_attr( $class ) . "'>" . self::format_json_encode( $output ) . '</script>' . "\n";
    	$return_string_json = "<script type='application/ld+json' class='" . esc_attr( $class ) . "'>" . self::format_json_encode( $output ) . '</script>' . "\n";
    	$return_string_json = str_replace("'", "'", $return_string_json);
    	$return_string_json = str_replace("&raquo;", "-", $return_string_json);
    	return $return_string_json;
    }

    This seems to have fixed the issue but obviously not an ideal fix as it will need to be copied over every time yoast updates and does not account for any other HTML entities that appear on other pages. If you can recommend a better fix for this issue it would be much appreciated.

    Many thanks.

    • This reply was modified 5 years, 5 months ago by flashjunkie.
    • This reply was modified 5 years, 5 months ago by flashjunkie.
Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Minify is breaking YOAST JSON-LD Meta’ is closed to new replies.