0

Bellow is how I currently add, save and display an extra user meta field:

/* Add Extra Fields to the User Profile */
function extra_user_profile_fields( $user ) { 
  ?>
    <table class="form-table">
    <tr>
    <th><label for="publications"><?php _e("Publications"); ?></label></th>
    <td>
    <textarea rows="10" cols="450" name="publications" id="publications"  class="regular-text" />
    <?php echo esc_attr( get_the_author_meta( 'publications', $user->ID ) ); ?>
    </textarea><br />
    </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' );

/* 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, 'publications', $_POST['publications'] );
}

/* Display the selected user meta data with a shortcode */
add_shortcode('user_meta', 'user_meta_shortcode_handler');
/* usage: [user_meta user_id=1] */
function user_meta_shortcode_handler($atts,$content=null){ ?>
  <?php
    echo '<h3>Publicații</h3>';
    echo wpautop(get_user_meta($atts['user_id'], 'publications', true));
  ?>
  <?php
}

In this way, with only one shortcode I can display multiple metadata at the same time. Now I want to modify this code to be able to save also a label for each user meta field and to display it/them later together with the corresponding value. How to do this with my code?

1
  • I do not want to insert a label parameter in the shortcode, as suggested @birgire.
    – Yuri
    Commented Jan 25, 2013 at 20:36

1 Answer 1

1

you could also use a label parameter in your shortcode, like this:

[user_meta user_id="1" label="publications"]

with these code changes:

* Display the selected user meta data with a shortcode */
add_shortcode('user_meta', 'user_meta_shortcode_handler');

/* usage: [user_meta user_id="1" label="publications"] */
function user_meta_shortcode_handler($atts,$content=null){ 
     extract( shortcode_atts( array(
         'user_id' => '1',
         'label' => 'publications'
     ), $atts ) );          

    $output.='<h3>'.$label.'</h3>';
    $output.= wpautop(get_user_meta($user_id, 'publications', true));
    return $output;
}

EDIT:

ok, maybe you mean something like this:

add_action( 'show_user_profile', 'extra_user_profile_fields' );

/* Add Extra Fields to the User Profile */
function extra_user_profile_fields( $user ) { 
  ?>
    <table class="form-table">
    <tr>
        <th><label for="my_label"><?php _e("Publications Label"); ?></label></th>
        <td>
            <input type="text" value="<?php echo get_the_author_meta( 'my_label', $user->ID ); ?>" name="my_label" id="my_label"/>
        </td>
    </tr>
    <tr>
        <th><label for="my_publications"><?php _e("Publications"); ?></label></th>
        <td>
            <textarea rows="10" cols="450" name="my_publications" id="my_publications"  class="regular-text" /><?php echo get_the_author_meta( 'my_publications', $user->ID ); ?></textarea>
        </td>
    </tr>
    </td>
    </table>
  <?php 
}

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

/* 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, 'my_label', esc_attr($_POST['my_label']) );
    update_user_meta( $user_id, 'my_publications', esc_attr($_POST['my_publications']) );
}

/* Display the selected user meta data with a shortcode */
add_shortcode('user_meta', 'user_meta_shortcode_handler');
/* usage: [user_meta user_id="1"] */
function user_meta_shortcode_handler($atts,$content=null){ 
     extract( shortcode_atts( array(
         'user_id' => '1',
     ), $atts ) );          

    $output.='<h3>';
    $output.= wpautop(get_user_meta($user_id, 'my_label', true));
    $output.='</h3>';
    $output.= wpautop(get_user_meta($user_id, 'my_publications', true));
    return $output;
}

extra user profile fields (publications textarea + label input)

4
  • The idea is that with the initial shortcode I can display multiple metadata with (potentially) multiple labels at the same time (I mean with only one shortcode), but with yours not.
    – Yuri
    Commented Jan 25, 2013 at 20:23
  • I updated the above code.
    – birgire
    Commented Feb 3, 2013 at 11:51
  • Thank you! The second version is very close to what I need! Now, I think, I can adapt/customize it myself. Thank you again!
    – Yuri
    Commented Feb 3, 2013 at 16:49
  • ok great ;-) I also used a "return" instead of "echo" in the shortcode function, otherwise it will always display the shortcode content before the post content. Good luck with your project.
    – birgire
    Commented Feb 3, 2013 at 17:20

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.