0

I'm french and I use the plugin "Theme my login". I want to know how I could update user's meta in a basic page. Thank's for help.

In the template of my page , I have this code :

    $user_adresse_key = 'user_adresse';
            $user_adresse = get_user_meta( $user_id, $user_adresse_key, $single );
            ?>
            <form id="form_livraison" action="#formulaire" method='post'>
                <input type='text' name='user_adresse' value="<?php echo $user_adresse;?>" />
                <input type='submit' value='mettre à jour' name='submit_form' />
            </form>
            <?php
            update_user_meta($user_id, 'user_adresse', $_POST['user_adresse']);
            ?>
  • firstly check the values by debugging the code..echo $user_adresse; also check for user id.. – Priya jain Jul 7 '14 at 9:09
  • I check and it's return values. – nadir Jul 7 '14 at 11:18
1

This should go near the top of your PHP script on the page to listen for the submitted data.

<?php
if (isset($_POST['user_adresse'])) {
    $userID = get_current_user_id();
    $userADRESSE = $_POST['user_adresse'];
    update_user_meta($userID , 'user_adresse' , $userADRESSE ); #'user_adresse' should be the name of the meta key
} ?>

and the form should be something like

<?php $user_adresse = get_user_meta(get_current_user_id(), 'user_adresse', true ); ?>
<form id="form_livraison" action="#formulaire" method='post'>
<input type='text' name='user_adresse' value="<?php echo $user_adresse;?>" />
            <input type='submit' value='mettre à jour' name='submit_form' />
        </form>

I would also recommend adding a nonce to the form for any front end user interactions.

Details can be found here under the section of Adding a nonce to a form

I have not tested this code so check for any misspellings etc!!

EDIT

    <?php
  if ( is_user_logged_in() )
{
 $user_id = get_current_user_id();
 $user_login = get_user_meta( $user_id, 'user_login', true );
 $user_email = get_user_meta( $user_id, 'user_email', true );
 $first_name = get_user_meta( $user_id, 'first_name', true );
 $last_name = get_user_meta( $user_id, 'last_name', true );
 $user_societe = get_user_meta( $user_id, 'user_societe', true );
 $user_adresse = get_user_meta( $user_id, 'user_adresse', true );
 $user_code_postal = get_user_meta( $user_id, 'user_code_postal', true );
 $user_ville = get_user_meta( $user_id, 'user_ville', true );
 $user_url = get_user_meta( $user_id, 'user_url', true );

echo $_POST['user_adresse']; // This will only show what is posted previously on the page - if you want to dsiplay the saved address use echo $user_adresse
if (isset($_POST['user_adresse'])) {
    $userADRESSE = $_POST['user_adresse'];
    update_user_meta($user_id , 'user_adresse' , $userADRESSE ); #'user_adresse' should be the name of the meta key
}
 ?>
<form id="form_livraison" action="#formulaire" method='post'>
<h2>Récapitulatif de vos coordonnées</h2>
<br />
<table style="width:320px">
<tr>
<td>Nom</td>
<td><?php echo $last_name; ?></td>
</tr>
<tr>
<td>Prénom</td>
<td><?php echo $first_name; ?></td>
</tr>
<tr>
<td>Adresse</td>
<td>
<?php  if (isset($_POST['user_adresse'])) { ##This means that if you have just updated the post it will show what the post is updated to - otherwise the original meta is displayed.
        echo $_POST['user_adresse']; } else { echo $user_adresse; }?></br><input type='text' name='user_adresse' placeholder="Ajouter/Changez votre adresse" /></td>
</tr>
<tr>
<td>Code postal</td>
<td><?php echo $user_code_postal; ?></td>
</tr>
<tr>
<td>Ville</td>
<td><?php echo $user_ville; ?></td>
</tr>
<tr>
<td>Web Adresse</td>
<td><input type='text' name='url' value="<?php echo $user_url;?>" /></td>
</tr>
</table>
<br />
<input type='submit' value='mettre à jour' name='submit_form' />
</form>

Happy Code Result!

  • Thank a lot for help me. I test and $_POST['user_adresse'] return anything in my code. – nadir Jul 7 '14 at 11:56
  • Sound silly but did you enclose the top section of code in <?php ?> tags - also I missed the tags off the top line of the second block too - I have just edited them in now. – Bysander Jul 7 '14 at 12:09
  • Yes I did that. – nadir Jul 7 '14 at 12:19
  • ok, I have run the code and it seems to work for me. Can you post your code into pastebin and I can try see what is wrong – Bysander Jul 7 '14 at 13:09
  • I test this code in a another template and it's working perfectly. I will search where is the problem. Again, thank a lot – nadir Jul 7 '14 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.