Created
January 18, 2016 22:10
-
-
Save shaharhesse/dbf88823a0d43c4ba15c to your computer and use it in GitHub Desktop.
Show latest post from Facebook page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* After finding the Facebook API docs to be inaccurate, | |
* and being forced to use the API to get a single latest post dynamically. | |
* API v5 | |
*/ | |
// Using Composer for Facebook PHP SDK | |
require_once( ABSPATH . '/vendor/autoload.php' ); | |
$fb = new Facebook\Facebook(array( | |
'app_id' => '{app_id}', | |
'app_secret' => '{app_secret}', | |
'default_graph_version' => 'v2.5', | |
)); | |
$fbApp = $fb->getApp(); | |
$accessToken = $fbApp->getAccessToken(); | |
$request = $fb -> sendRequest( | |
'GET', | |
'/{page_id}/posts', | |
array( | |
'limit' => '1' | |
), | |
$accessToken | |
); | |
$response = $request->getDecodedBody(); | |
$ids = array('',''); | |
$i = 0; | |
if( $response ): | |
if( is_array( $response ) && | |
isset( $response['data'] ) && | |
isset( $response['data'][$i] ) && | |
isset( $response['data'][$i]['id'] ) | |
): | |
$ids = explode( '_', $response['data'][$i]['id'] ); | |
endif; | |
endif; | |
$facebook_post_url = 'https://www.facebook.com/' . $ids[0] . '/posts/' . $ids[1] ; | |
?> | |
<div class="fb-post" data-href="<?php echo $facebook_post_url ?>" data-width="500" ></div> | |
<div id="fb-root"></div> | |
<script>(function(d, s, id) { | |
var js, fjs = d.getElementsByTagName(s)[0]; | |
if (d.getElementById(id)) return; | |
js = d.createElement(s); js.id = id; | |
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&version=v2.2"; | |
fjs.parentNode.insertBefore(js, fjs); | |
}(document, 'script', 'facebook-jssdk'));</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment