0

I have some problems with custom user profil. I Created 2 différents user profiles :

  • one is calld "particulier"
  • the other is called "entreprise"

You can see the code below. The thing is : At the begining, all the fields are filled, for all the users for all users regardless of their profile.

I go to the profile of a "particular" user. I modify a value of a field I update the user.

and then, some of the fields become empty. These are fields for users whose profile is "entreprise".

More specifically, these are fields that are pooled for all users belonging to the same company (I have specified the location of these fields or variables in the code).

I really don't understand why these data are erased ... I really hope you can help me. I have been trying to find a solution for several days. Many many thanks for your help.

the code is :

<?php
/* ---------------------------------------*/
/* FIRST CUSTOM USER PROFIL "particulier" */
/* ---------------------------------------*/

add_action( 'show_user_profile', 'extra_user_particulier_fields' );
add_action( 'edit_user_profile', 'extra_user_particulier_fields' );

function extra_user_particulier_fields( $user ) { 
global $lang, $langue;
    $user_id = $user->ID;
    $user1 = new WP_User( $user_id );
    $user_role = array_shift($user1->roles);

    if ($user_role == 'administrator'  ) return false;
    if ($user_role == 'membres_entreprise'  ) return false;
?>
    <h3>Information membre particulier</h3>
     <?php if (current_user_can( 'manage_options' )) { 

      $user_partinfo = get_userdata($user_id);
      $user_registered_part = $user_partinfo->user_registered;
      $last_name = $user_partinfo->last_name;
      $first_name = $user_partinfo->first_name;
      $user_email = $user_partinfo->user_email;
      ?><style> .user-url-wrap { display:none; } </style>

    <table class="form-table">
        <tbody>
            <tr>
                <th>Civilité/Anrede* </th>
                <td><input class="regular-text" id="particulier_titre1" type="text" name="particulier_titre1" value="<?php echo esc_attr( get_the_author_meta( 'particulier_titre1', $user_id ) ); ?>" /></td>
            </tr>
            <tr>
                <th>Titre</th>
                <td><input class="regular-text" id="particulier_titre2" type="text" name="particulier_titre2" value="<?php echo esc_attr( get_the_author_meta( 'particulier_titre2', $user_id ) ); ?>" /></td>
            </tr>
           <tr>
             <tr>               
           <th>Nom </th>
                <td> <?php echo esc_attr( get_the_author_meta( 'last_name', $user_id ) ); ?></td>
            </tr>
                <th>Prénom</th>
                <td><?php echo esc_attr( get_the_author_meta( 'first_name', $user_id ) ); ?></td>
            </tr>
            <tr>
                <th>Rue (partie 1)</th>
                <td><input class="regular-text" id="particulier_rue1" type="text" name="particulier_rue1" value="<?php echo esc_attr( get_the_author_meta( 'particulier_rue1', $user_id) ); ?>" /></td>
            </tr>
            <tr>
                <th>Code postal</th>
                <td><input class="regular-text" id="particulier_plz" type="text" name="particulier_plz" value="<?php echo esc_attr( get_the_author_meta( 'particulier_plz', $user_id ) ); ?>" /></td>
            </tr>
            <tr>
                <th>Ville</th>
                <td><input class="regular-text" id="particulier_ort" type="text" name="particulier_ort" value="<?php echo esc_attr( get_the_author_meta( 'particulier_ort', $user_id ) ); ?>" /></td>
            </tr>
            <tr>
                <th>Pays</th>
                <td><input class="regular-text" id="particulier_pays" type="text" name="particulier_pays" value="<?php echo esc_attr( get_the_author_meta( 'particulier_pays', $user_id ) ); ?>" /></td>
            </tr>
            <tr>
                <th>Téléphone</th>
                <td><input class="regular-text" id="particulier_telfix" type="text" name="particulier_telfix" value="<?php echo esc_attr( get_the_author_meta( 'particulier_telfix', $user_id ) ); ?>" /></td>
            </tr>
            <tr>
                <th>Téléphone mobile</th>
                <td><input class="regular-text" id="particulier_telmobile" type="text" name="particulier_telmobile" value="<?php echo esc_attr( get_the_author_meta( 'particulier_telmobile', $user_id ) ); ?>" /></td>
            </tr>
            <tr>
                <th>Société </th>
                <td><input class="regular-text" id="particulier_societe" type="text" name="particulier_societe" value="<?php echo esc_attr( get_the_author_meta( 'particulier_societe', $user_id ) ); ?>" /></td>
            </tr>
        </tbody>
    </table>
    <?php } ?>
<?php }

add_action( 'personal_options_update', 'save_extra_user_particulier_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_particulier_fields' );

function save_extra_user_particulier_fields( $user_id ) {
 
    if ( !current_user_can( 'edit_user', $user_id ) ) {
        return false;
    }
        update_user_meta( $user_id, 'particulier_titre1', $_POST['particulier_titre1'] );
        update_user_meta( $user_id, 'particulier_titre2', $_POST['particulier_titre2'] );
        update_user_meta( $user_id, 'particulier_rue1', $_POST['particulier_rue1'] );
        update_user_meta( $user_id, 'particulier_plz', $_POST['particulier_plz'] );
        update_user_meta( $user_id, 'particulier_ort', $_POST['particulier_ort'] );
        update_user_meta( $user_id, 'particulier_pays', $_POST['particulier_pays'] );
        update_user_meta( $user_id, 'particulier_telfix', $_POST['particulier_telfix'] );
        update_user_meta( $user_id, 'particulier_telmobile', $_POST['particulier_telmobile'] );
        update_user_meta( $user_id, 'particulier_societe', $_POST['particulier_societe'] );
}


/* ---------------------------------------*/
/* SECOND CUSTOM USER PROFIL "entreprise" */
/* ---------------------------------------*/
add_action( 'show_user_profile', 'extra_user_entreprise_fields' );
add_action( 'edit_user_profile', 'extra_user_entreprise_fields' );

function extra_user_entreprise_fields( $user ) { 
    global $lang,$isupp;
    /* permet l'affichage sur certain rôle seulement*/
        $userent_id = $user->ID;
        $user1 = new WP_User( $userent_id );
        $user_role = array_shift($user1->roles);
    
        if ($user_role == 'administrator'  ) return false;
        if ($user_role == 'membres_particulier'  ) return false;
    ?>
        <h3>Information membre entreprises</h3>
        <?php if (current_user_can( 'manage_options' )) { 
          $user_entinfo = get_userdata($userent_id);
          $user_registered_ent = $user_entinfo->user_registered;
        ?>
        <table class="form-table">
            <tbody>
                <tr>
                    <th>Raison sociale</th>
                    <td><?php echo esc_attr( get_the_author_meta( 'raisonsociale', $userent_id ) ); ?></td>
                    <td><input style="visibility: hidden;"  class="regular-text raisonsociale" type="text" name="raisonsociale_data" value="<?php echo esc_attr( get_the_author_meta( 'raisonsociale', $userent_id ) ); ?>"/> </td>
                </tr>
                <tr>
                <th>N° de membre de la société <br/> (Ajouté par le club)</th>
                    <td><input class="regular-text" id="entreprise_nummembre" type="text" name="entreprise_nummembre" value="<?php echo esc_attr( get_the_author_meta( 'entreprise_nummembre', $userent_id ) ); ?>" /><br/>     
                    <i><?php echo 'La valeur ici sera modifiée automatiquement pour tous les membres de la même entreprise '; ?> </i>
                </td>
                </tr>       
                <tr>
                    <th>Civilité/Anrede </th>
                    <td><input class="regular-text" id="anrede" type="text" name="anrede" value="<?php echo esc_attr( get_the_author_meta( 'anrede', $userent_id ) ); ?>" /></td>
                </tr>
                <tr>
                    <th>Titre</th>
                    <td><input class="regular-text titel" type="text" name="titel" value="<?php echo esc_attr( get_the_author_meta( 'titel', $userent_id ) ); ?>" /></td>
                </tr>
                 <tr>
                 <th><i>Information nom / prénom </i></th>
                 <td><i> <?php echo 'Le nom et le prénom sont à remplir dans la partie "Nom" du profil (au dessus). ' ?> </i> </td>
                </tr>
                <tr>
                    <th>Nom </th>
                    <td><?php echo esc_attr( get_the_author_meta( 'last_name', $userent_id ) ); ?></td>
                </tr>
                <tr>
                    <th>Prénom</th>
                    <td><?php echo esc_attr( get_the_author_meta( 'first_name', $userent_id ) ); ?></td>
                </tr> 
                <tr>
                    <th>Fonction</th>
                    <td><input class="regular-text" id="fonction" type="text" name="fonction" value="<?php echo esc_attr( get_the_author_meta( 'fonction', $userent_id ) ); ?>" /></td>
                </tr>
                <tr>
                    <th>Service</th>
                    <td><input class="regular-text" id="service" type="text" name="service" value="<?php echo esc_attr( get_the_author_meta( 'service', $userent_id ) ); ?>" /></td>
                </tr>
                <tr>
                    <th>Téléphone </th>
                    <td><input class="regular-text" id="tel" type="text" name="tel" value="<?php echo esc_attr( get_the_author_meta( 'tel', $userent_id ) ); ?>" /></td>
                </tr>
                <tr>
                    <th>E-mail</th>
                    <td><input class="regular-text email" type="text" name="email" value="<?php echo esc_attr( get_the_author_meta( 'email', $userent_id ) ); ?>" /></td>
                </tr>
                <tr> <th> <p>Contact pour la facturation</p></th><td>--------------------------------</td></tr>
                <tr>
                    <th>Civilité/Anrede </th>
                    <td><input class="regular-text" id="contactanrede" type="text" name="contactanrede" value="<?php echo esc_attr( get_the_author_meta( 'contactanrede', $userent_id ) ); ?>" /></td>
                </tr>
                <tr>
                    <th>Titre</th>
                    <td><input class="regular-text titel" type="text" name="contacttitel" value="<?php echo esc_attr( get_the_author_meta( 'contacttitel', $userent_id ) ); ?>" /></td>
                </tr>
                <tr>
                    <th>Nom </th>
                    <td><input class="regular-text nachname" type="text" name="contactnachname" value="<?php echo esc_attr( get_the_author_meta( 'contactnachname', $userent_id ) ); ?>" /></td>
                </tr>
                <tr>
                    <th>Prénom</th>
                    <td><input class="regular-text name" type="text" name="contactvorname" value="<?php echo esc_attr( get_the_author_meta( 'contactvorname', $userent_id ) ); ?>" /></td>
                </tr> 
                <tr>
                    <th>Fonction</th>
                    <td><input class="regular-text" id="contactfonction" type="text" name="contactfonction" value="<?php echo esc_attr( get_the_author_meta( 'contactfonction', $userent_id ) ); ?>" /></td>
                </tr>
                <tr>
                    <th>Service</th>
                    <td><input class="regular-text" id="contactservice" type="text" name="contactservice" value="<?php echo esc_attr( get_the_author_meta( 'contactservice', $userent_id ) ); ?>" /></td>
                </tr>
                <tr>
                    <th>Téléphone </th>
                    <td><input class="regular-text" id="contacttel" type="text" name="contacttel" value="<?php echo esc_attr( get_the_author_meta( 'contacttel', $userent_id ) ); ?>" /></td>
                </tr>
                <tr>
                    <th>Téléphone mobile</th>
                    <td><input class="regular-text telmobile" type="text" name="contactmobile" value="<?php echo esc_attr( get_the_author_meta( 'contactmobile', $userent_id ) ); ?>" /></td>
                </tr>
                <tr>
                    <th>E-mail</th>
                    <td><input class="regular-text useremail_contactfact" type="text" name="contactemail" value="<?php echo esc_attr( get_the_author_meta( 'contactemail', $userent_id ) ); ?>" /></td>
                </tr>
    
    
                <tr>
                    <th>Visibilité <br/> (Choisie par les membres)</th>
                    <td> <?php echo esc_attr( get_the_author_meta( 'visibiliteent', $userent_id )); ?></td>
                    <td><input style="visibility: hidden;" class="regular-text" id="visibiliteent" type="text" name="visibiliteent" value="<?php echo esc_attr( get_the_author_meta( 'visibiliteent', $userent_id ) ); ?>" />                   </td>
                </tr>
                <tr>
                    <th>Admission de l'entreprise par le club </th>
                    <td>
                        <input type="radio" name="admissionsociete_data" value="oui" id="oui"
                            <?php echo ("oui" !== get_the_author_meta( 'admissionsociete', $userent_id )) ? "" : " checked=\"checked\"";?> />
                        <label class="oui" for="oui">oui</label>
                        <br/>
                        <input type="radio" name="admissionsociete_data" value="non" id="non"
                            <?php echo ("non" !== get_the_author_meta( 'admissionsociete', $userent_id )) ? "" : " checked=\"checked\"";?> />
                        <label class="non" for="non">non</label>
                        <br/>
                    </td>                
                </tr>
       
            </tbody>
        </table>
         <?php } ?>
    <?php }

    add_action( 'personal_options_update', 'save_extra_user_entreprise_fields' );
    add_action( 'edit_user_profile_update', 'save_extra_user_entreprise_fields' );

    function save_extra_user_entreprise_fields( $userent_id ) {

        if ( !current_user_can( 'edit_user', $userent_id ) ) {
            return false;
        }
        update_user_meta( $userent_id, 'raisonsociale', $_POST['raisonsociale_data'] );
        update_user_meta( $userent_id, 'admissionsociete', $_POST['admissionsociete_data']  );

        update_user_meta( $userent_id, 'anrede', $_POST['anrede'] );
        update_user_meta( $userent_id, 'titel', $_POST['titel'] );
        update_user_meta( $userent_id, 'fonction', $_POST['fonction'] );
        update_user_meta( $userent_id, 'service', $_POST['service'] );
        update_user_meta( $userent_id, 'tel', $_POST['tel'] );

        update_user_meta( $userent_id, 'contactanrede', $_POST['contactanrede'] );
        update_user_meta( $userent_id, 'contacttitel', $_POST['contacttitel'] );
        update_user_meta( $userent_id, 'contactvorname', $_POST['contactvorname'] );
        update_user_meta( $userent_id, 'contactnachname', $_POST['contactnachname'] );
        update_user_meta( $userent_id, 'contactfonction', $_POST['contactfonction'] );
        update_user_meta( $userent_id, 'contactservice', $_POST['contactservice'] );
        update_user_meta( $userent_id, 'contacttel', $_POST['contacttel'] );
        update_user_meta( $userent_id, 'contactmobile', $_POST['contactmobile'] );
        update_user_meta( $userent_id, 'contactemail', $_POST['contactemail'] );
        update_user_meta( $userent_id, 'typem', $_POST['typem'] );
    
        //--------------------------------------------
        //Loop to modify the field for all members 
        //when there is a modification for one member

        //    ALL THE DATA IN THIS LOOP ARE DELETED
        //--------------------------------------------
        $raisonsociale = $_POST['raisonsociale_data'] ;
        $userscommun = new WP_User_Query( array(
            'meta_key'  => 'typem',     
            'orderby'   => 'meta_value',
            'order'     => 'asc',       
            'meta_query' => array(
                array(
                    'key'     => 'raisonsociale',
                    'value'   => $raisonsociale,
                    'compare' => 'LIKE'
                ),
                )
            ) );

            $userfirmas = $userscommun->get_results();
            foreach( $userfirmas as $userfirma ) { 
                $useridcommun = $userfirma->ID;

                update_user_meta( $useridcommun, 'entreprise_nummembre', $_POST['entreprise_nummembre'] );
                update_user_meta( $useridcommun, 'contactanrede', $_POST['contactanrede'] );
                update_user_meta( $useridcommun, 'contacttitel', $_POST['contacttitel'] );
                update_user_meta( $useridcommun, 'contactvorname', $_POST['contactvorname'] );
                update_user_meta( $useridcommun, 'contactnachname', $_POST['contactnachname'] );
                update_user_meta( $useridcommun, 'contactfonction', $_POST['contactfonction'] );
                update_user_meta( $useridcommun, 'contactservice', $_POST['contactservice'] );
                update_user_meta( $useridcommun, 'contacttel', $_POST['contacttel'] );
                update_user_meta( $useridcommun, 'contactmobile', $_POST['contactmobile'] );
                update_user_meta( $useridcommun, 'admissionsociete', $_POST['admissionsociete_data']  );    
                update_user_meta( $useridcommun, 'contactemail', $_POST['contactemail'] );
            }               
    }
5
  • what do you mean by "pooled"? This doesn't make much sense as that's not how fields/User meta work in WordPress, how is that implemented? And what is the reason for the $user1 variable? Is it not identical to $user? Why does it use get_the_author_meta instead of get_user_meta?
    – Tom J Nowell
    Feb 3, 2023 at 16:44
  • This doesn't look very scalable, as saving a user profile will get slower and slower as more users are added until there are too many and none can be added or updated. Would it not be better to store this data elsewhere such as a CPT? Then you'd have company archives and a single location to store the data without duplication?
    – Tom J Nowell
    Feb 3, 2023 at 16:50
  • Thanks for the answer. pooled : they are common data, linked data. For example, if I modify one of these data for a user, all the other users of the same company will also have this data modified "get_the_author_meta" because all the data come from a registration form But this part is working very well
    – Timama
    Feb 3, 2023 at 17:10
  • Yes, thank you for this comment with the CPTs Tom. But there won't be too many users. That's not the problem. The user must be in a user profile
    – Timama
    Feb 3, 2023 at 17:15
  • I think there is an interaction because the two profiles "particulier" and "entreprise" are custom profiles. But I can't understand why? I thought it was from the use of a hook. Maybe I need to use another hook than "edit_user_profile" for these common data
    – Timama
    Feb 3, 2023 at 17:18

1 Answer 1

0

I found. All users have the same fields. When you add a custom field to a user, it is added to the others. So, in my loop below the text

// ALL THE DATA IN THIS LOOP ARE DELETED

I use the variable $SocialReasons = $_POST['socialreasons_data'] ;

and for this one, it is not specified to use the variable $raisonsociale of the user being updated. Then,when I update a user "Particulier", $raisonsociale is thus empty and the data which results from it are NULL

I added a test
if (!empty($raisonsociale)) and it works !

The code below..... thanks for your help !!

        //Boucle pour modifier le champs chez tous les membres
    $raisonsociale = $_POST['raisonsociale_data'] ;
    if (!empty($raisonsociale))
    {
        $userscommun = new WP_User_Query( array(
            'meta_key'  => 'typem',     
            'orderby'   => 'meta_value',
            'order'     => 'asc',       
            'meta_query' => array(
                array(
                    'key'     => 'raisonsociale',
                    'value'   => $raisonsociale,
                    'compare' => 'LIKE'
                ),
                )
            ) );

            $userfirmas = $userscommun->get_results();

            foreach( $userfirmas as $userfirma ) { 
                $useridcommun = $userfirma->ID;

                update_user_meta( $useridcommun, 'entreprise_nummembre', $_POST['entreprise_nummembre'] );
                update_user_meta( $useridcommun, 'logo_listeentreprise', sa_sanitize_chars($_POST['logo_listeent'])  );
                update_user_meta( $useridcommun, 'width_logoent', $_POST['width_logoent'] );
                update_user_meta( $useridcommun, 'dateinscripsociete', $_POST['dateinscripsociete']  ); 
                update_user_meta( $useridcommun, 'admissionsociete', $_POST['admissionsociete_data']  );    
                update_user_meta( $useridcommun, 'siegerue1', $_POST['siegerue1'] );
                update_user_meta( $useridcommun, 'siegerue2', $_POST['siegerue2'] );
                update_user_meta( $useridcommun, 'siegeplz', $_POST['siegeplz'] );
                update_user_meta( $useridcommun, 'siegeville', $_POST['siegeville'] );
                update_user_meta( $useridcommun, 'siegepays', $_POST['siegepays'] );
                update_user_meta( $useridcommun, 'siteweb', $_POST['siteweb'] );
                update_user_meta( $useridcommun, 'contactanrede', $_POST['contactanrede'] );
                update_user_meta( $useridcommun, 'contacttitel', $_POST['contacttitel'] );
                update_user_meta( $useridcommun, 'contactvorname', $_POST['contactvorname'] );
                update_user_meta( $useridcommun, 'contactnachname', $_POST['contactnachname'] );
                update_user_meta( $useridcommun, 'contactfonction', $_POST['contactfonction'] );
                update_user_meta( $useridcommun, 'contactservice', $_POST['contactservice'] );
                update_user_meta( $useridcommun, 'contacttel', $_POST['contacttel'] );
                update_user_meta( $useridcommun, 'contactmobile', $_POST['contactmobile'] );
                update_user_meta( $useridcommun, 'contactemail', $_POST['contactemail'] );
                update_user_meta( $useridcommun, 'facturationservice', $_POST['facturationservice'] );
                update_user_meta( $useridcommun, 'facturationrue1', $_POST['facturationrue1'] );
                update_user_meta( $useridcommun, 'facturationrue2', $_POST['facturationrue2'] );
                update_user_meta( $useridcommun, 'facturationplz', $_POST['facturationplz'] );
                update_user_meta( $useridcommun, 'facturationville', $_POST['facturationville'] );
                update_user_meta( $useridcommun, 'facturationpays', $_POST['facturationpays'] );
                update_user_meta( $useridcommun, 'facturationpays', $_POST['facturationpays'] );    
            }           
    }


        $oldadmissionent = get_the_author_meta( 'admissionent', $userent_id );
        $newadmissionent = $_POST['admissionent'];

        if ($oldadmissionent !== $newadmissionent && $newadmissionent=='oui') {
            $user_ID = $userent_id;
            do_action( 'mail_admission', $user_ID); 
        }
    update_user_meta( $userent_id, 'admissionent', $_POST['admissionent'] );    
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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