Yoast sitemap broken due to script tags

两盒软妹~` 提交于 2019-12-08 05:00:59

问题


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 );
    }
}


来源:https://stackoverflow.com/questions/54488753/yoast-sitemap-broken-due-to-script-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!