Adding a custom wp_usermeta into a WordPress User Profile

For my WordPress users, I add a lot of custom wp_usermeta attributes.

Within the WordPress Database, there are two tables for user-specific data. They are the wp_users (which contains information like the student’s email, name, password, etc), and the wp_usermeta (which contains everything else). For more information about the way WordPress sets up their tables, check out the documentation.

You can store custom data in the wp_usermeta.

Example:

To explain how I built it in my courses — In the Business Ignition Bootcamp course, students can get ‘STRIKES’ if they fail to submit their homework on time. I can store how many strikes a user has in the wp_usermeta.

  • When students create their account, I set up a usermeta called ‘bib_strikes’ into their usermeta.
  • Students have to submit their homework via GravityForms before a specific due date.
  • On that specific due date, I have CRON job that runs another piece of code that checks to see if they submitted the homework or not.
  • If they did NOT submit it, it runs another piece of code that adds a +1 to the ‘bib_strikes’ for that student, storing that data.
  • On the front end, the website displays how many strikes the user has. If they failed to submit, they now have 1 strike, based on that ‘bib_strikes’ data.

Other Use Cases:

  1. You can store if a user completed a tutorial in the wp_usermeta.
  2. You can track their student progress and store it here as well.
  3. You can notate if the student has clicked on a specific button, set a certain option, or even flag them that they reached out to support.

The options are endless.

But what about changing things?

It’s easy to update data by using the update_user_meta().

But let’s say my code gave a student a strike, and the student apologizes and wants it removed.

There’s two ways to change that value:

METHOD 1:

You can change it through the WordPress database itself. Visit myPHPAdmin, that opens up the WordPress mySQL database in a web format.

You then navigate to the wp_usermeta table, find the user, find the meta_value, and modify.

Notice that user ID 1 has 6 strikes. User ID 1 is often an admin, btw.

The challenge with this method is that if you have support who aren’t familiar with this, they can really break things. (And do you really want to give a non-developer access to something that can ruin your entire site?)

METHOD 2: (Better way)

The better way is to add this snippet of code to a plugin or functions.php.

/******
 * This code shows you custom content within the user profile
 * 
 * 
 *  ****/

// Hooks near the bottom of profile page (if current user) 
add_action('show_user_profile', 'custom_user_profile_fields');

// Hooks near the bottom of the profile page (if not current user) 
add_action('edit_user_profile', 'custom_user_profile_fields');


// @param WP_User $user
function custom_user_profile_fields( $user ) {

?>
    <table class="form-table">
        <tr>
            <th>
                <label for="code"><?php _e( 'Total BIB Strikes(if 3+, then they are kicked)' ); ?></label>
            </th>
            <td>
                <input type="text" name="code" id="code" value="<?php echo esc_attr( get_the_author_meta( 'bib_strikes', $user->ID ) ); ?>" class="regular-text" />
            </td>
        </tr>
    </table>
<?php
}


// Hook is used to save custom fields that have been added to the WordPress profile page (if current user) 
add_action( 'personal_options_update', 'update_extra_profile_fields' );

// Hook is used to save custom fields that have been added to the WordPress profile page (if not current user) 
add_action( 'edit_user_profile_update', 'update_extra_profile_fields' );

function update_extra_profile_fields( $user_id ) {
    if ( current_user_can( 'edit_user', $user_id ) )
        update_user_meta( $user_id, 'bib_strikes', $_POST['code'] );
}

For your use – change the following:

Leave a Comment