WordPress: extended user information

The information about a user or author of a WordPress blog can be extended easily. This could for example be useful to insert the date of birth, bibliographic information, the address of the workplace or, extremely comfortable, a camp for special permission - to check later in the template.
The operation you are going to do does not require a plugin, but the code you need can be placed in the - usual - file functions.php .

Note: For more articulated situations the use of a Plugin might be the best choice. Acting on the file functions.php tie our code to the theme. A plugin, especially if the code written is "abundant", is easier to reuse.

Let us open the file functions.php and start writing the code that will show you our additional field in the administration of WordPress:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/ / This function - see add_action () below, deals with display
/ / Add our camp in the administration of WordPress

nuovo_user_meta function ($ user) {
?>
<h3> Additional Fields </ h3>
<table class="form-table">
<tr>
<th> <label for="nuovo_user_meta"> New Field </ label> </ th>
<td>
esc_attr ( get_the_author_meta ( 'nuovo_user_meta' , $user -> ID ) ) ; ?> " class="regular-text" /><br /> <Input type = "text" name = "nuovo_user_meta" id = "nuovo_user_meta" value = "<? Php echo esc_attr (get_the_author_meta ('nuovo_user_meta', $ user -> id));?>" Class = "text-regular" /> <br />
<span class="description"> enter your additional field. </ span>
</ Td>
</ Tr>
</ Table>
<? Php}

/ / Add our function to the administration of WordPress
/ / In this "simple" case, the "exhibition" (show_user_profile)
/ / And the "change" (edit_user_profile) coincide, but in cases
/ / Could be more articulate different
, 'nuovo_user_meta' ) ; add_action ('show_user_profile', 'nuovo_user_meta');
, 'nuovo_user_meta' ) ; add_action ('edit_user_profile', 'nuovo_user_meta');

Going to peek in the administration, selecting your user, you will have the new field. But for now the new field is only displayed, but will do a few other lines of code to manage its storage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/ / Store, for user $ user_id, identified as a new field
/ / 'Nuovo_user_meta'
$user_id ) { nuovo_user_meta_update function ($ user_id) {
/ / Only those with permits editing
! current_user_can ( 'edit_user' , $user_id ) ) return false ; if (! current_user_can ('edit_user', $ user_id)) return false;

/ * Copy and paste this line for additional fields. Make sure to change 'twitter' to the ID field. * /
, 'nuovo_user_meta' , $_POST [ 'nuovo_user_meta' ] ) ; update_usermeta ($ user_id, 'nuovo_user_meta', $ _POST ['nuovo_user_meta']);
}
/ / In this case we use the add_action () to add
/ / Our piece of code. Note that we allow the update
/ / Both the user and displays their profile (personal_options_update)
/ / That any user or administrator who has permission to edit (edit_user_profile_update)
, 'nuovo_user_meta_update' ) ; add_action ('personal_options_update', 'nuovo_user_meta_update');
, 'nuovo_user_meta_update' ) ; add_action ('edit_user_profile_update', 'nuovo_user_meta_update');

At this point, our additional field is complete and nothing remains but to use it. : To retrieve a field of this type simply call the function get_the_author_meta() or the_author_meta() :

1
'nuovo_user_meta' ) ?> </p> <p> additional field: <? php the_author_meta ('nuovo_user_meta')?> </ p>

5 comments to "WordPress: extended user information"

  1. March 23, 2010 News from the blog on Wordpress: how to change the user name ADMIN :

    [...] WordPress: The extension of the user information about a user or author of a WordPress blog can be extended easily. This could for example be useful to insert the date of birth, bibliographic information, the address of the workplace or, extremely comfortable, a camp for special permission - to check later in the template. Blog: undolog | read more [...]

  2. March 28, 2010 Pine:

    I think there is an error in the writing of the first block of code ... you can check? If I change my functions.php file by adding this site crashes ... What's wrong?
    Thank you.

  3. March 28, 2010 Giovambattista Fazioli :

    @ Pino: what version of WordPress you've tried? Have you by chance a few error message on the screen?

  4. March 28, 2010 Pine:

    Using WordPress 2.9.2

    This is my functions.php file:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    <Php
    function_exists ( 'register_sidebars' ) ) if ( function_exists ('register_sidebars'))
    ) ; register_sidebars (2);
    ?>

    / / This function - see add_action () below, deals with display
    / / Add our camp in the administration of WordPress

    nuovo_user_meta function ($ user) {
    ?>
    <h3> Additional Fields </ h3>
    <table class="form-table">
    <tr>
    <th> <label for="nuovo_user_meta"> New Field </ label> </ th>
    <td>
    esc_attr ( get_the_author_meta ( 'nuovo_user_meta' , $user -> ID ) ) ; ?> " class="regular-text" /><br /> <Input type = "text" name = "nuovo_user_meta" id = "nuovo_user_meta" value = "<? Php echo esc_attr (get_the_author_meta ('nuovo_user_meta', $ user -> id));?>" Class = "text-regular" /> <br />
    <span class="description"> enter your additional field. </ span>
    </ Td>
    </ Tr>
    </ Table>
    <? Php}

    / / Add our function to the administration of WordPress
    / / In this "simple" case, the "exhibition" (show_user_profile)
    / / And the "change" (edit_user_profile) coincide, but in cases
    / / Could be more articulate different
    , 'nuovo_user_meta' ) ; add_action ('show_user_profile', 'nuovo_user_meta');
    , 'nuovo_user_meta' ) ; add_action ('edit_user_profile', 'nuovo_user_meta');

    that gives me the error is:

    1
    Parse error: syntax error, unexpected '}' in / var / www / virtual / ... / functions.php on line 24
  5. March 29, 2010 Giovambattista Fazioli :

    @ Pino: it would seem a brace not closed properly ...

Leave a comment

XHTML TAG PERMIT: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA