Category Archives: Wordpress

Dynamic drop-downs in Formidable Pro

One quick way to implement dynamic drop-downs (where second drop-down entries are dependent on the selection of first drop-down value) is to use the Dynamic Fields feature of Formidable Pro.
Upside
a) Very quick and easy to implement.
b) Developer can avoid all the fancy ajax and get/post scripts to fetch values from database. This reduces cost of development and increases ease of maintenance.
Downside
a) This is a paid feature.

Hope this helps! Please leave me a comment and let me know if there is any other way to implement dynamic fields in formidable.

Restrict WP Admin access

Quick tips to prevent users other than administrators to access WP-admin and the admin toolbar.
add_action( 'admin_init', 'your_method', 1 ); /* blocks WP-admin */
function your_method() {
$user = wp_get_current_user();
if ( ! $user->has_cap('administrator') && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
wp_die( __( 'You are not allowed to access the back end of this site.' ), 'Access Denied', array( 'back_link' => true ) );
}
}

add_action('set_current_user', 'your_method'); /* hides admin toolbar */
function your_method() {
$user = wp_get_current_user();
if ( !$user->has_cap('administrator') ) {
add_filter('show_admin_bar', '__return_false');
}
}

Hope this helps! Please leave me a note and let me know if there is a better way to achieve the same results.

Formidable Pro Nugget 2

I will be discussing about how to fetch values from “Data from Entries” field using Formidable API.

Data from Entries field type is a brilliant offering from Formidable Pro (FP) team, which at the very basic can display field values dynamically (let’s consider drop-down for this example) from other form entries.

Let’s consider two forms: FormA (source) and FormB (target). For this example, let’s assume FormA consists of one field “Name” and has two entries created. FormB has one Data From Entries field “Name” and is configured to display the Names from FormA as a drop-down. Ergo, the Name field in FormB will display two names, let’s assume the names are John and Jane Doe.

On previewing FormB, it will paint the drop-down list with the two names as expected. However, on analyzing the HTML structure in detail (I use Firebug), it is revealed to be somewhat similar as shown below.
<div id="frm_data_field_<field_id>_container">
<select id="field_<field_key>" name="item_meta[<field_id>]">
<option value=""> </option>
<option value="6">John Doe</option>
<option value="11">Jane Doe</option>
</select>
</div>

You’ll notice that option values are integer and not the actual names being displayed. This might be baffling when writing custom code to pull the user selected name from drop-down. However, these integer values are links back to the original form (FormA) entries driving the drop-down values. The numbers 6 & 11 (assumed for this example) are the corresponding item_ids for each entry from FormA.
So, while writing custom business logic to fetch the names from FormB, something as shown below will help. I am using hook frm_after_create_entry against FormB for illustration.
add_action('frm_after_create_entry', 'your_custom_method', 30, 2);
function your_custom_method($entry_id, $form_id) {
global $wpdb, $frm_entry, $frm_entry_meta;
if($form_id == 10) { //assuming FormB_id=10
//assuming Name field_id in FormA=111 & Name drop-down field_id in FormB=150
$name = $wpdb->get_var('SELECT meta_value FROM '.$wpdb->prefix.'frm_item_metas WHERE field_id=111 AND item_id='.$_POST['item_meta'][150]); //this line gets you the name from the original FormA entry based on the drop-down selection in FormB
//once user selected name is pulled from database, you are free to utilize the variable as per requirement
}
}

Hope this helps! Please leave a note and let me know if there is a better way to achieve the same result.

WordPress User Role

A quick way to get currently logged in user role using WordPress API.
$user = wp_get_current_user(); //returns a WP_User object
if($user->has_cap('ccexecutive') || $user->has_cap('administrator')) { //For example, check for a default admin role and a custom role
//custom logic
}

I tested the code and it works like a charm. Hope it helps! Please leave a note and let me know if there is a better way to achieve the same result.

Formidable Pro Nugget 1

Today, I will be discussing on Formidable Pro. This plugin is very popular for form building solutions and has an advanced API to support custom logic. Their documentation is also comprehensive. However, sometimes we don’t have the time to search a bible to get something specific we want to implement quickly. This is the intention behind my posts on Formidable Pro (FP). I will be sharing some tips and tricks that have been proven to work for faster turnaround to clients.
The first in the series is to get all entries for a particular form and the meta data/field values.
$entries = $frm_entry->getAll("form_id=8"); //replace 8 with your form id
foreach($entries as $entry){ //spin through the form entries
//In this example, we fetch two field values from each form entry. Replace 93 and 94 with your field ids
$entry_meta_field1 = $frm_entry_meta->get_entry_meta($entry->id, 93);
$entry_meta_field2 = $frm_entry_meta->get_entry_meta($entry->id, 94);
}

Hope this helps! Please leave a comment and let me know if there is a better way to achieve the same results.

Pass WordPress variables to JavaScript

WordPress has an excellent way to pass on variables declared in PHP to JavaScript. The steps are given below along with example.
$site_parameters = array(
'stylesheet_directory' => get_stylesheet_directory_uri()
); //define the variable in PHP; I want to pass the WordPress folder path to avoid hardcoding values in JS
wp_register_script( 'the handle', get_stylesheet_directory_uri().'/path to js file' ); //register the script
wp_localize_script( 'the handle', 'SiteParameters', $site_parameters ); //localize the variables
wp_enqueue_script( 'the handle', get_stylesheet_directory_uri().'/path to js file'); //enqueue the script

‘the handle’ should be the same in all the three steps for a particular script being registered.
To access the variable in JavaScript, simple use
var WPpath = SiteParameters.stylesheet_directory;

I have tested the code and it works beautifully! Hope this helps.

PHP and Web Service

I will be discussing a very simple example of using web services with PHP. The Web Service consumes a zip code (US zip code in our example) and returns an array containing the City, State, Area Code and Time Zone. To keep things simple, I will only convert Zip Code into City and State.
Web Service used: http://www.webservicex.net/uszip.asmx?op=GetInfoByZIP
Web Service WSDL: http://www.webservicex.net/uszip.asmx?WSDL
Programming Language: PHP
Framework: WordPress. Valid for any framework using PHP.
PHP APIs: SOAP Client, XML Parser
The code follows:
$zipcode = $_POST['zipcode']; //I am fetching the zip code via an ajax call from an user input
$url = "http://www.webservicex.net/uszip.asmx?WSDL";
$client = new SoapClient($url); //create a new SOAP client object and feed it with the WSDL URL
//$fns = $client->__getFunctions(); //You can dump the available functions using the getFunctions method
//var_dump($fns); //dumping the functions reveal the method definitions
$res = $client->GetInfoByZIP(array('USZip' => $zipcode)); //use the GetInfoByZIP method passing the USZip as an array parameter
$parsedXML = simplexml_load_string($res->GetInfoByZIPResult->any); //the returned value is in XML format which can be easily parsed using PHP XML parser
$city = $parsedXML->Table[0]->CITY; //fetch the city
$state = $parsedXML->Table[0]->STATE; //fetch the state

The output of the line var_dump($fns) is attached below.Method dump
As you can see, the GetInfoByZIP method has an array parameter and returns a complex type GetInfoByZIPResponse. The same can be verified from the WSDL mentioned in this post. This example can be used with any kind of web service and scaled as per business requirements.
Hope this helps! Please leave your valuable comments and let me know what you think.

WordPress and Owl Carousel

In this post, I would like to discuss a certain behavior of OwlCarousel I noticed with respect to the ‘responsive’ option of the plugin.
The below code snippet doesn’t output one single column on mobile devices if there is only one post/item to display.
responsiveClass:true,
responsive:{
0:{
items:1,
},
768:{
items:echo number of columns,
}
}

Instead, a slight change to the script as shown below works perfectly fine.
responsiveClass:true,
responsive:{
0:{
items:1.0000000000001,
},
768:{
items:echo number of columns,
}
}

Please leave a comment if you faced similar issue and what kind of resolution you came up with.