and like us on FB

This article will show you how to embed a Facebook post into your WordPress post or page with shortcode. Like a lot of Facebook embed features, it’s arguably not the most efficient short-term means of displaying post content on your page. In fact, the default Facebook embed code that’s provided in the menu attached to each post is quite adequate and will do as advertised.

So, Why Use Shortcode?

When the Facebook embed code changes (as it invariably will) – and assuming you were using the cheesy embed copy-and-paste embed code method – you would have to find and replace each occurrence of the embed code if you wish to use new features. With shortcode, of course, you only update one function and it effects changes site-wide. In fact, when we first published this code there were no available options for altering its behaviour in any way… and it’s almost a guarantee that Facebook will introduce other features in the future.

The Result

Using the shortcode of [fbpost url="https://www.facebook.com/beliefmedia/posts/1372047079556970"], we’ll render the following Facebook post . Without providing a width, Facebook will scale the container automatically. We’ve applied the necessary code to center it.

The shortcode of [fbpost url="10152007774068589" width="400"] will return the following (note that we’ve only provided the post ID, and we’ve also added a width of 400):

The ID-only method will only work on your own Facebook posts, or those posts belonging to to the handle as defined by a shortcode attribute.

Where Do I Find the Post URL?

The post URL is found by copying the link on the post date. Right click on the date and select “Copy link address”.

Facebook Embed URL

Facebook Post URL Copied from Post Date

WordPress Shortcode

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin. You may optionally download and install our plugin from the bottom of of the page.

If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.

It's a requirement when using this function that you download the FBSDK function. Review its use and download the code here. It's possible that your theme is already rendering the SDK by default (open up your source code and search for "connect.facebook.net" to confirm).

Shortcode Attributes

url

The url is either the full post URL or, if you have your handle defined as your own page, just the post ID.

width

The width of the post. It should be a minimum of 350 pixels, and a maximum or 750 pixels. By default the container will use fluid width.

text

Applied to a photo post. Set text="0" to exclude the text from the Facebook post, if any.

handle

If you would like to use a post ID only method, you will have to define your default page handle. Alternatively, you could use a handle and post ID for every post – negating the need for a full URL.

PHP Function

Outside of WordPress, the following can be used. The Facebook SDK is required.

Download

Title: Embed Facebook Posts into WordPress with Shortcode (or PHP)
Description: Embed Facebook Posts into Your WordPress Post or Page with Shortcode.
Download: Shortcode (V0.3) | WP Plugin (V0.3) | PHP Code (V0.1)

Shortt URL for this post: http://shor.tt/10ou