Skip to content

Instantly share code, notes, and snippets.

@1naveengiri
Created December 12, 2019 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1naveengiri/3a870a86aa83ff2253e56d567f52611e to your computer and use it in GitHub Desktop.
Save 1naveengiri/3a870a86aa83ff2253e56d567f52611e to your computer and use it in GitHub Desktop.
Instagram feed widget for geodirectory
<?php
/**
* Add custom field to the custom field list.
*
* @param array $custom_fields {
* The custom fields array to be filtered.
*
* @type string $field_type The type of field, eg: text, datepicker, textarea, time, checkbox, phone, radio, email, select, multiselect, url, html, file.
* @type string $class The class for the field in backend.
* @type string $icon Can be font-awesome class name or icon image url.
* @type string $name The name of the field.
* @type string $description A short description about the field.
* @type array $defaults {
* Optional. Used to set the default value of the field.
*
* @type string data_type The SQL data type for the field.
* @type string admin_title The admin title for the field.
* @type string site_title This will be the title for the field on the frontend.
* @type string admin_desc This will be shown below the field on the add listing form.
* @type string htmlvar_name This is a unique identifier used in the HTML, it MUST NOT contain spaces or special characters.
* @type bool is_active If false the field will not be displayed anywhere.
* @type bool for_admin_use If true then only site admin can see and edit this field.
* @type string default_value The default value for the input on the add listing page.
* @type string show_in The locations to show in. [detail],[moreinfo],[listing],[owntab],[mapbubble]
* @type bool is_required If true the field will be required on the add listing page.
* @type string validation_pattern HTML5 validation pattern (text input only by default).
* @type string validation_msg HTML5 validation message (text input only by default).
* @type string required_msg Required warning message.
* @type string field_icon Icon url or font awesome class.
* @type string css_class Field custom css class for field custom style.
* @type bool cat_sort If true the field will appear in the category sort options, if false the field will be hidden, leave blank to show option.
* }
* }
* @param string $post_type The post type requested.
*/
function geodir_custom_field_instagram($custom_fields,$post_type){
$custom_fields['instagram_feed'] = array( // The key value should be unique and not contain any spaces.
'field_type' => 'text', //
'class' => 'gd-instagram',
'icon' => 'fa fa-instagram',
'name' => __('Instagram feed', 'geodirectory'),
'description' => __('Adds a input for instagram access token and outputs feed.', 'geodirectory'),
'defaults' => array(
'data_type' => 'VARCHAR',
'admin_title' => 'Instagram',
'site_title' => 'Instagram',
'admin_desc' => 'Enter your Instagram Access Token',
'htmlvar_name' => 'instagramaccesstoken',
'is_active' => true,
'for_admin_use' => false,
'default_value' => '',
'show_in' => '[detail],[owntab]',
'is_required' => false,
'validation_pattern' => '',
'validation_msg' => 'Please enter a valid Instagram Access Token.',
'required_msg' => '',
'field_icon' => 'fa fa-instagram',
'css_class' => '',
'cat_sort' => false
)
);
return $custom_fields;
}
// 'geodir_custom_fields_custom' will add to custom fields box, 'geodir_custom_fields_predefined' will add to the predifined box
add_filter('geodir_custom_fields_predefined','geodir_custom_field_instagram',10,2);
function geodir_custom_field_output_instagram_feed( $html,$location,$cf ){
global $gd_post;
if (isset($gd_post->{$cf['htmlvar_name']}) && $gd_post->{$cf['htmlvar_name']} != '' ):
$class = ($cf['htmlvar_name'] == 'geodir_timing') ? "geodir-i-time" : "geodir-i-text";
$field_icon = geodir_field_icon_proccess($cf);
if (strpos($field_icon, 'http') !== false) {
$field_icon_af = '';
} elseif ($field_icon == '') {
$field_icon_af = ($cf['htmlvar_name'] == 'geodir_timing') ? '<i class=”fa fa-clock-o”></i>' : '';
} else {
$field_icon_af = $field_icon;
$field_icon = '';
}
$html = '<div class="geodir_more_info ' . $cf['css_class'] . ' ' . $cf['htmlvar_name'] . '" style=”clear:both;">';
$access_token = $gd_post->{$cf['htmlvar_name']}; // your access_token will be this format
$count_feed = 6; // how many feed want to load
$requestURL = 'https://api.instagram.com/v1/users/self/media/recent?access_token='.$access_token.'&count='.$count_feed;
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $requestURL,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => 1
));
$json_response = curl_exec($ch);
curl_close($ch);
$insta_feeds = json_decode($json_response, true);
foreach ($insta_feeds['data'] as $post) {
$pic_text=$post['caption']['text'];
$pic_link=$post['link'];
$pic_like_count=$post['likes']['count'];
$pic_comment_count=$post['comments']['count'];
$pic_src=str_replace("http://", "https://", $post['images']['standard_resolution']['url']);
$pic_created_time=date("F j, Y", $post['caption']['created_time']);
$pic_created_time=date("F j, Y", strtotime($pic_created_time . " +1 days"));
$html .= "<div class='col-md-4 col-sm-6 col-xs-12 item_box'>";
$html .= "<a href='{$pic_link}' target='_blank'>";
$html .= "<img class='img-responsive photo-thumb' src='{$pic_src}' alt='{$pic_text}'>";
$html .= "</a>";
$html .= "<p>";
$html .= "<p>";
$html .= "<div style='color:#888;'>";
$html .= "<a href='{$pic_link}' target='_blank'>{$pic_created_time}</a>";
$html .= "</div>";
$html .= "</p>";
$html .= "<p>{$pic_text}</p>";
$html .= "</p>";
$html .= "</div>";
}
// instagram feed code comes here.
$html .= '</div>';
endif;
return $html;
}
add_filter('geodir_custom_field_output_text_key_instagram_feed','geodir_custom_field_output_instagram_feed',10,3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment