0

I would like to extend user data, adding picture, department, phone, etc. You may achieve this adding custom fields to the usermeta, however I wonder if this techniques is deprecated and it would be better to create new tables with custom data to prevent future potential pitfalls when WP is updated.

What is the right way?

3
  • User meta isn't deprecated, and never has been, can you point us to where you were informed of that? User meta is a critical component of WP Core that many core features rely upon
    – Tom J Nowell
    Commented Jun 19, 2016 at 16:30
  • I thought that customizing WP tables could expose to future incompatibility... If this is not the case it may be easier and more natural to add fields to an existing meta table?
    – Riccardo
    Commented Jun 19, 2016 at 16:33
  • Maybe we've misunderstood, I'll write an answer
    – Tom J Nowell
    Commented Jun 19, 2016 at 16:57

2 Answers 2

2

You should avoid adding new tables or customising the existing tables to add fields for users.

If you want to store a phone number etc, do it using user meta:

add_user_meta( $user_id, 'riccardo_phone_number', '0123-456-7890' );
....
$phone_number = get_user_meta( $user_id );

User meta is a key and a value with a post ID in the database. There is no need for changes to the table structure/schema to add new information.

Modifying the WP_User table will cause major issues with future upgrades of WordPress. When WordPress Core modifies their Database schema, you run the risk of it failing on update, or data destruction.

Creating new tables and modifying the core tables also means writing custom SQL queries, duplicating the main query, and giving up all the APIs for data access, as well as introducing a new point of failure. Custom post types, post meta, and custom taxonomies are almost always a better choice, bringing other benefits with them.

1
  • +! for the alternative solutions and clarity on what gets impacted with custom development.
    – klewis
    Commented Oct 21, 2016 at 12:37
2

Creating a table for your WordPress database requires a considerable amount of time. Wordpress Codex has an article with a step by step guide on how to do this, which basically consists of creating a plugin and adding the functions to create a table and trigger the function on plugin install.

1. Creating the Tables:

Thew function is this one:

<?php

global $jal_db_version;
$jal_db_version = '1.0';

function jal_install() {
    global $wpdb;
global $jal_db_version;

$table_name = $wpdb->prefix . 'liveshoutbox';

$charset_collate = $wpdb->get_charset_collate();

$sql = "CREATE TABLE $table_name (
    id mediumint(9) NOT NULL AUTO_INCREMENT,
    time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
    name tinytext NOT NULL,
    text text NOT NULL,
    url varchar(55) DEFAULT '' NOT NULL,
    UNIQUE KEY id (id)
) $charset_collate;";

require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );

add_option( 'jal_db_version', $jal_db_version );
}

function jal_install_data() {
    global $wpdb;

    $welcome_name = 'Mr. WordPress';
    $welcome_text = 'Congratulations, you just completed the installation!';

    $table_name = $wpdb->prefix . 'liveshoutbox';

    $wpdb->insert( 
        $table_name, 
        array( 
            'time' => current_time( 'mysql' ), 
            'name' => $welcome_name, 
            'text' => $welcome_text, 
        ) 
    );
}

2. Calling the functions:

On your Plugin file

register_activation_hook( __FILE__, 'jal_install' );
register_activation_hook( __FILE__, 'jal_install_data' );

The original Article can be found here: Creating Tables with Plugins


You can try this option if you really wish to learn by doing it; however, if you are working on a project, I recommend using a very well maintained plugin Called "Advanced Custom Fields" which currently has over 1 million installs and a 5 star rating. This plugin lets you easily add custom User Meta with a GUI which saves a considerable amount of time.

2
  • I have tested the tool and was wondering if this was the correct way. I think I will proceed this way, adding custom fields to xxxmeta tables. Thanks a lot for sharing your thoughts
    – Riccardo
    Commented Jun 19, 2016 at 17:24
  • This plugin lets you easily pull this information with their built in functions the_field() and get_the_field() if you wish to store it in a varIable for displaying it on your template. Good luck and happy coding! Commented Jun 19, 2016 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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