Wordpress - How to include new fields in the user table?

1

I want to include the following fields in the register of registered users in my blog: IBGE, Municipality and UF.

How do I include these fields and later access them on a specific page?

    
asked by anonymous 19.10.2016 / 01:09

2 answers

2

Use the wp_user_meta table. Wordpress manages the database for you so it is neither necessary nor advisable to change the structure of existing tables. To associate new information with the user use:

$userId = get_current_user_id();
$nomeCampo = 'uf';
$valor = 'SP';
update_user_meta($userId, $nomeCampo, $valor);

The documentation defines the function of the following form :

update_user_meta(int $user_id, string $meta_key, mixed $meta_value, mixed $prev_value = '');

To redeem the information saved elsewhere use:

$userId = get_current_user_id();
$nomeCampo = 'uf';
get_user_meta($userId, $nomeCampo);

The documentation defines the function of the following form :

get_user_meta(int $user_id, string $key = '', bool $single = false);
    
19.10.2016 / 01:42
2

Should serve. Just change the code, changing the social networks for your need link

In functions.php:

<?php  function my_show_extra_profile_fields( $user ) { ?>
<table class="form-table">
  <tr>
    <th><label for="ibge">IBGE</label></th>
    <td><input type="text" name="ibgeuser" id="ibgeuser" value="<?php echo esc_attr( get_the_author_meta( 'ibgeuser', $user->ID ) ); ?>" class="regular-text" />
      <br />
      <span class="description">IBGE</span></td>
  </tr>

  <tr>
    <th><label for="municipiouser">Município</label></th>
    <td><input type="text" name="municipiouser" id="municipiouser" value="<?php echo esc_attr( get_the_author_meta( 'municipiouser', $user->ID ) ); ?>" class="regular-text" />
      <br />
      <span class="description">Município</span></td>
  </tr>

  <tr>
    <th><label for="ufuser">UF</label></th>
    <td><input type="text" name="ufuser" id="ufuser" value="<?php echo esc_attr( get_the_author_meta( 'ufuser', $user->ID ) ); ?>" class="regular-text" />
      <br />
      <span class="description">UF</span></td>
  </tr>
</table>

To save the data entered by the user:

<?php
// GUARDAR E MANTER INFO DOS CAMPOS
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );

function my_save_extra_profile_fields( $user_id ) {

    if ( !current_user_can( 'edit_user', $user_id ) )
        return false;
    update_usermeta( $user_id, 'ibgeuser', $_POST['ibgeuser'] );
    update_usermeta( $user_id, 'municipiouser', $_POST['municipiouser'] );
    update_usermeta( $user_id, 'ufuser', $_POST['ufuser'] );
} ?>

To use db information, <?php echo $curauth->ibgeuser; ?> , and so on.

    
19.10.2016 / 01:32