DEV Community

Cover image for Create Facebook Feed in WordPress with Facebook API
Keramot UL Islam
Keramot UL Islam

Posted on • Originally published at blog.abmsourav.com

Create Facebook Feed in WordPress with Facebook API

With the help of Facebook rest API, we can create a Facebook feed on our website. Nowadays every business has a Facebook page. A Facebook feed on the website may help to boost business.
In this article, I’ll show how to get data from a Facebook page.

For fetching data from Facebook you need two things:

  1. Page ID
  2. Access Token

Get these data from Here.
Facebook provides some endpoints to access data. But for authentication, you need those two pieces of information.

$page_id = 'your facebook page id';
$token = 'your access token';
$url = 'https://graph.facebook.com/v4.0/' . $page_id . '/posts?' . '&access_token=' . $token;

$fb_posts = wp_remote_get( $url );
$posts = wp_remote_retrieve_body( $fb_posts );
print_r( json_decode( $posts ) );
Enter fullscreen mode Exit fullscreen mode

Now You are getting data but only basic data. For making a feed you need more depth information right?
You can also add a query in the endpoint.

$url_queries = 'fields=status_type,created_time,from,message,story,full_picture,permalink_url,attachments.limit(1){type,media_type,title,description,unshimmed_url},comments.summary(total_count),reactions.summary(total_count)';
$page_id = 'your facebook page id';
$token = 'your access token';
$url = 'https://graph.facebook.com/v4.0/' . $page_id . '/posts?' . $url_queries . '&access_token=' . $token;

$fb_posts = wp_remote_get( $url );
$posts = wp_remote_retrieve_body( $fb_posts );
print_r( json_decode( $posts ) );
Enter fullscreen mode Exit fullscreen mode

Now with some HTML and CSS, you can make a pretty visualization of the feed.

Congratulations, you have created the feed. But if you think deeply, you will find that it’s not good for performance. Because at every page refresh it’ll fetch data from the Facebook database, which is time-consuming.

So, you need to cash your data for better performance. WordPress has a mechanism called transient. It is used to cash data for a certain period of time...
Read More from my original article
________________________

Related Post:

Top comments (0)