Useful Snippets

Welcome!


This blog is used to collect useful snippets related to Linux, PHP, MySQL and more. Feel free to post comments with improvements or questions!

RSS Latest posts from my personal blog


Most viewed posts


Subscribe to RSS feed


Facebook oEmbed support for WordPress

Stanislav KhromovStanislav Khromov

Facebook has now publicly released oEmbed support, and a WordPress core ticket is under way to add support in a future WordPress version.

If you want to try out the new Facebook oEmbed support today, you can download and install this plugin: GitHub project or Direct zip download.

Here is an example of what the embed looks like:
fb-oembed

If you prefer the code, here it is in its entirety:

<?php
/*
Plugin Name: Facebook oEmbed
Version: 1.1
Author: khromov
*/

/**
 * https://core.trac.wordpress.org/ticket/34737
 */
add_action('init', function() {

    $endpoints = array(
        '#https?://www\.facebook\.com/video.php.*#i'          => 'https://www.facebook.com/plugins/video/oembed.json/',
        '#https?://www\.facebook\.com/.*/videos/.*#i'         => 'https://www.facebook.com/plugins/video/oembed.json/',
        '#https?://www\.facebook\.com/.*/posts/.*#i'          => 'https://www.facebook.com/plugins/post/oembed.json/',
        '#https?://www\.facebook\.com/.*/activity/.*#i'       => 'https://www.facebook.com/plugins/post/oembed.json/',
        '#https?://www\.facebook\.com/photo(s/|.php).*#i'     => 'https://www.facebook.com/plugins/post/oembed.json/',
        '#https?://www\.facebook\.com/permalink.php.*#i'      => 'https://www.facebook.com/plugins/post/oembed.json/',
        '#https?://www\.facebook\.com/media/.*#i'             => 'https://www.facebook.com/plugins/post/oembed.json/',
        '#https?://www\.facebook\.com/questions/.*#i'         => 'https://www.facebook.com/plugins/post/oembed.json/',
        '#https?://www\.facebook\.com/notes/.*#i'             => 'https://www.facebook.com/plugins/post/oembed.json/'
    );

    foreach($endpoints as $pattern => $endpoint) {
        wp_oembed_add_provider( $pattern, $endpoint, true );
    }
});

Web Developer at Aftonbladet (Schibsted Media Group)
Any opinions on this blog are my own and do not reflect the views of my employer.
LinkedIn
Twitter
WordPress.org Profile
Visit my other blog

Comments 4