Tips and cases of use for a successful WordPress website with calculated forms.
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).
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.
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.
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:
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:
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.
Once read the user's information, I'll describe how to update it after submitting the form.
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:
<?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,
'shipping_address',
$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.