Skip to content

Instantly share code, notes, and snippets.

@viruthagiri
Created February 15, 2012 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save viruthagiri/1838012 to your computer and use it in GitHub Desktop.
Save viruthagiri/1838012 to your computer and use it in GitHub Desktop.
Editing WordPress profiles with Gravity Forms
<?php
// add the following to your functions file or similar
define( 'FUBLO_GF_PROFILE', 1 ); // define the ID number of your profile form.
// ============================================================= PROFILE EDITING
/**
* These are the user metadata fields, with their names and the data about them.
* You can add more information to each field for example on whether to display
* in the public profile, the format, etc. In this example, we just have the
* index of each item so we can reference it easily.
*/
function fublo_gf_profile_metafields()
{
return array(
'displaynamepref' => array(
'gf_index' => 2,
),
'age' => array(
'gf_index' => 3,
),
'sex' => array(
'gf_index' => 4,
),
'location' => array(
'gf_index' => 5,
),
'twitter' => array(
'gf_index' => 6,
),
);
}
/**
* Update the user's profile with information from the received profile GF.
* run last - just to make sure that everything is fine and dandy.
*/
function fublo_gf_profile_update( $entry, $form )
{
// make sure that the user is logged in
// we shouldn't get here because the form should check for logged in users...
if ( !is_user_logged_in() )
{
wp_redirect( home_url() );
exit;
}
// get current user info...
global $current_user;
get_currentuserinfo();
// do the user data fields...
$new_user_data = array(
'ID' => $current_user->ID,
'first_name' => $entry['1.3'], // these are the ID numbers of these fields in our GF
'last_name' => $entry['1.6'],
);
// build the metadata from the entry
$new_user_metadata = array();
foreach ( fublo_gf_profile_metafields() as $field_name => $info )
{
$new_user_metadata[ $field_name ] = $entry[ $info['gf_index'] ];
}
// build the display name - (there's almost certainly something in WP to do this already, probably in an admin file)
switch ( $new_user_metadata['displaynamepref'] )
{
// like James
case 'first':
$display_name = $new_user_data['first_name'];
break;
// like James C
case 'short first':
$display_name = $new_user_data['first_name'] . ' ' . ucfirst( substr( $new_user_data['last_name'], 0, 1 ));
break;
// like J Cooke
case 'short last':
$display_name = ucfirst( substr( $new_user_data['first_name'], 0, 1 )) . ' ' . $new_user_data['last_name'];
break;
// like James Cooke.
case 'full':
default:
$display_name = $new_user_data['first_name'] . ' ' . $new_user_data['last_name'];
break;
}
$new_user_data['display_name'] = $display_name;
// ----------------------------------------------- SAVE ALL THE THINGS
wp_update_user( $new_user_data );
update_user_meta( $current_user->ID, 'fublo_profile', $new_user_metadata );
}
add_action( 'gform_after_submission_' . FUBLO_GF_PROFILE, 'fublo_gf_profile_update', 100, 2 );
<?xml version="1.0" encoding="UTF-8"?>
<forms version="1.6.1">
<form labelPlacement="left_label" useCurrentUserAsAuthor="1">
<title><![CDATA[Edit profile]]></title>
<description><![CDATA[Edit your profile]]></description>
<button type="text">
<text><![CDATA[Update]]></text>
</button>
<fields>
<field allowsPrepopulate="1" id="1" isRequired="1" size="medium" type="name">
<description><![CDATA[Your name in the site]]></description>
<label><![CDATA[Name]]></label>
<inputs>
<input id="1.3" name="first_name">
<label><![CDATA[First]]></label>
</input>
<input id="1.6" name="last_name">
<label><![CDATA[Last]]></label>
</input>
</inputs>
</field>
<field allowsPrepopulate="1" id="2" isRequired="1" size="medium" type="radio" enableChoiceValue="1">
<inputName><![CDATA[displaynamepref]]></inputName>
<label><![CDATA[Display name]]></label>
<choices>
<choice>
<text><![CDATA[First name only]]></text>
<value><![CDATA[first]]></value>
</choice>
<choice>
<text><![CDATA[First name and last name initial]]></text>
<value><![CDATA[short first]]></value>
</choice>
<choice>
<text><![CDATA[First name initial and last name]]></text>
<value><![CDATA[short last]]></value>
</choice>
<choice>
<text><![CDATA[Full name]]></text>
<value><![CDATA[full]]></value>
</choice>
</choices>
</field>
<field allowsPrepopulate="1" id="3" isRequired="1" size="medium" type="number" rangeMin="10" rangeMax="120" numberFormat="decimal_dot">
<inputName><![CDATA[age]]></inputName>
<label><![CDATA[Age]]></label>
</field>
<field allowsPrepopulate="1" id="4" isRequired="1" size="medium" type="radio">
<inputName><![CDATA[sex]]></inputName>
<label><![CDATA[Sex]]></label>
<choices>
<choice>
<text><![CDATA[Male]]></text>
</choice>
<choice>
<text><![CDATA[Female]]></text>
</choice>
</choices>
</field>
<field allowsPrepopulate="1" id="5" isRequired="1" size="medium" type="text">
<inputName><![CDATA[location]]></inputName>
<label><![CDATA[Location]]></label>
<maxLength><![CDATA[255]]></maxLength>
</field>
<field allowsPrepopulate="1" id="6" size="medium" type="text">
<description><![CDATA[Twitter if you have it]]></description>
<inputName><![CDATA[twitter]]></inputName>
<label><![CDATA[Twitter handle]]></label>
<maxLength><![CDATA[16]]></maxLength>
</field>
</fields>
<descriptionPlacement><![CDATA[below]]></descriptionPlacement>
<requireLogin><![CDATA[1]]></requireLogin>
<notification>
<subject><![CDATA[New submission from {form_title}]]></subject>
<message><![CDATA[{all_fields}]]></message>
<from><![CDATA[you@example.com]]></from>
</notification>
</form>
</forms>
<?php
/*
Template Name: Edit Profile
*/
// ---------------------------------------------------------------- CHECK STATUS
// check for logged in - kick if not.
if ( !is_user_logged_in() )
{
wp_redirect( home_url() );
exit;
}
/**
* pull in the required stuff for the GF
* more here : http://www.gravityhelp.com/documentation/page/Gravity_form_enqueue_scripts
* gravity_form_enqueue_scripts($form_id, $is_ajax);
*/
if ( function_exists( 'gravity_form_enqueue_scripts' ) )
{
gravity_form_enqueue_scripts( FUBLO_GF_PROFILE, false );
}
// get current user info...
global $current_user;
get_currentuserinfo();
// grab the current profile data...
$fublo_current_user_data = get_user_meta( $current_user->ID, 'fublo_profile', true );
// stuff the user's shizz into it... we use this to populate the GF.
$fublo_current_user_data['first_name'] = $current_user->first_name;
$fublo_current_user_data['last_name'] = $current_user->last_name;
get_header();
// ---------------------------------------------------------------- ROLL THE TEMPLATE
if (have_posts())
{
the_post();
?>
<h1><?php the_title() ?></h1>
<?php
the_content(); // this gives the opportunity to write something to our users.
if ( function_exists( 'gravity_form' ))
{
gravity_form( FUBLO_GF_PROFILE, false, false, false, $fublo_current_user_data );
}
else
{
?>
<h2>We need Gravity Forms installed to run this theme.</h2>
<?php
}
}
else
{
?>
<h1>Profile editing currently unavailable</h1>
<?php
}
get_footer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment