0

I tried to add extra user profile fields. After that I want so show these extra fields using a shortcode. I have added the following code to my functions.php file to add the extra fields and save them to database:

<!-- add extra fields to user profile -->
<?php
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );

function extra_user_profile_fields( $user ) { ?>
    <h3><?php _e("Extra profile information", "blank"); ?></h3>

    <table class="form-table">
    <tr>
        <th><label for="school-id"><?php _e("KlickSchool ID"); ?></label></th>
        <td>
           <input type="text" name="school-id" id="school-id" value="<?php echo      esc_attr( get_the_author_meta( 'school-id', $user->ID ) ); ?>"     class="regular-text" /><br />
           <span class="description"><?php _e("Bitte geben Sie die ID für die     Niederlassung an"); ?></span>
        </td>
    </tr>
    <tr>
       <th><label for="city"><?php _e("Standort"); ?></label></th>
       <td>
         <input type="text" name="city" id="city" value="<?php echo esc_attr( get_the_author_meta( 'city', $user->ID ) ); ?>" class="regular-text" /><br />
         <span class="description"><?php _e("Bitte geben Sie den Standort der Niederlassung ein"); ?></span>
       </td>
    </tr>
   </table>
<?php }

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

function save_extra_user_profile_fields( $user_id ) {

     if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }

     update_user_meta( $user_id, 'school-id', $_POST['school-id'] );
     update_user_meta( $user_id, 'city', $_POST['city'] );
}
?>

I tried to display one extra field with this code:

<?php
( 'adress-klickschool', 'klickschool_adress' );

function klickschool_adress() {
?>

      <div class="flex_column av_one_half  flex_column_div av-zero-column-padding avia-builder-el-2  el_after_av_one_half  el_before_av_codeblock adress-column">
    <?php
     $current_user = wp_get_current_user();

    /*
    * @example Safe usage: $current_user = wp_get_current_user();
    * if ( ! ( $current_user instanceof WP_User ) ) {
    *     return;
    * }
    */
    echo'<div class="image-info-box">','<img src="http://intranet.klickschool.de/wp-content/uploads/2019/11/logo-sh.svg">', '</div>';?>
**<?php if ( get_the_author_meta( 'school-id' ) ) { ?>
    <p class="school-id">
        <p><?php the_author_meta( 'school-id' ); ?></p>
    </p>
<?php } ?>**
<?php
echo '<div class="info-box">' . $current_user->description . '<br />', '</div>';
?>
</div>      
<?php } ?>

The code marked with the two stars should display the extra added field "school-id". But the code does not show this field. Do you have any ideas?

1

You should use get_user_meta instead of get_the_author_meta(). because get_the_author_meta() has Valid values for the $field parameter include: get_the_author_meta

you can take reference from below code.

$school_id = get_user_meta( $current_user->ID, 'school-id',true);
if ( !empty($school_id) ) { ?>
    <p class="school-id">
        <p>
           <?php echo $school_id; # what you want to do with school id ?>
        </p>
    </p>
<?php }
  • thanks! That works – tom84 Nov 12 at 12:49

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.