I am trying to create a form that will allow users to update their information on the front end. I am just starting to learn PHP (I know C# very well). If I use this code to create a form on a WordPress page, how do I get the information from the user when they press the ‘Update’ button and then update the database?
function Insert_AccountTable_1()
{
$current_user = wp_get_current_user();
if ($current_user->ID != 0)
{
return <<<HTML
<form class="AlumniForm" method="post">
<h3>Name</h3>
<br>
<table>
<tbody>
<tr>
<th><label for="Username">Username</label></th>
<td><span>$current_user->user_login</span></td>
<td><span class="description">Usernames cannot be changed.</span></td>
</tr>
<tr>
<th><label for="current_user_firstname">First Name</label></th>
<td><input id="current_user_firstname" type="text" name="current_user_firstname" value="$current_user->user_firstname"/></td>
</tr>
<tr>
<th><label for="current_user_lastname">Last Name</label></th>
<td><input id="current_user_lastname" name="current_user_lastname" type="text" value="$current_user->user_lastname"/></td>
</tr>
<tr>
<th><label for="current_user_displayname">Display Name<span class="description">(required)</span></label></th>
<td><input id="current_user_displayname" type="text" value="$current_user->display_name"/></td>
</tr>
</tbody>
</table>
<h3>Contact Info</h3>
<br>
<table>
<tbody>
<tr>
<th><label for="current_user_email">E-mail<span class="description">(Required)</span></label></th>
<td><input id="current_user_email" type="text" value="$current_user->user_email"/></td>
</tr>
<tr>
<th><label for="current_user_url">Website</label></th>
<td><input id="current_user_email" type="text" value="$current_user->user_url"/></td>
</tr>
</tbody>
</table>
<input type="submit" name="current_user_submitupdates">Update</input>
</form>
HTML;
}
else
{
return <<<HTML
<h3>Error - User not logged in</h3>
HTML;
}
}
add_shortcode('InsertAccountTableI', 'Insert_AccountTable_1');
Use
wp-admin/admin-post.php
as form action handler, and bind your custom function as callback to that.A simple example for email updates. We will use a shortcode named
[userform]
here, but you can use a template too.Inserting â¦
⦠into a page will produce a basic form:
The user can change her/his email address here.
To understand what variables are available and where they are stored look at these files:
wp-admin/user-edit.php
wp-admin/includes/user.php
andwp-includes/user.php
The tables
users
anduser_meta
are worth a look too if you want to send plain SQL queries.