RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts</strong | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.69% (5.89%*) • Home Loan Fixed: 5.39% (6.59%*) • Fixed: 5.39% (6.59%*) • Variable: 5.69% (5.89%*) • Investment IO: 5.69% (6.48%*) • Investment PI: 5.39% (6.59%*)

Add Facebook Videos To WordPress With Shortcode

Add Facebook Videos To WordPress With Shortcode

While YouTube seems to remain the default means of sharing video, one can easily share Facebook videos in the same manner. With Facebook's embedded video player you can easily add Facebook videos to your website - in WordPress or otherwise. You can use any public video post by a page or a person as the video source. This post will show you how to include (allowed) Facebook videos to your WordPress post or page with WordPress shortcode. We'll also include functions to be used outside of WordPress.

While Facebook provides a cut-and-paste iframe approach to embedding videos, it's rather pedestrian in nature. It also doesn't scale video with the same precision as the (default) SDK-dependent version. As such, we'll ignore it.

The Facebook Video URL

The Facebook video URL is generally written in two ways:

https://www.facebook.com/flightorg/videos/10154120412423640/
https://www.facebook.com/video.php?v=10154120412423640

These are the URL formats that we'll accommodate in our shortcode function. In reality, there's any number of URL formats that might be used. Because we're using PHP's parse_url() function, and because we identify a video primarily by way of its query string (v=), our approach tends to satisfy most combinations. If a 'pretty' URL is provided, we assume that the video id is the last string of digits provided in the string.

The Result

The shortcode of [fbvideo v="https://www.facebook.com/video.php?v=10153109099568640"] will render the following video into your WP post.

One advantage of rendering a Facebook video rather than its YouTube brethren is that Facebook will render the description as an option under the video, thus increasing conversion on likes and other interactions. So, in the next example we will use the more common 'pretty' video format, and we'll render the accompanying description. The shortcode of [fbvideo v="https://www.facebook.com/flightorg/videos/10154063210603640/" text="true"] will display the following:

The preferred means of sharing a video is with just the video ID. For example, [fbvideo v="10154063210603640"].

Shortcode Attributes

As with the video text as demonstrate above, there are a number of options that might be used to change the behaviour of the player.

v

v is the video URL, or just the video ID. The URL may not always be perfectly formed and you may have to trim unnecessary parameters. Ideally, simply provide just the video ID.

width

The width is the width of the video as it'll appear in your WordPress post or page. The video will center by default. Minimum width is 220px.

text

The text determines if the video description and other means of interacting with the video will display. Use true or false (default).

autoplay

If autoplay is true, the video will automatically start playing when the page loads. The video will be played without sound (muted). People can turn on sound via the video player controls. This setting does not apply to mobile devices. Can be false (default) or true.

captions

Set to true (captions="true") to show captions (if available) by default. Captions are only available on desktop.

fullscreen

Allow the video to be played in fullscreen mode. Can be false (default) or true (fullscreen="true").

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.

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

1
<?php 
2
/*
3
 Add Facebook Videos to Your WordPress Post or Page with Shortcode
4
 http://www.beliefmedia.com/facebook-videos
5
*/
6
 
7
function beliefmedia_facebook_video($atts) {
8
  extract(shortcode_atts(array(
9
    'v'  => '',
10
    'width'  => '560',
11
    'height'  => '',
12
    'text'  => 'false',
13
    'autoplay' => 'false',
14
    'captions' => 'false',
15
    'fullscreen' => 'false',
16
  ), $atts));
17
 
18
   if (strpos($v,'http') !== false) {
19
 
20
     if (strpos($v,'v=') !== false) {
21
       parse_str(parse_url($v , PHP_URL_QUERY), $params);
22
       $vid = @$params['v'];
23
         } else {
24
       $v = rtrim($v, '/');
25
       $vid = substr($v, strrpos($v, '/') + 1);
26
     }
27
 
28
   } else {
29
     $vid = trim($v);
30
   }
31
 
32
  $return = '<div style="max-width: ' . $width . 'px; margin: auto; display: block;">';
33
  $return .= '<div class="fb-video" data-href="https://www.facebook.com/video.php?v=' . $vid . '" data-width="' . $width . '" data-show-text="' . $text . '" data-allowfullscreen="' . $fullscreen . '" data-autoplay="' . $autoplay . '" data-show-captions="' . $captions . '"></div>';
34
  $return .= '</p></div>';
35
 
36
 return $return;
37
}
38
add_shortcode('fbvideo', 'beliefmedia_facebook_video');

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.

You may optionally install the WordPress plugin below. You will still have to install the Facebook SDK (also available as a plugin).

Considerations

  • The video address will vary so it's expected that you'll take the necessary precautions in cleaning the video URL before posting it into your post. The best method of embedding a video is by including just the video ID.
  • You can generally get the video URL (and ID) by hovering your mouse over the video publication date and reading the URL in your browser footer. Alternatively, when copying the URL of a video that's been shared, hover over the video text for the same result. You may also (right-click) copy-and-paste the text directly into your shortcode (and adjust as necessary).
  • While Facebook offers a few features over YouTube (in terms of interaction), it will also degrade your video and audio quality.
  • If you're a client it's likely we've already added this feature to your website. If we haven't, get in touch and we'll do it for you.
  • Using the embedded video player API you can control the embedded video player as well as observing events fired by the player. For example you may listen to the event when a video is paused or start the video playback using a custom button.

PHP Function

Outside of WordPress, something such as the following will yield the same result as shortcode usage. As with the shortcode function, the Facebook SDK is required.

1
<?php 
2
/*
3
 Add Facebook Videos To WordPress With Shortcode
4
 http://www.beliefmedia.com/facebook-videos
5
*/
6
 
7
function beliefmedia_facebook_video($v, $options = '') {
8
 
9
   /* Default Options & Merge User Options */
10
   $options = array ($options);
11
   $defaults = array('width'  => '560', 'text'  => 'false', 'autoplay' => 'false', 'captions' => 'false', 'fullscreen' => 'true');
12
   $opts = array_merge($defaults,$options);
13
 
14
   if (strpos($v,'http') !== false) {
15
 
16
     if (strpos($v,'v=') !== false) {
17
       parse_str(parse_url($v , PHP_URL_QUERY), $params);
18
       $vid = @$params['v'];
19
         } else {
20
       $v = rtrim($v, '/');
21
       $vid = substr($v, strrpos($v, '/') + 1);
22
     }
23
 
24
   } else {
25
     $vid = trim($v);
26
   }
27
 
28
  $return = '<div style="max-width: ' . $opts['width'] . 'px; margin: auto; display: block;">';
29
  $return .= '<div class="fb-video" data-href="https://www.facebook.com/video.php?v=' . $vid . '" data-allowfullscreen="' . $opts['fullscreen'] . '" data-width="' . $opts['width'] . '" data-show-text="' . $opts['text'] . '" data-autoplay="' . $opts['autoplay'] . '" data-show-captions="' . $opts['captions'] . '"></div>';
30
  $return .= '</p></div>';
31
 
32
 return $return;
33
}
34
 
35
/* Usage */
36
$options = array('width' => '520', 'text' => 'false', 'autoplay' => 'false', 'captions' => 'false', 'fullscreen' => 'true');
37
echo beliefmedia_facebook_video($v = '10154120412423640', $options);

Download


Title: Add Facebook Videos To WordPress With Shortcode (WP Plugin)
Description: Add Facebook videos to your WP post or page with shortcode. Requires FBSDK.
  Download • Version 0.2, 1.6K, zip, Category: WordPress Plugins (General)
WordPress Shortcodes, (924.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