To modify an RSS item’s <link>
node value, use the the_permalink_rss
filter. This is demonstrated in the code below where if a feature
post has a value set for _featureurl
, we will return the custom value. Otherwise, the default permalink is returned:
/**
* Filters the permalink to the post for use in feeds.
*
* @param string $post_permalink The current post permalink.
*/
add_filter( 'the_permalink_rss', 'wpse_the_permalink_rss', 10, 1 );
function wpse_the_permalink_rss( $post_permalink ) {
// Bail if this is not a feature.
if ( 'feature' !== get_query_var( 'post_type') ) {
return $post_permalink;
}
// Get the permalink URL.
$feature_permalink = get_post_meta(
get_the_ID(),
'_featureurl',
true
);
// If the the custom URL has been specified return it, otherwise, use default permalink.
// Note: This is run through esc_url() via the_permalink_rss().
if ( $feature_permalink ) {
return $feature_permalink;
} else {
return $post_permalink;
}
}
Example RSS output for the feature
post type, which has the following URL:
http://example.com/feed/?post_type=feature
Note that the <link>
node under the <item>
node contains the URL https://google.com
, which is what I set for the _featureurl
custom field:
<?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>Features – WP Theme Testing</title>
<atom:link href="http://localhost/wp-theme-testing/feed/?post_type=feature" rel="self" type="application/rss+xml" />
<link>http://localhost/wp-theme-testing</link>
<description>I <3 testing themes!</description>
<lastBuildDate>Sun, 04 Mar 2018 05:46:16 +0000</lastBuildDate>
<language>en-US</language>
<sy:updatePeriod>hourly</sy:updatePeriod>
<sy:updateFrequency>1</sy:updateFrequency>
<generator>https://wordpress.org/?v=4.9.1</generator>
<image>
<url>http://localhost/wp-theme-testing/wp-content/uploads/2017/06/cropped-favicon-32x32.png</url>
<title>Features – WP Theme Testing</title>
<link>http://localhost/wp-theme-testing</link>
<width>32</width>
<height>32</height>
</image>
<item>
<title>test feature</title>
<link>https://google.com</link>
<comments>http://localhost/wp-theme-testing/feartures/test-feature/#respond</comments>
<pubDate>Sun, 04 Mar 2018 05:41:29 +0000</pubDate>
<dc:creator><![CDATA[dave]]></dc:creator>
<guid isPermaLink="false">http://localhost/wp-theme-testing/?post_type=feature&p=3035</guid>
<description><![CDATA[a test!]]></description>
<content:encoded><![CDATA[<p>a test!</p>
]]></content:encoded>
<wfw:commentRss>http://localhost/wp-theme-testing/feartures/test-feature/feed/</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
</channel>
</rss>