0

I have add new columns on wp_users and in /wp-admin/user.edit.php I add new fields and connect this to database, so if I put data on the database manually, the new fields recive that, but if I update or insert data on WordPress site in the new fields it don't update on the database and stay with same data I have put on the database manually. What's wrong with this? Thank you.

  • 1
    First of all, never make any changes in WP core files, it will get flush off after WP updates to new version. Instead you can add custom user profile fields using hooks or plugin. – Jiten Gaikwad Jun 20 '17 at 15:35
  • I already tried plugins and they do not save the information in the database and what I need is that I store in the database the various information corresponding to the various columns that I created in the database because I need to tinker with the data using mirthconnect. There is a plugin that stores the information in the database but it stores several information in a single column. – João Varanda Jun 20 '17 at 16:03
  • 1
    As Jiten said modifying Wordpress core files or core Wordpress tables is a really bad idea. Take a look at the Advanced Custom Fields plugin it makes things like adding custom fields to wp_users a lot easier, and doesn't break anything in the Wordpress core. – Picard Jun 20 '17 at 19:43
  • I already tried to use the plugin you indicated but it does not create the fields in the wp_users table, supposedly saves the data idependently. – João Varanda Jun 21 '17 at 9:25
  • If you use plugin, it saves custom field value in wp_usermeta table in key value format. If you add city field, you can get value using get_user_meta($userid, 'city', true); – Jiten Gaikwad Jun 21 '17 at 16:35
2

You're going to definitely want to do this with a plugin. Modifying WordPress Core files is an extremely bad idea as others have pointed out because every update will wipe them out and future versions may change the location/function of what you're editing. You can get away by adding it to the theme's functions.php file as well but you run into the same problem as with WP Core, updates will wipe them out. Writing a Plugin is your best option.

Say you wanted to add a field for a user to define their favorite color. You first would do the following:

function my_extra_author_fields( $user ) { ?>
    <h3>My Extra Author Fields</h3>

    <table class="form-table">
        <tr>
            <th><label for="favorite_color">Favorite Color</label></th>
            <td>
                <input type="text" name="favorite_color" id="favorite_color" class="regular-text" value="<?php esc_attr( get_the_author_meta( 'favorite_color', $user->ID ) ); ?>" />
                <br />
                <span class="description">Please enter your favorite color</span>
            </td>
        </tr>
    </table>
<?php }

add_action( 'show_user_profile', 'my_extra_author_fields' );
add_action( 'edit_user_profile', 'my_extra_author_fields' );

This will create the new fields in the form for user profile but will not actually save anything yet. That part comes next:

function save_my_extra_author_fields( $user_id ) {

    // Check to see if user can edit this profile
    if ( ! current_user_can( 'edit_user', $user_id ) )
        return false;

    update_user_meta( $user_id, 'favorite_color', $_POST['favorite_color'] );
}

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

You will need to do a new <tr> element and update_user_meta() call for each custom field you want to add.

You can then access the value of these fields with get_the_author_meta( 'favorite_color' ) if you want to use the return programmatically such as testing to see if the favorite color is set or use the_author_meta( 'favorite_color' ) to simply echo it.

  • Where do I add the code you told me? – João Varanda Jun 20 '17 at 23:14
  • 1
    In a fresh plugin file in /wp-content/plugins/{pluginname} – Cedon Jun 21 '17 at 0:29
  • I have no idea how to do it, supposedly just paste the two functions you gave me, add my fields and save a file in php and put it in the root folder of plugins right? The problem is that I did this and the plugin does not appear on the site. – João Varanda Jun 21 '17 at 8:58
  • 1
    You create a new .php file in a folder under /wp-content/plugins/. At the top of the file place the comment block, one very similar to what you put in style.css for a theme, and then paste in those functions. – Cedon Jun 23 '17 at 0:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.