lundi 27 juin 2016

Modifing RSS nodes in WordPress

I have been asked by my work to add a rel='nofollow' to the WordPress RSS feed on our site. Now the RSS feed already has the rel='nofollow' added to all the <a href> tags which is working fine. What they are really asking is to add the nofollow to the actual RSS node itself.

They basically want <link rel='nofollow'> instead of <link>

Would adding a nofollow at the node level actually do anything? I understand it working at the href level but it seems odd to do this here. If this does work as expected then using PHP how can I modify this node to add this namespace?

Here is an example of my RSS feed.

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

I have a custom PHP page that modifies this RSS already but I am not sure if I need to replace the node completely or if there is a away to modify it directly. I was thinking of using str_replace but that did not work.

<?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;
        }
    }
}
?>

Thanks in advance. Code examples appreciated.

Aucun commentaire:

Enregistrer un commentaire