Created
January 3, 2013 18:27
-
-
Save beweinreich/4445662 to your computer and use it in GitHub Desktop.
Wordpress Plugin that allows for easy Instagram integration
This file contains 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 | |
/* | |
Plugin Name: Instagram Integration | |
Plugin URI: http://roundedco.com | |
Description: Allows admin to manage instagram integration | |
Author: Brian Weinreich | |
Version: 1.0 | |
Author URI: http://www.roundedco.com | |
*/ | |
global $client_id; | |
global $client_secret; | |
global $redirect_uri; | |
global $instagram_id; | |
// your instagram app client id | |
$client_id = "123456"; | |
// your instagram app client secret | |
$client_secret = "123456"; | |
// this should be the FULL URL path to the plugin page ... i.e. /wp-admin/admin.php?page=binstagram.php | |
$redirect_uri = site_url()."/wp-admin/admin.php?page=binstagram.php"; | |
// the users instagram ID | |
$instagram_id = "123456"; | |
/********************************************************************/ | |
/* Setting up the plugin (menu, settings page, etc) */ | |
/********************************************************************/ | |
add_action('admin_menu', 'create_instagram_menu'); | |
function create_instagram_menu() { | |
add_menu_page('Instagram', 'Instagram', 'administrator', __FILE__, 'instagram_settings_page'); | |
add_action( 'admin_init', 'register_mysettings' ); | |
} | |
function register_mysettings() { | |
register_setting( 'instagram-settings-group', 'instagram_access_token' ); | |
} | |
function instagram_settings_page() { | |
global $client_id; | |
global $redirect_uri; | |
?> | |
<div class="wrap"> | |
<h2>Instagram</h2> | |
<p>A valid access token is required to pull down the user's instagram feed and display it on the website. </p> | |
<? check_for_access_token();?> | |
<hr /> | |
<p><a class="button-primary" href="https://api.instagram.com/oauth/authorize/?client_id=<?=$client_id;?>&redirect_uri=<?=$redirect_uri;?>&response_type=code">Authorize Instagram</a></p> | |
</div> | |
<?php } | |
function check_for_access_token() { | |
global $client_id; | |
global $redirect_uri; | |
global $client_secret; | |
if(isset($_GET['code'])) { | |
$url = 'https://api.instagram.com/oauth/access_token'; | |
$fields_string = ""; | |
$fields = array( | |
'client_id' => urlencode($client_id), | |
'client_secret' => urlencode($client_secret), | |
'grant_type' => urlencode("authorization_code"), | |
'redirect_uri' => urlencode($redirect_uri), | |
'code' => urlencode($_GET['code']) | |
); | |
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } | |
rtrim($fields_string, '&'); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch,CURLOPT_URL, $url); | |
curl_setopt($ch,CURLOPT_POST, count($fields)); | |
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
$result = json_decode($result); | |
if(isset($result->error_type)) { | |
echo "There was an error updating the access token"; | |
$access_token = get_option("instagram_access_token", "N/A"); | |
} else { | |
$access_token = $result->access_token; | |
update_option("instagram_access_token", $result->access_token); | |
} | |
echo '<script>window.location="'.$redirect_uri.'"</script>'; | |
} else { | |
$access_token = get_option("instagram_access_token", "N/A"); | |
} | |
echo "<p>Access token: <b>".$access_token."</b></p>"; | |
} | |
/********************************************************************/ | |
/* These are the functions you can call anywhere in the application */ | |
/********************************************************************/ | |
// $count = number of items to return | |
function instagram_feed($count) { | |
global $instagram_id; | |
$access_token = get_option("instagram_access_token"); | |
$url = "https://api.instagram.com/v1/users/".$instagram_id."/media/recent?count=".$count."&access_token=".$access_token; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch,CURLOPT_URL, $url); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
$result = json_decode($result); | |
return $result; | |
} | |
// $hashtag = #something | |
// $count = number of items to return | |
function get_pics_with_hashtag($hashtag, $count) { | |
global $instagram_id; | |
$access_token = get_option("instagram_access_token"); | |
$hashtag = str_replace("#","",$hashtag); | |
$url = "https://api.instagram.com/v1/tags/".$hashtag."/media/recent?count=".$count."&access_token=".$access_token; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch,CURLOPT_URL, $url); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
$result = json_decode($result); | |
return $result; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment