O'Reilly logo

Facebook Cookbook by Jay Goldman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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>

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required