Calculated Fields Form Blog

Tips and cases of use for a successful WordPress website with calculated forms.

Blog / Building a form to update the information of logged users

Building a form to update the information of logged users

Most businesses or services (such as gyms, stores, clinics, transportation services, etc.) require users to be able to edit their profiles.

This post describes how to read and update the information and metadata of registered users (User profiles).


Most businesses or services (such as gyms, stores, clinics, transportation services, etc.) require users to be able to edit their profiles.

This post describes how to read and update the information and metadata of registered users (User profiles).

Resources to use:

To read the user's data, we will use the set of "DS" fields (Set of fields distributed with the Developer and Platinum versions of the plugin, whose values are read from an external data source, like a database or CSV file, in this case, a database). To update the user's information in the database, I will edit the "cp_calculatedfieldsf_insert_in_database.php" file (File distributed with the Developer and Platinum versions of the plugin for storing the data collected by the form in a different database or table).

I'm going to make an aside to mention the tables in the WordPress database where users' information is stored. WordPress stores the basic users' data, like login, id, email, nice name, registered date, etc., into the "wp_users" table, and additional metadata, like first name, last name, shipping address, etc., into the "wp_usermeta" table.

On your WordPress, the prefix "wp_" in tables' names might be different. Our plugin includes some constants for the tables' names. You don't need to know the prefix used by your WordPress.

Case of use:

I will describe the process with a practical example. Supposing you have an e-commerce website with registered buyers and you need to build a form that allows buyers to edit their email and shipping addresses (their profiles). In WordPress, the email address is stored in the "user_email" column of the "wp_users" table, and the shipping address is stored as metadata in the "wp_usermeta" table with the "shipping_address" meta_key.

The form building:

To read the user email, insert an "Email DS" field in the form (I'll assume its name is fieldname1) and configure it as follows:

  • Select the "User Data" option as the data source.
  • Select the "user_email" option for the "Attribute for values" attribute.
  • And very important, tick the "Display data of logged user" checkbox to get the email address of the logged user.

Email DS Field

For the shipping address, I'll use a "Textarea DS" field (fieldname2). The information would be read directly from the database with a SQL query:

  • Selec the "Database" option for the data source attribute.
  • Leave the database connection data with the default values because the shipping address is stored in the website's database.
  • Tick the "Custom Query" option.
  • And enter the query:

SELECT metavalue as value FROM {wpdb.usermeta} WHERE metakey="shippingaddress" AND userid={user.id}

Note the use of the "as value" alias in the "SELECT" clause. It allows the plugin to know the column to use for the field's value. The {wpdb.usermeta} constant is a reference for the name of the usermeta table (with the prefix used by your WordPress) and {user.id} constant contains the id of logged user.

Textarea DS Field

Once read the user's information, I'll describe how to update it after submitting the form.

Updating the user's profile:

For storing the submitted data into a different table or database, it is required to edit the "cp_calculatedfieldsf_insert_in_database.php" file. As I said above, the "cp_calculatedfieldsf_insert_in_database.php" file is distributed with the Developer and Platinum versions of the plugin to allow the developers to manage the data as they need.

To edit the file, open it with the text editor of your choice. This file includes references to the $params variable. It is an array with the submitted fields and their values. It also contains other important information, like the "formid" item with the id of the submitted form, the "itemnumber" item with the id of submissions, etc.

The code into the "cp_calculatedfieldsf_insert_in_database.php" file is in every form's submission. For this reason, I'll include my code into a conditional statement to evaluate it only when the "update the user's data" form is submitted.

If the form's id is 7, the code to enter in the "cp_calculatedfieldsf_insert_in_database.php" file would be:

Updating user profile


    <?php
        if($params['formid']==7)
        {
            $user_id = get_current_user_id();
            if($user_id )
            {
                wp_update_user(
                    array(
                        'ID' => $user_id,
                        'user_email'=>$params['fieldname1']
                    )
                );
                update_user_meta(
                    $user_id,
                    'user_email',
                    $params['fieldname2']
                );
            }
        }

The previous code uses three WordPress functions:

get_current_user_id, to get the id of the current user. This function returns the number 0 if the user is not registered.

wp_update_user, to update the data of the user.

update_user_meta, to update the metadata of the user.