如何为额外的用户元字段保存标签并在以后显示它?

时间:2013-01-25 作者:Iurie

下面是我当前添加、保存和显示额外用户元字段的方式:

/* 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
}
这样,只要使用一个短代码,我就可以同时显示多个元数据。现在我想修改这段代码,以便能够为每个用户元字段保存一个标签,并在以后将其与相应的值一起显示。如何使用我的代码执行此操作?

1 个回复
最合适的回答,由SO网友:birgire 整理而成

您还可以在短代码中使用label参数,如下所示:

[user_meta user_id="1" label="publications"]
通过这些代码更改:

* 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:

好吧,也许你的意思是这样的:

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)

结束

相关推荐