Stephen Kam // Gravity Forms // Get Username from Email
<?php
/**
* Stephen Kam // Gravity Forms // Get Username from Email
*
* Capture username portion of an email address and store it into another field.
*
* @version 0.1
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link http://gravitywiz.com/...
*
* Plugin Name: Gravity Forms Username from Email
* Plugin URI: http://gravitywiz.com/
* Description: Capture username portion of an email address and store it into another field.
* Author: Gravity Wiz
* Version: 0.1
* Author URI: http://gravitywiz.com
*/
class SK_Username_From_Email {
public function __construct( $args = array() ) {
// set our default arguments, parse against the provided arguments, and store for use throughout the class
$this->_args = wp_parse_args( $args, array(
'form_id' => false,
'source_field_id' => false,
'target_field_id' => false
) );
// do version check in the init to make sure if GF is going to be loaded, it is already loaded
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
// make sure we're running the required minimum version of Gravity Forms
if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) {
return;
}
// carry on
add_action( 'gform_pre_submission', array( $this, 'update_submitted_data' ) );
}
public function update_submitted_data( $form ) {
if( ! $this->is_applicable_form( $form ) ) {
return;
}
$value = (string) rgpost( "input_{$this->_args['source_field_id']}" );
if( ! $value ) {
return;
}
$bits = explode( '@', $value );
$_POST[ "input_{$this->_args['target_field_id']}"] = $bits[0];
}
public function is_applicable_form( $form ) {
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
return $form_id == $this->_args['form_id'];
}
}
# Configuration
new SK_Username_From_Email( array(
'form_id' => 1315,
'source_field_id' => 2,
'target_field_id' => 1
) );