Is there a filter or action to dequeue jQuery on Y

2019-06-07 17:52发布

问题:

I've added more detail from this question.

Yoast SEO page-sitemap.xml is invalid XML. Showing error Extra content at the end of the document

I found that Yoast SEO page-sitemap.xml is invalid because of jquery script tags being inserted before the <?xml> declaration.

Like this:

<script type='text/javascript' src='https://dev-intechrahealth.pantheonsite.io/wp-includes/js/jquery/jquery.js?ver=1.12.4'></script>
<script type='text/javascript' src='https://dev-intechrahealth.pantheonsite.io/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.4.1'></script>
<?xml version="1.0" encoding...

I do not want them added to sitemap files. I simply want my sitemap file to begin like this:

<?xml version="1.0" encoding...

What I found works is adding a bit of URL detection code to /wp-includes/script-loader.php that checks if the current URL is a sitemap, then conditionally loads the jquery scripts with $scripts->add. Like this:

// jQuery
$url = $_SERVER["REQUEST_URI"];
$isItSitemap = strpos($url, 'sitemap');
if ($isItSitemap==false) {
    $scripts->add( 'jquery', false, array( 'jquery-core', 'jquery-migrate' ), '1.12.4' );
    $scripts->add( 'jquery-core', '/wp-includes/js/jquery/jquery.js', array(), '1.12.4' );
    $scripts->add( 'jquery-migrate', "/wp-includes/js/jquery/jquery-migrate$suffix.js", array(), '1.4.1' );
}

But that's editing core and I don't want to do that. I want this to happen in my theme's (Divi) functions.php so as to be preserved when I update core.

So I did this in functions.php:

/* Register jQuery but first detect if the Yoast SEO sitemap is rendering and dequeue script if so. This prevents an XML format error which is caused when <script> tags appear before the <?xml> declaration */
add_action( 'wp_enqueue_scripts', 'dequeue_jquery_for_sitemap', 10 );
function dequeue_jquery_for_sitemap() {
$url = $_SERVER["REQUEST_URI"];
$isItSitemap = strpos($url, 'sitemap');
if (!$isItSitemap==false) {
    wp_dequeue_script( 'jquery' );
    wp_dequeue_script( 'jquery-core' );
    wp_dequeue_script( 'jquery-migrate' );
}
}

I even tried wp_deregister_script as well:

wp_dequeue_script( 'jquery' );
wp_deregister_script( 'jquery' );
wp_dequeue_script( 'jquery-core' );
wp_deregister_script( 'jquery-core' );        
wp_dequeue_script( 'jquery-migrate' );
wp_deregister_script( 'jquery-migrate' );

However, this doesn't solve the problem. They still show up in the page-sitemap.xml.

What other way is there to achieve this? Or what am I missing?