WordPress

dostosowywanie — niektóre dane jednego niestandardowego profilu użytkownika są usuwane, gdy aktualizuję inny niestandardowy profil użytkownika

  • 3 lutego, 2023
  • 13 min read
dostosowywanie — niektóre dane jednego niestandardowego profilu użytkownika są usuwane, gdy aktualizuję inny niestandardowy profil użytkownika


Mam pewne problemy z niestandardowym profilem użytkownika. Stworzyłem 2 różne profile użytkowników:

  • jeden nazywa się „particulier”
  • drugi nazywa się „przedsiębiorczość”

Możesz zobaczyć kod poniżej. Chodzi o to, że na początku wszystkie pola są wypełnione, dla wszystkich użytkowników dla wszystkich użytkowników, niezależnie od ich profilu.

Wchodzę na profil „konkretnego” użytkownika. Modyfikuję wartość pola Aktualizuję użytkownika.

a następnie niektóre pola stają się puste. Są to pola dla użytkowników, których profil to „przedsiębiorca”.

Mówiąc dokładniej, są to pola, które są gromadzone dla wszystkich użytkowników należących do tej samej firmy (określiłem lokalizację tych pól lub zmiennych w kodzie).

Naprawdę nie rozumiem, dlaczego te dane są usuwane … Naprawdę mam nadzieję, że możesz mi pomóc. Od kilku dni próbuję znaleźć rozwiązanie. Wielkie dzięki za pomoc.

kod to:

<?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'] );
            }               
    }


Źródło

Warto przeczytać!  php — Punkt końcowy istnieje i pojawia się w panelu Moje konto. Należy dodać punkt końcowy do menu rozwijanego Moje konto w motywie Porto