Yoast sitemap broken due to script tags

2019-05-30 15:17发布

问题:

Yoast SEO is returning an error when I access its sitemap. The file page-sitemap.xml says "error on line 2 at column 1: Extra content at the end of the document"

I tracked the problem down to the fact that four javascripts are referenced above the declaration. So, we see

<script type='text/javascript' src='https://www.example.com/wp-includes/js/jquery/jquery.js?ver=1.12.4'></script>
<script type='text/javascript' src='https://www.example.com/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.4.1'></script>
<script type='text/javascript' src='https://www.example.com/wp-content/plugins/imPower-weight-loss-tools/js/jquery.tools.min.js?ver=5.0.3'></script>
<script type='text/javascript' src='https://www.example.com/wp-content/plugins/imPower-weight-loss-tools/js/jquery-ui.js?ver=5.0.3'></script>
<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="//www.example.com/wp-content/plugins/wordpress-seo/css/main-sitemap.xsl"?>

Instead of simply

<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="//www.example.com/wp-content/plugins/wordpress-seo/css/main-sitemap.xsl"?>

as the start to the file.

In what way can I remove these script tags so that Yoast's page-sitemap.xml file can validate?

回答1:

UPDATED: I thought the below worked, but it didn't due to some other condition I had going on. When that condition changed, this no longer worked. I've asked this as a separate question with better information in a separate question here.

add_action( 'wp_enqueue_scripts', 'defer_jquery_for_sitemap', 100 );
function defer_jquery_for_sitemap() {
    $url = $_SERVER["REQUEST_URI"];
    $isItSitemap = strpos($url, 'sitemap');
    if ($isItSitemap==false) {
        wp_script_add_data( 'jquery-core', 'async/defer' , true );
        wp_script_add_data( 'jquery-migrate', 'async/defer' , true );
    }
}