Hacking WordPress: User Profile Fields and CForms

Background:

A membership-based website sponsors business networking events. To participate in these events, a user needs to fill out a form that has  ten fields. There are many events and each time a user wants to participate in an event, these same ten fields need to be filled in. This has become an onerous task especially since users are entering the same information again and again.

Solution:

To eliminate the need for a user to fill in the same data in the same fields again and again, store the data for these fields in a user’s profile. When a user comes to the website to fill out a form, they would first log in and then go to the form for the event. If the user has provided the necessary information in their user profile, all ten form fields will be prefilled using that stored information.1

Implementing the Solution:

Implementing the solution required doing the following :

  1. Creating a custom plugin.
  2. Modifying a file in Cforms.
  3. Setting default values in Cforms to prefill the fields.

Note: The methods described here are based on Justin Tadlock’s tutorial. I recommend reading it. You may also want to look at all his tutorials.

1. Creating a custom plugin:

The plugin follows this structure:

  1. Comment information containing the name of the plugin, plugin author, etc.
  2. “add_action” statements
  3. Functions to get and save the data
A. Comment information containing the name of the plugin, plugin author, etc.:

This goes at the very top of the plugin’s PHP file. This is the information you will see when you go to your list of plugins.

/*
Plugin Name: additional-fields.php
Plugin URI: http://
Description: This adds additional fields to a user's profile page.
IMPORTANT NOTE - THESE FIELDS ARE TIED INTO LIB_AUX.PHP IN CFORMS
Author: Jeff
Version: 0.22
Author URI: http://
*/
B. add_action statements:

An add_action statement hooks a function on to a specific action. An action is “triggered” when a user does something like editing their profile. We need to hook (in a sense “attach”) a function to an action (e.g., updating a user profile) so that when the action occurs the function runs. (The list of actions you can hook into can be found here.) Let’s look at some code.

add_action( 'show_user_profile', 'my_show_extra_profile_fields' );

Now, let’s take it apart.

show_user_profile is the name of the action I want to hook in to.

my_show_extra_profile_fields
is the name of the function that should run when someone comes to their profile page. In this case, it will display the additional fields and fill in the data accordingly.

Additional add_action statements (see below) allow the user to edit their information and save it.2

add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
C. Functions to get and save data:

Next we need functions to get the user’s stored data and to save any changes.

Getting the data: The function below gets the user’s data and formats it for display. Notice the WordPress function get_the_author_meta which actually retrieves the data and esc_attr makes sure that it is properly formatted for display.

function my_show_extra_profile_fields( $user ) {
  $tmp = '
<h3>Online Registration Profile</h3>
   <table class="form-table">
    <tr>
     <th><label for="company">Company Name</label></th>
       <td>
       <input type="text" name="company" id="company"
        value="' . esc_attr(get_the_author_meta('company',$user-/>ID)) . '"
        class="regular-text" /><br />
        <span class="description">Please enter your Company Name.</span>
     </td>
   </tr>
  </table>

Saving the Data: This function first checks the user’s permissions (current_user_can) and if the user has permission to save the data, the WordPress function update_usermeta does the work of updating the information. Notice how this is structured.

update_usermeta( $user_id, 'company', $_POST['company'] );

First, the $user_id is the ID of the person currently logged in. Second, ‘company’ is the name of the field that stores the data. Last is $_POST['company'] which is the data to be saved.

function my_save_extra_profile_fields( $user_id ) {
  if ( !current_user_can( 'edit_user', $user_id ) )
   return false;
    update_usermeta( $user_id, 'company', $_POST['company'] );
}

2. Modifying a file in Cforms:

To make sure that the saved data is read by Cforms, we need to make a modification to the function check_default_vars which is located in lib_aux.php in the Cforms plugin subdirectory.

In the function check_default_vars you will find the following:

$m = str_replace( '{CurUserID}', $CurrUser->ID, $m );
$m = str_replace( '{CurUserName}', $CurrUser->display_name, $m );
$m = str_replace( '{CurUserEmail}', $CurrUser->user_email, $m );

You will then add something like the following:

$m = str_replace( '{company}', $CurrUser->company, $m );

Let’s look carefully at that statement above. The first parameter {company} is the “code” that we will enter into Cforms so that it prefills the form field called “Company.” The next parameter gets the value of “company” based on the currently logged in user.

3. Setting default values in Cforms to prefill the fields:

In the Cforms administration section, select the form you want to modify and set the default value of each field so that it can be automatically filled in. Notice that the default value is the parameter we just examined, that is, it is {company}.

Q.E.D.!

{ ===== //\\ ===== }

Here is the complete plugin code (this only shows one of the ten fields that my plugin stores):

< ?php /* Plugin Name: additional-fields.php Plugin URI: http:// Description: This adds additional fields to a user's profile page. IMPORTANT NOTE - THESE FIELDS ARE TIED INTO LIB_AUX.PHP IN CFORMS (See below) Author: Jeff Version: 0.22 Author URI: http:// */ /*************************************** In the file lib_aux.php, you need to make sure this function function check_default_vars($m,$no) has the fields that corresponds to the fields below. e.g.; $m = str_replace( '{company}', $CurrUser->company, $m );
*****************************************/

add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );

function my_show_extra_profile_fields( $user ) {

$tmp = '

<h3>Online Registration Profile</h3>
   <table class="form-table">
    <tr>
     <th><label for="company">Company Name</label></th>
       <td>
       <input type="text" name="company" id="company"
        value="' . esc_attr(get_the_author_meta('company',$user-/>ID)) . '"
        class="regular-text" /><br />
        <span class="description">Please enter your Company Name.</span>
     </td>
   </tr>
  </table>

';
print $tmp;
}

function my_save_extra_profile_fields( $user_id ) {
  if ( !current_user_can( 'edit_user', $user_id ) )
    return false;
  update_usermeta( $user_id, 'company', $_POST['company'] );
}
((The methods described here are based, in part, on this tutorial.))

  1. In the remainder of this post, I will only illustrate the methods used for only one of the ten fields.
  2. This information is stored in the table wp_usermeta. You may find this database description and ER diagram to be quite useful as you delve into WordPress.
  • chris

    Jeff – Awesome post… great info! Have you tried using this method combined with a plugin like Register Plus to more easily create the additional profile fields? I imagine that it would work assuming you correctly called out the form fields being used — is this a correct assumption? I have an extremely lengthy form that I would like pre-filled as much as possible with profile data, so I was going to make nearly all the items in my form part of the profile.

  • http://intensedebate.com/people/jeffsaddmind Jeff

    Thank you! It's always good to hear that someone has found a post to be of use. I have not used Register Plus but, theoretically, you are correct, namely that if the form field names match…then it should prefill the data. I would probably just make sure that the field names are absolutely unique so that there no collision with anything else that may be in the tables.

blog comments powered by Disqus
More in Wordpress (3 of 3 articles)