1

I m currently working on a project that involves me creating a custom registration form. This form after submission adds the user data to the wp users table, then inserts the user ID to the wp options table. I can see all the users that register. But I will like to customize the table that lists all users to show custom fields. And also a link that I can use to activate users (Changing a field set in the wp options table to true).
Please I need help

1

For the first part of your question, you can add new columns to the users table.

Its in two steps: first you need to register the columns, then output information for each row.

To add new columns, you hook into the manage_users_columns filter:

function yourdomain_manage_users_columns( $columns ) {

    // $columns is a key/value array of column slugs and names
    $columns[ 'custom_field' ] = 'Custom Field';

    return $columns;
}

add_filter( 'manage_users_columns', 'yourdomain_manage_users_columns', 10, 1 );

Then you need to output your custom field using the manage_users_custom_column filter:

function yourdomain_manage_users_custom_column( $output, $column_key, $user_id ) {

    switch ( $column_key ) {

        // look for the slug you registered
        case 'custom_field' :

            // get your custom field, parse it however you want
            $value = get_user_meta( $user_id, 'custom_field', true );

            // return the value
            return $value;

            break;
        default: break;
    }

    // if no column slug found, return default output value
    return $output;
}

add_filter( 'manage_users_custom_column', 'yourdomain_manage_users_custom_column', 10, 3 );

As for user activation:

Maybe the WP_options table isn't the best way to go about it, as it will grow quickly, slow your website be hard to maintain.

You might want to create a new User Role for non-activated users and set it as default; which you can change easily through the admin interface.

If you are adding this code via a plugin, you might do:

function yourdomain_add_user_role() {

    // capabilities
    $caps = array(                   
        'level_0'   => true,      // inherit subscriber capabilities
        'read'      => false      // but can't read posts, etc
    );

    // ads the inactive user role
    add_role( 
        'inactive',               // Role slug
        'Inactive Account',       // Role name
        $caps                     // capabilities
    );
}

register_activation_hook( __FILE__, 'yourdomain_add_user_role' );

Have a look at the codex for a (long) list of user roles and capabilities: https://codex.wordpress.org/Roles_and_Capabilities

Hope that helps!

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.