Displaying Content to Friends

Problem

How do I display content only to friends of a specific user?

Solution

Use the fb:if-is-friends-with-viewer tag. The simplest form is:

<fb:if-is-friends-with-viewer>You're friends!</fb:if-is-friends-with-viewer>

Discussion

As with all fb tags that begin with if, you can use the fb:else tag inside the fb:if-is-friends-with-viewer to provide alternate content if they aren’t friends. You can also specify a different uid if you don’t want to check for friendship with the loggedinuser, and you can explicitly exclude the loggedinuser if you don’t want to treat them as being friends with themselves (you will also be a friend of yourself unless you set includeself to false):

<fb:if-is-friends-with-viewer uid="12345" includeself="false">
    You're friends with <fb:name uid="12345" />. Yay!
    <fb:else>
        You aren't friends with <fb:name uid="12345" />. Boo!
    </fb:else>
</fb:if-is-friends-with-viewer>

Get Facebook Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.