Category Archives: PHP

Store uploaded file URL in custom field

I had to tweak the code here a little to get the desired result. For some reason, wp_get_attachment_url() method wasn’t returning the correct values. My version of the code is given below for reference:

add_filter( 'frm_new_post', 'create_a_custom_field', 10, 2 );
function create_a_custom_field( $post, $args ) {
$field_id = 89; //replace 89 with the correct field id
if ( isset( $_POST['item_meta'][ $field_id ] ) && !empty( $_POST['item_meta'][ $field_id ] ) ) {
$field_value = sanitize_text_field( $_POST['item_meta'][ $field_id ] );
$attachmentids = '';
foreach ($_POST['item_meta'][ $field_id ] as $value) {
$attachmentids=$attachmentids.','.$value;
}
$post['post_custom']['_product_image_gallery'] = $attachmentids;
}
return $post;
}

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

Formidable Pro new method

Quick update on one of the new methods among many introduced since FP v2.0

Deprecated method: $frm_entry_meta->get_entry_meta(entry_id, field_id)
New method: FrmEntryMeta::get_entry_meta_by_field(entry_id, field_id)

Hope this helps! Please feel free to share your experiences as well with the newer version of FP.

UltimateClientManager plugin

Riding on my proof of concept and research, I built v1.0 of my plugin for UCM.

My requirement is to build a plugin to manage Leads for different clients. There are options to add and modify leads. Currently, the plugin is being scaled so that Leads can flow back and forth between different systems. Technically, this will involve integration (Read/Write) to Formidable Pro as all the leads also sync to Formidable database as part of a separate solution.

Please let me know if you have questions and I will try to address individual queries. I shall be writing more on this subject once integration with Formidable Pro is complete.

PHPExcel reports generation

I worked on an assignment where the desire was to generate excel report of Formidable Pro entries from front end view. After some research, I chose PHPExcel as the library of my choice. The reason for using PHPExcel is a mature library combined with comprehensive documentation. Once you download the library and explode the zip file, all you require is the core, i.e. the *Classes* folder uploaded to your custom project. Include the library in your custom code using the lines below
ini_set('max_execution_time', 0); //this is optional and may be required if the data set is huge. It increases the script execution time
require_once 'path/to/Classes/PHPExcel.php';
//your custom code to generate report

I found PHPExcel to be quite efficient and mature for generating reports for analytics. However, please feel free to share your experience and let me know if there is a better library which can deliver the same results more efficiently.

Create Formidable entries from outside using nusoap web service

Nusoap makes designing web services in PHP easy. My requirement was to use Nusoap to design a web service that an external system can use to post entries in Formidable.
Nusoap can be downloaded here. Once downloaded, explode the zip file and upload *lib* folder to the project directory. We will be creating two files: postdata.php and createfpentry.php. The first file will act as the client to post requests. The latter will act as the web service server to handle post requests and insert formidable entries.
postdata.php
<?php
require_once ('lib/nusoap.php'); //imports the nusoap library
//set the post parameter
$param = array( 'fpformfieldslist' => array( 'v1' => trim($_GET['<var1>']),
'v2' => trim($_GET['<var2>']),
'v3' => trim($_GET['<var3>'])
)
);
$client = new nusoap_client('<hostname>/createfpentry.php'); //new object referring web service server
$response = $client->call('insert_fp_entry',$param); //invoke insert_fp_entry method from the server
//Process result
if($client->fault) {
//echo fault code and fault string: $client->faultcode and $client->faultstring
}
else {
echo $response; //echo response on successful web service call
}
?>

createfpentry.php
<?php
require_once ('lib/nusoap.php'); //import nusoap library
require_once( $_SERVER['DOCUMENT_ROOT'] . '/path/to/wp-load.php' ); //load WordPress from outside
$server = new soap_server; //server object
$server->register('insert_fp_entry'); //register insert_fp_entry method
function insert_fp_entry($fpformfieldslist) {
global $frm_entry, $frm_entry_meta;
$isCreated = $frm_entry->create(array(
'form_id' => 25, //replace 25 with your form id
'item_meta' => array(
152 => $fpformfieldslist['v1'], //replace 152 with your field id. You can set other fields with values equal to the different GET parameters
),
));
$result = 'Entry successfully created'; //you can make conditional edits to the response
return $result;
}
$server->service($HTTP_RAW_POST_DATA);
exit();
?>

The URL to access the web service is http://<host>/postdata.php?v1=test1&v2=test2&v3=test3. This will create an entry in formidable backend. The parameter values should be escaped for special characters.

Hope this helped! I shall be posting more on this subject in future posts. Please share your experiences and let me know if there is a better way to achieve similar results.

WordPress with Ultimate Client Manager

Ultimate Client Manager (UCM) is a Project Management tool designed for managing tasks and invoicing. This product offers the flexibility of customization to suit individual business needs.
I got an opportunity to work on a proof of concept where the intent was to establish the theory into practice. After studying their documentation, I chose to write a simple script to create a job inside of UCM, set the status to ‘New’ and Start Date as ‘Today’.
<?php
chdir('/path/to/ucm/');
include('init.php');
$job['name'] = 'Demo';
$job['status'] = 'New';
$job['date_start'] = date('Y-m-d');
$new_job_id = module_job::save_job('new',$job);
$new_job = module_job::get_job($new_job_id); //get values from newly created job
echo 'new job id:'.$new_job_id.' created with job name:'.$new_job['name'];
?>

Executing this code will insert a new job in UCM and the results will be echoed to the browser. I will be scaling this script as per my project requirements and most likely hook this to WordPress. But this can be used with any other platform.
Hope this helps! I will be writing more on this in future.

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.