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

Asked

Viewed 137 times

1

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

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

2 answers

2

It should do. Just change the code, switching social networks for your need http://www.escolawp.com/2011/12/como-adicionar-mais-campos-no-perfil-de-usuarios-do-wordpress/

Na 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.

  • It is interesting that the main content of the publication is on the site and not on external links. Put what you saw on the link in your reply.

2


Use the table wp_user_meta. Wordpress manages the database for you so that it is neither accurate nor advisable to change the structure of existing tables. To associate new information to 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 rescue 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.