wordpress contact form 7 unique id - wordpress

i am currently using contact form 7 for wordpress. i would like a unique id for every form filled in and sent.
i have had a look around the internet but cant find anything, although i did find the following:
http://contactform7.com/special-mail-tags/
would there be any easy way to make my own function to do something similar to the above tags? i would need it to be a function to go into my themes function file, so that plugin updates wont affect it.
Cheers Dan

[_serial_number] field is an auto-incremented form submission ID. You just have to have Flamingo plugin installed as well. See http://contactform7.com/special-mail-tags/
(It's the same link as in the question, I guess the field wasn't there when the question was asked).

To generate ID for the Contactform7 you need to hook the 'wpcf7_posted_data'.
However, to generate incremental ID you need to save the forms in database so you can retrieve which ID should be next on next Form submit. For this you will need CFDB plugin (https://cfdbplugin.com/).
If you dont want to put the code inside theme functions.php file, you can use this plugin instead: https://wordpress.org/plugins/add-actions-and-filters/
Example code:
function pk_generate_ID($formName, $fieldName) {
//Retrieve highest ID from all records for given form stored by CFDB, increment by 1 and return.
require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
$start = '001';
$exp = new CFDBFormIterator();
$atts = array();
$atts['show'] = $fieldName;
$atts['filter'] = "$fieldName>=$start&&$fieldName<999999999999"; //is_numeric() is not permitted by default for CFDB filter
$atts['limit'] = '1';
$atts['orderby'] = "$fieldName DESC";
$atts['unbuffered'] = 'true';
$exp->export($formName, $atts);
$found = $start;
while ($row = $exp->nextRow()) {
$row2 = $row[$fieldName];
$row2 += 1;
$found = max($found,$row2);
}
return $found;
}
function pk_modify_data( $posted_data){
$formName = 'Form1'; // change this to your form's name
$fieldName = 'ID-1'; // change this to your field ID name
//Get title of the form from private property.
$cf_sub = (array) WPCF7_Submission::get_instance();
$cf = (array) $cf_sub["\0WPCF7_Submission\0contact_form"];
$title = (string) $cf["\0WPCF7_ContactForm\0title"];
if ( $posted_data && $title == $formName) ) { //formName match
$posted_data[$fieldName] = pk_generate_ID($formName, $fieldName);
}
return $posted_data;
}
add_filter('wpcf7_posted_data', 'pk_modify_data');

Related

WordPress - Gravity Forms: How to add a "confirm phone number" field

If you are familiar with WordPress Gravity Forms then you know that can add a "Phone" field from the advanced fields options. That's great but I am working on a site that offers a service to mobile users so I need make sure that the person filling out the form does so twice (in two fields) to ensure that there isn't a typo in their mobile number entered. I've been looking everywhere and can't figure out how to do this.
This code from http://gravitywiz.com/custom-field-confirmation/ worked perfectly for me. There are more detailed instructions on their site.
Paste this into your functions.php file and change register_confirmation_fields(8, array(1, 2)); to suit your form.
/**
* Double Confirmation Fields
* http://gravitywiz.com/2012/05/01/custom-field-confirmation/
*/
register_confirmation_fields(8, array(1, 2));
add_filter('gform_validation', 'gfcf_validation');
function gfcf_validation($validation_result) {
global $gfcf_fields;
$form = $validation_result['form'];
$confirm_error = false;
if(!isset($gfcf_fields[$form['id']]))
return $validation_result;
foreach($gfcf_fields[$form['id']] as $confirm_fields) {
$values = array();
// loop through form fields and gather all field values for current set of confirm fields
foreach($form['fields'] as $field) {
if(!in_array($field['id'], $confirm_fields))
continue;
$values[] = rgpost("input_{$field['id']}");
}
// filter out unique values, if greater than 1, a value was different
if(count(array_unique($values)) <= 1)
continue;
$confirm_error = true;
foreach($form['fields'] as &$field) {
if(!in_array($field['id'], $confirm_fields))
continue;
// fix to remove phone format instruction
if(RGFormsModel::get_input_type($field) == 'phone')
$field['phoneFormat'] = '';
$field['failed_validation'] = true;
$field['validation_message'] = 'Your values do not match.';
}
}
$validation_result['form'] = $form;
$validation_result['is_valid'] = !$validation_result['is_valid'] ? false : !$confirm_error;
return $validation_result;
}
function register_confirmation_fields($form_id, $fields) {
global $gfcf_fields;
if(!$gfcf_fields)
$gfcf_fields = array();
if(!isset($gfcf_fields[$form_id]))
$gfcf_fields[$form_id] = array();
$gfcf_fields[$form_id][] = $fields;
}
// register field IDs 1 and 2 on form ID 8
register_confirmation_fields(8, array(1, 2));

Autocomplete drupal

It's not a problem but i would like to learn how to get some other value.
function search_autocomplete($string) {
$matches = array();
$temp= array();
$query = db_select('user','s');
$return = $query
->fields('s')
->condition('lastname', '%' . db_like($string) . '%', 'LIKE')
->range(0, 10)
->execute();
foreach ($return as $row) {
$matches[$row->user_id] = check_plain($row->lastname).' '.($row->firstname);
}
drupal_json_output($matches);
}
Now when search box appears when i start typing by last name i get user lets say Doe John. But when i press search button i get his user_id.
I know when i change user_id to lets say last name i get the last name when i search.
Now my question is how to get last and first name when search is clicked, but send the user_ID.
The entity reference module handles it by putting the uid in brackets after the name:
foreach ($return as $row) {
$key = check_plain($row->lastname).' '.($row->firstname).' ('.$row->user_id].')';
$matches[$key] = check_plain($row->lastname).' '.($row->firstname);
}
Another (but more complex) option would be to create a hidden field to store the uid in.

Get gravity forms fields

I am using the gravity form on my site. I am working on create the custom report for this I have need gravity form fields name and id based on specific form id.Please let me know how I can do it.
I am using the following function but it is showing all forms info based on it. it is looking very hard to parse it. Please let me know any function so I can get fields name easily.
$forms = RGFormsModel::get_forms_by_id(13);
try this
function get_all_form_fields($form_id){
$form = RGFormsModel::get_form_meta($form_id);
$fields = array();
if(is_array($form["fields"])){
foreach($form["fields"] as $field){
if(isset($field["inputs"]) && is_array($field["inputs"])){
foreach($field["inputs"] as $input)
$fields[] = array($input["id"], GFCommon::get_label($field, $input["id"]));
}
else if(!rgar($field, 'displayOnly')){
$fields[] = array($field["id"], GFCommon::get_label($field));
}
}
}
//echo "<pre>";
//print_r($fields);
//echo "</pre>";
return $fields;
}
It's not that hard to parse:
$fields=array();
foreach ( $forms['fields'] as $field ) {
$fields[]=array($field['id'] => $field['inputName']);
}
P.S. I'm assuming you use Gravity Forms < 1.7 as RGFormsModel::get_forms_by_id is a deprecated function since 1.7
// Get the Form fields
$form = RGFormsModel::get_form_meta($form_id);
// Run through the fields to grab an object of the desired field
$field = RGFormsModel::get_field( $form, $field_id );
I use the above to get a specific field I want to filter the value of. The $field contains an object with all the properties you want.
echo $field->label // Gets the label
echo $field->inputName // Gets the name
echo $field->type // Gets the type
echo $field->cssClass // Gets the CSS Classes as a string
You are able to get the entered value/content of a field by using rgpost() and by referencing the id ($field->id).
// Check the entered value of every field
foreach( $form['fields'] as &$field ) {
// Get the content for the specific field
$fieldContent = rgpost( "input_".$field->id );
// Check the content
if ( $fieldContent == ... ){}
}

How can I designate a comment box in WordPress to use a particular plugin

I have a plugin installed on my wordpress blog that takes the email address and message and creates a new user on my other CMS platform. Problem is this effects all comment boxes. I want to re-work this plugin to check if its on a particular page if so some values would be different. If not then proceed like usual.
function ifcj_createUser_SFI($comment_id) {
# determine commenters email
global $wpdb;
$sql = "SELECT comment_author_email FROM $wpdb->comments WHERE comment_ID = $comment_id";
$userEmail = $wpdb->get_var($sql);
if ( $userEmail == NULL ) {
return false;
}
# pull in class file
require( ABSPATH . 'class.convio_api.php');
# create instance and connection with convio, ALWAYS REQUIRED
$c = new ConvioAPI('site_id=xxx&api_key=xxxxxx&login_name=xxx#xxx.org&login_password=xxxxx');
# add user to groups/interests (this can be done when creating the user also)
$c->createUpdateUser('primary_email='.$userEmail.'&add_group_ids=73663&add_interest_ids=3761');
return true;
}
add_action('comment_post','ifcj_createUser_SFI',0,1);
Thanks in advance.
Use conditional tags: http://codex.wordpress.org/Conditional_Tags
Here is how you would test for a certain page: http://codex.wordpress.org/Conditional_Tags#A_PAGE_Page
Like:
if ( is_page( '2' ) ) {
return false;
}

How to get drupal custom profile values?

There is a checkbox added in user profile form, subscribe to daily newsletter. I want to get list of users who have subscribed (checkbox ON) and send them daily newsletter. How can I do this in drupal-6. (1) to get list of all subscribed users (2) Send them Email.
If you can explain at code level, I 'll really appreciate that.
If your profile field is profile_newsletter, your :
<?php
function mymodule_get_subscribers() {
$query = 'select * from {profile_fields} where `name`="%s";';
$result = db_query($query,'profile_newsletter');
$field = db_fetch_object($result);
$user_query = 'select * from {profile_values} where `fid`=%d and `value`=1;';
$result = db_query($user_query,$field->fid);
$subscribers = array();
while($subscriber = db_fetch_object($result)) {
$subscribers[] = $subscriber->uid;
}
return $subscribers; // Your array of subscriber user IDs
}
?>
This code was quick, is untested and should probably contain a few sanity-checks. But it should work.
For the sending of newsletters and such I'd recommend using a pre-rolled module. I haven't tried any for Drupal, but Simplenews seems to do the trick. http://drupal.org/project/simplenews
If nothing else it probably contains a good sending-function. Otherwise just use PHP mail()-function.
function your_custom_function ($user_id, "Field you want to get") {
$result = db_query("SELECT t2.value FROM profile_fields t1, profile_values t2 where
t1.title='Field you want to get' and t2.uid=".$user_id." and t1.fid=t2.fid");
$field = db_fetch_object($result);
$profile_type =$field->value;
return $profile_type;
}

Resources