Is there a filter or action to dequeue jQuery on Yoast sitemaps?

大憨熊 提交于 2019-12-23 01:40:46

问题


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?

来源:https://stackoverflow.com/questions/54586417/is-there-a-filter-or-action-to-dequeue-jquery-on-yoast-sitemaps

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