修改 WordPress 中的 RSS 节点
Modifing RSS nodes in WordPress
我的工作要求我在我们网站上的 WordPress RSS 提要中添加 rel='nofollow'
。现在,RSS 提要 已经 将 rel='nofollow'
添加到所有 <a href>
标签中,工作正常。他们真正要求的是将 nofollow
添加到实际的 RSS node
本身。
他们基本上想要 <link rel='nofollow'>
而不是 <link>
在节点级别添加一个 nofollow
真的有用吗?我知道它在 href
级别工作,但在这里这样做似乎很奇怪。如果这确实按预期工作,那么使用 PHP 如何修改此节点以添加此命名空间?
这是我的 RSS 提要示例。
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
>
<channel>
<title>Article Title here</title>
<link>http://fakewebsiteurl.com</link>
<description>article description here</description>
<language>en-US</language>
<generator>https://wordpress.org/?v=4.5.2</generator>
<item>
<title>Test Article</title>
<link>http://fakewebsiteurl.com/news/test-article/</link>
<comments>http://fakewebsiteurl.com/news/test-article/#respond</comments>
<pubDate>Thu, 05 May 2016 18:16:50 +0000</pubDate>
<description><![CDATA[<p>Description text here</p>
<p>The post <a rel="nofollow" href="fakewebsiteurl.com/news/test-article/">Test Article</a> appeared here</p>
]]></description>
</item>
<item>
...
</item>
</channel>
我有一个自定义 PHP 页面已经修改了这个 RSS,但我不确定我是否需要完全替换该节点,或者是否可以直接修改它。我正在考虑使用 str_replace
但那没有用。
<?php
namespace X;
class RssFeed {
public function __construct() {
add_filter( 'the_content_feed', array( $this, 'add_nofollow_href' ) );
}
function add_nofollow_namespace($content) {
if (is_feed()) {
$link = '<link>';
$linkno = '<link rel="nofollow">';
$updated = str_replace($link, $linkno, $content);
return $updated;
}
}
}
?>
提前致谢。代码示例表示赞赏。
不幸的是,唯一可靠的方法是 create a custom feed template. As you can see from the source,核心提要模板中的 <link>
硬编码如下:
<link><?php bloginfo_rss('url') ?></link>
无法通过过滤器编辑此标签的属性。
我用 <link>
标签上的 rel="nofollow"
属性测试了 W3C Feed Validator:
This feed does not validate. line 13, column 1: Unexpected rel
attribute on link element [help]
<link rel="nofollow">http://exapmle.tld</link>
所以修改后的提要不会 validate:
The nofollow
keyword may be used with a and area elements. This
keyword does not create a hyperlink, but annotates any other
hyperlinks created by the element (the implied hyperlink, if no other
keywords create one).
The nofollow
keyword indicates that the link is not endorsed by the
original author or publisher of the page, or that the link to the
referenced document was included primarily because of a commercial
relationship between people affiliated with the two pages.
<link>
标签是 required by the rss2 specification, so removing (via plugin or custom template) wouldn't be an option. A drastic approach would be to disable the whole feed altogether (e.g. mentioned here)。
可以通过 rss2_ns
挂钩添加自定义 namsespace 并通过 rss2_head
挂钩添加自定义频道节点。 atom 命名空间已包含在 self 关系中:
<atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
出于好奇我测试了:
<atom:link href="<?php bloginfo_rss('url'); ?>"
rel="nofollow" type="application/rss+xml" />
这提供了一个有效的提要,但带有关于 nofollow
未注册 link relationship 的警告。但我不确定搜索机器人是否会考虑这种方法?
上一个回答:
除了创建自定义提要模板(如@mevius 所述)之外,我能想到的唯一解决方法是以下 输出缓冲 通过 rss_tag_pre
破解和 rss2_head
钩子:
add_action( 'rss_tag_pre', function( $tag )
{
if( 'rss2' === $tag )
ob_start();
} );
add_action( 'rss2_head', function()
{
echo str_replace( '<link>', '<link rel="nofollow">', ob_get_clean() );
} );
我们以 rss2 提要模板为目标。
如果您认为拥有这样的属性值得,那么您可以随时为它创建一个ticket。
我的工作要求我在我们网站上的 WordPress RSS 提要中添加 rel='nofollow'
。现在,RSS 提要 已经 将 rel='nofollow'
添加到所有 <a href>
标签中,工作正常。他们真正要求的是将 nofollow
添加到实际的 RSS node
本身。
他们基本上想要 <link rel='nofollow'>
而不是 <link>
在节点级别添加一个 nofollow
真的有用吗?我知道它在 href
级别工作,但在这里这样做似乎很奇怪。如果这确实按预期工作,那么使用 PHP 如何修改此节点以添加此命名空间?
这是我的 RSS 提要示例。
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
>
<channel>
<title>Article Title here</title>
<link>http://fakewebsiteurl.com</link>
<description>article description here</description>
<language>en-US</language>
<generator>https://wordpress.org/?v=4.5.2</generator>
<item>
<title>Test Article</title>
<link>http://fakewebsiteurl.com/news/test-article/</link>
<comments>http://fakewebsiteurl.com/news/test-article/#respond</comments>
<pubDate>Thu, 05 May 2016 18:16:50 +0000</pubDate>
<description><![CDATA[<p>Description text here</p>
<p>The post <a rel="nofollow" href="fakewebsiteurl.com/news/test-article/">Test Article</a> appeared here</p>
]]></description>
</item>
<item>
...
</item>
</channel>
我有一个自定义 PHP 页面已经修改了这个 RSS,但我不确定我是否需要完全替换该节点,或者是否可以直接修改它。我正在考虑使用 str_replace
但那没有用。
<?php
namespace X;
class RssFeed {
public function __construct() {
add_filter( 'the_content_feed', array( $this, 'add_nofollow_href' ) );
}
function add_nofollow_namespace($content) {
if (is_feed()) {
$link = '<link>';
$linkno = '<link rel="nofollow">';
$updated = str_replace($link, $linkno, $content);
return $updated;
}
}
}
?>
提前致谢。代码示例表示赞赏。
不幸的是,唯一可靠的方法是 create a custom feed template. As you can see from the source,核心提要模板中的 <link>
硬编码如下:
<link><?php bloginfo_rss('url') ?></link>
无法通过过滤器编辑此标签的属性。
我用 <link>
标签上的 rel="nofollow"
属性测试了 W3C Feed Validator:
This feed does not validate. line 13, column 1: Unexpected rel attribute on link element [help]
<link rel="nofollow">http://exapmle.tld</link>
所以修改后的提要不会 validate:
The
nofollow
keyword may be used with a and area elements. This keyword does not create a hyperlink, but annotates any other hyperlinks created by the element (the implied hyperlink, if no other keywords create one).The
nofollow
keyword indicates that the link is not endorsed by the original author or publisher of the page, or that the link to the referenced document was included primarily because of a commercial relationship between people affiliated with the two pages.
<link>
标签是 required by the rss2 specification, so removing (via plugin or custom template) wouldn't be an option. A drastic approach would be to disable the whole feed altogether (e.g. mentioned here)。
可以通过 rss2_ns
挂钩添加自定义 namsespace 并通过 rss2_head
挂钩添加自定义频道节点。 atom 命名空间已包含在 self 关系中:
<atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
出于好奇我测试了:
<atom:link href="<?php bloginfo_rss('url'); ?>"
rel="nofollow" type="application/rss+xml" />
这提供了一个有效的提要,但带有关于 nofollow
未注册 link relationship 的警告。但我不确定搜索机器人是否会考虑这种方法?
上一个回答:
除了创建自定义提要模板(如@mevius 所述)之外,我能想到的唯一解决方法是以下 输出缓冲 通过 rss_tag_pre
破解和 rss2_head
钩子:
add_action( 'rss_tag_pre', function( $tag )
{
if( 'rss2' === $tag )
ob_start();
} );
add_action( 'rss2_head', function()
{
echo str_replace( '<link>', '<link rel="nofollow">', ob_get_clean() );
} );
我们以 rss2 提要模板为目标。
如果您认为拥有这样的属性值得,那么您可以随时为它创建一个ticket。