Great stuff from the Fublo bolg: http://blog.fublo.net/2011/12/edit-wordpress-user-profiles-gravity-forms/
On our latest client project, we created a Gravity Form which allowed logged in users to edit their public profiles, all done on the public side of the WordPress site, without visiting /wp-admin
.
This post outlines the way in which we stitched all the code together to make use of Gravity Forms. It’s not a definitive solution, but hopefully will help some people that need profile editing functionality inside their theme. The components are:
- A page template to hold the profile editing form and populate it with the existing profile information.
- A Gravity form with the required fields set up and named correctly.
- An additional hook and function call to process the received data and save it to the logged in use profile.
The code below updates the normal WP user’s first, last and display names, plus stores the additional data in a user meta field called “fublo_profile”, this can all be adapted as required.
Hook and function call
The following is the code that can be added to the functions file, or other included file in your theme. You will almost certainly need to edit this to fit with your requirements:
- Adjust the ID number of your form on line 3.
- Edit the fields in your form on line 13.
<pre>// 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 );</pre>
Page template
The page template checks that the user is logged in, grabs their data and uses it to populate the form. All the data is stored in $fublo_current_user_data
which is then passed to the form when it’s embedded in the template.
You should create a page in your wp-admin and set its template as “Edit profile” once this code is in place.
page-edit-profile.php
<pre>/* 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();</pre>
Gravity form
The Gravity form itself is now the simple bit. You should be able to see how it is loaded by the page template and how its fields are processed by the hook in the functions above.
You can use this export file which matches the function call above so it should fire the profile data back into the logged in user’s record. Profile edit form export data (XML file) – do right click, save as.
You will want to edit the confirmation action – one option is to point the user back to their newly updated profile page with a message saying its updated.
All done
That’s it – hopefully you’ll have a good starter profile editing form that you can expand and fit to your requirements. All the code can be viewed in this gist. Comments and questions always welcome.
Revisions
- May 14, 2012 @ 14:33:52 [Current Revision] by PeterLugg
- May 14, 2012 @ 14:30:35 by PeterLugg
No comments yet.