首页 > 解决方案 > 在 Yoast 站点地图上是否有使 jQuery 出列的过滤器或操作?

问题描述

我从这个问题中添加了更多细节。

Yoast SEO page-sitemap.xml 是无效的 XML。显示错误Extra content at the end of the document

我发现 Yoast SEO无效,因为在声明page-sitemap.xml之前插入了 jquery 脚本标签。<?xml>

像这样:

<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...

我不希望它们添加到站点地图文件中。我只是希望我的站点地图文件像这样开始:

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

我发现的工作是添加一些 URL 检测代码来/wp-includes/script-loader.php检查当前 URL 是否是站点地图,然后有条件地使用$scripts->add. 像这样:

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

但那是编辑核心,我不想这样做。我希望这发生在我的主题(Divi)functions.php中,以便在我更新核心时保留。

所以我这样做了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' );
}
}

我什至也试过wp_deregister_script

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

但是,这并不能解决问题。他们仍然出现在page-sitemap.xml.

还有什么其他方法可以实现这一目标?或者我错过了什么?

标签: jquerywordpressyoast

解决方案


推荐阅读