RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.38% (6.14%*) • Home Loan Fixed: 5.44% (6.26%*) • Fixed: 5.44% (6.26%*) • Variable: 5.38% (6.14%*) • Investment IO: 5.69% (6.52%*) • Investment PI: 5.49% (5.98%*)

Embed Facebook Posts into Your WordPress Post or Page with Shortcode

Embed Facebook Posts into Your WordPress Post or Page with Shortcode

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".

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.

1
<?php 
2
/*
3
 Embed Facebook Posts into Your WordPress Post or Page with Shortcode
4
 http://www.beliefmedia.com/embed-facebook-posts-wordpress-shortcode
5
*/
6
 
7
8
 
9
  $atts = shortcode_atts(array(
10
    'url' => false,
11
    'width' => false,
12
    'text' => true,
13
    'handle' => 'beliefmedia'
14
  ), $atts);
15
 
16
  /* Is it a full URL? */
17
  $url = (strpos($atts['url'],'facebook.com') !== false) ? $atts['url'] : 'https://www.facebook.com/' . $atts['handle'] . '/posts/' . $atts['url'];
18
 
19
  /* Build result */
20
  $return = '<div class="fb-post" data-href="' . $url . '"';
21
  if ($atts['width'] !== false) $return .= ' data-width="' . $atts['width'] . '"';
22
  if ($atts['text'] === true) $return .= ' data-show-text="true"';
23
  $return .= '></div>';
24
 
25
  /* Wrap in our own DIV and
26
 
27
 tags */
28
  $result = '<div style="text-align:center;">' . $return . '</div>';
29
 
30
 return $result;
31
}
32
add_shortcode('fbpost', 'beliefmedia_embed_facebook_posts');

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.

1
<?php 
2
/*
3
 Embed Facebook Posts into Your WordPress Post or Page with Shortcode
4
 http://www.beliefmedia.com/embed-facebook-posts-wordpress-shortcode
5
*/
6
 
7
function beliefmedia_embed_facebook_posts($url, $width = false, $text =  true, $handle = 'BeliefMedia') {
8
 
9
  /* Is it a full URL? */
10
  $url = (strpos($url,'facebook.com') !== false) ? $url : 'https://www.facebook.com/' . $handle . '/posts/' . $url;
11
 
12
  /* Build result */
13
  $return = '<div class="fb-post" data-href="' . $url . '"';
14
  if ($width === true) $return .= ' data-width="' . $width . '"';
15
  if ($text === true) $return .= ' data-show-text="true"';
16
  $return .= '></div>';
17
 
18
  /* Wrap in our own DIV and
19
 
20
 tags */
21
  $result = '<div style="text-align:center;">' . $return . '</div>';
22
 
23
 return $result;
24
}

Download


Title: Embed Facebook Posts into WordPress (WP Plugin)
Description: Display WordPress Meetups With a Shortcode or PHP Function. Renders WordPress meetups in a specific area. Results are cached for 24 hours.
  Download • Version 0.3, 1.5K, zip, Category: WordPress Plugins (General)
WordPress Plugins (General), (712.0B)    PHP Code & Snippets, (995.0B)    

■ ■ ■

 
Download our complimentary 650-page guide on marketing for mortgage brokers. We'll show you exactly how we generate billions in volume for our clients.
Finance Guide, Cropped Top and Bottom
  Timezone: 1 · [ CHANGE ]

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment