Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GD Twitter Feed Custom Field example
<?php
/*
Plugin Name: GD Twitter Feed Custom Field
Plugin URI: https://wpgeodirectory.com/
Description: Adds a twitter custom field option, when a user adds their twitter username it will output thier feed.
Author: GeoDirectory Team
Version: 1.0.0
Author URI: https://wpgeodirectory.com
*/
/**
* 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.
@type bool cat_filter If true the field will appear in the advanced search 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_twitter($custom_fields,$post_type){
$custom_fields['twitter_feed'] = array( // The key value should be unique and not contain any spaces.
'field_type' => 'text',
'class' => 'gd-twitter',
'icon' => 'fa fa-twitter',
'name' => __('Twitter feed', 'geodirectory'),
'description' => __('Adds a input for twitter username and outputs feed.', 'geodirectory'),
'defaults' => array(
'data_type' => 'VARCHAR',
'admin_title' => 'Twitter',
'site_title' => 'Twitter',
'admin_desc' => 'Enter your Twitter username',
'htmlvar_name' => 'twitterusername',
'is_active' => true,
'for_admin_use' => false,
'default_value' => '',
'show_in' => '[detail],[owntab]',
'is_required' => false,
'validation_pattern' => '^[A-Za-z0-9_]{1,32}$',
'validation_msg' => 'Please enter a valid twitter username.',
'required_msg' => '',
'field_icon' => 'fa fa-twitter',
'css_class' => '',
'cat_sort' => false,
'cat_filter' => false
)
);
return $custom_fields;
}
add_filter('geodir_custom_fields','geodir_custom_field_twitter',10,2);
/**
* Filter the custom field output.
*
* @param string $html The html to be output.
* @param string $location The location name of the output location.
* @param object $cf The custom field object info.
*
* @return string The html to output.
*/
function geodir_custom_field_output_twitter_feed($html,$location,$cf){
global $post;
if (isset($post->{$cf['htmlvar_name']}) && $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;">';
$html .= '<a class="twitter-timeline" data-height="600" data-dnt="true" href="https://twitter.com/'.$post->{$cf['htmlvar_name']}.'">Tweets by '.$post->{$cf['htmlvar_name']}.'</a> <script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>';
$html .= '</div>';
endif;
return $html;
}
add_filter('geodir_custom_field_output_text_key_twitter_feed','geodir_custom_field_output_twitter_feed',10,3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.