Using WordPress ‘insert_user_meta’ PHP filter

The insert_user_meta WordPress PHP Filter allows you to modify a user’s meta values and keys right after the user is created or updated, and before any user meta is inserted or updated.

Usage

add_filter('insert_user_meta', 'your_custom_function', 10, 3);

function your_custom_function($meta, $user, $update) {
    // your custom code here
    return $meta;
}

Parameters

  • $meta (array): Default meta values and keys for the user.
  • $user (WP_User): User object.
  • $update (bool): Whether the user is being updated rather than created.

More information

See WordPress Developer Resources: insert_user_meta

Examples

Add custom user meta field

Add a custom user meta field called job_title.

add_filter('insert_user_meta', 'add_job_title_meta', 10, 3);

function add_job_title_meta($meta, $user, $update) {
    $meta['job_title'] = 'Web Developer';
    return $meta;
}

Modify the default admin color

Change the default admin color scheme for new users.

add_filter('insert_user_meta', 'change_default_admin_color', 10, 3);

function change_default_admin_color($meta, $user, $update) {
    if (!$update) {
        $meta['admin_color'] = 'light';
    }
    return $meta;
}

Disable rich editing for a specific user role

Disable the rich editor for users with the ‘editor’ role.

add_filter('insert_user_meta', 'disable_rich_editing_for_editors', 10, 3);

function disable_rich_editing_for_editors($meta, $user, $update) {
    if (in_array('editor', $user->roles)) {
        $meta['rich_editing'] = 'false';
    }
    return $meta;
}

Force SSL for administrators

Force SSL for users with the ‘administrator’ role.

add_filter('insert_user_meta', 'force_ssl_for_administrators', 10, 3);

function force_ssl_for_administrators($meta, $user, $update) {
    if (in_array('administrator', $user->roles)) {
        $meta['use_ssl'] = 1;
    }
    return $meta;
}

Set locale based on user role

Set a different locale for users with the ‘translator’ role.

add_filter('insert_user_meta', 'set_locale_for_translators', 10, 3);

function set_locale_for_translators($meta, $user, $update) {
if (in_array(‘translator’, $user->roles)) {
$meta[‘locale’] = ‘es_ES’;
}
return $meta;
}