Show extra user profile meta for current user

I tried to add extra user profile fields. After that I want so show these extra fields using a shortcode. I have added t

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=".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?

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?

Share Improve this question edited Nov 15, 2019 at 23:11 butlerblog 5,1213 gold badges28 silver badges44 bronze badges asked Nov 12, 2019 at 11:39 tom84tom84 2551 gold badge6 silver badges21 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 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 }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745000302a4605452.html

相关推荐

  • Show extra user profile meta for current user

    I tried to add extra user profile fields. After that I want so show these extra fields using a shortcode. I have added t

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信