Monthly Archives: March 2015

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.

Truncate string overflow

I was trying to set acl_name for a document using DFC when I encountered the below exception:
DfAttributeValueException:: THREAD: http-8080-1; MSG: [DFC_OBJECT_BADATTRVALUE] value is too big for attribute 'acl_name'. Value UTF-8 length is 36. Maximum length is 32.; ERRORCODE: ff; NEXT: null
This makes sense as string attributes in Documentum can hold a maximum of 32 characters. Ideally, we shouldn’t try to insert data more than the attribute can handle. However, this sometimes become necessary when dealing with a live application where the data set is already defined and changing production data is not encouraged. In such cases, a quick way to suppress the exception and truncate overflowing characters is to add the below line in dfc.properties file.
dfc.compatibility.truncate_long_values=true
Hope this helps! Please leave me a comment and let me know if you faced similar issue in the past and if there is a better way to achieve the same results.

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.