0

I have 2 websites: one is a company website, the other is a blog for the company - these sites are on separate domains.

On the homepage of the company website, there's a square tile featuring the latest post from the blog, including the blog title (hyperlinked) and the post author's picture/avatar.

To retrieve the latest post's title via RSS, I added a function similar to the one documented here to my functions.php. However, I'm struggling to find a working method for retrieving the author's picture.

A few plugins out there initially caught my eye. For example, one added the featured image of a post to the RSS, thereby making it retrievable. But asking authors to upload their profile pictures to each post is a less than ideal setup.

Is there a plugin-free method for getting the author's picture? At the very least, I'd like to find a plugin that specifically accounts for this scenario.

1

I'm afraid it's not possible to do it without any plugin.

Author's image (gravatar) is not published in RSS, and it's computed based on author's e-mail address, which also is not published in RSS...

But if you have control over both of these sites, then you could use JSON API/XML RPC to get the newest blog posts - then you could get all info from these posts...

1

It's informative to check out Really Simple Syndication Best Practices Profile .

About the <item> tag:

An item MAY contain the following child elements: author, category, comments, description, enclosure, guid, link, pubDate, source and title. All of these elements are OPTIONAL but an item MUST contain either a title or description.

About the <author> tag:

An item's author element provides the e-mail address of the person who wrote the item (OPTIONAL).

<author>jbb@dallas.example.com (Joe Bob Briggs)</author>

So if the authors' emails are public, then we could use:

/**
 * Add the <author> tag containing the author's email address.
 */
add_action( 'rss2_item', function()
{
    printf( '<author>%s</author>', get_the_author_meta( 'email' ) );
} );

Then there are extensions, like the Dublin Core.

But I'm not aware of a suitable tag to use for the avatar url.

We could perhaps use the <enclosure> tag, with three required attributes:

      <enclosure length="24986239" type="audio/mpeg"
                 url="http://dallas.example.com/joebob_050689.mp3" />

When an enclosure's size cannot be determined, a publisher SHOULD use a length of 0.

but then we would have to know the mime/type to follow the standard correctly. So that might be a problem.

Custom <wpse:avatar> tag:

Maybe the correct way is to introduce a new tag, for example:

<wpse:avatar></wpse:avatar>

But then we must define our own namespace:

xmlns:wpse="http://example.tld"

where the example.tld site contains information about our custom tag for others to read about. You can read more about it here and here, for example.

Then we could use the following:

/**
 * Add the custom 'wpse' namespace
 */
add_action( 'rss2_ns', function()
{
    print( ' xmlns:wpse="http://example.tld" ' );
} );

/**
 * Add the avatar url into our <wpse:avatar> tag
 */
add_action( 'rss2_item', function()
{
    printf( 
        '<wpse:avatar>%s</wpse:avatar>', 
        get_avatar_url( get_the_author_meta( 'ID' ) )
    );
} );

were we use the new get_avatar_url() function that was added in the core in version 4.2. You can read more about that function in my answer here.

Here's an example output:

<wpse:avatar>http://0.gravatar.com/avatar/c52e94b32934ec08c573b1c850a7a8a3
?s=96&d=mm&r=g</wpse:avatar>
  • Publishing author's e-mail address doesn't seem like a good idea to me. Second solution looks much better :) But since both sites are using WP, then there's no need to get these articles from RSS, I guess. – Krzysiek Dróżdż May 14 '15 at 6:51
  • 1
    I mention the <author> tag as a possibility within the RSS2 specification, that someone might take advantage of. There might be users with public emails and one can for example think of the many companies that use WordPress on their closed intranet. But I agree with you, that I would probably go for a custom tag ;-) As you mention there are so many other ways to do this - and there are of course some benefits using RSS regarding support, standard and tools. @KrzysiekDróżdż – birgire May 14 '15 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.