John
John

Reputation: 178

In WordPress, how do I target a specific user and adapt the attached code?

I am trying to change certain elements for a certain user, in the back administration area of WordPress.

I have found the following code which works for a specific role, but how do I change it to target a specific user?

function wpa66834_role_admin_body_class( $classes ) {
    global $current_user;
    foreach( $current_user->roles as $role )
        $classes .= ' role-' . $role;
    return trim( $classes );
}
add_filter( 'admin_body_class', 'wpa66834_role_admin_body_class' );

To clarify: instead of identifying role in the foreach section and then adding the role to the class name, I would like it to identify the user by their specific name or ID, and then add that name or ID to the class name.

Upvotes: 2

Views: 324

Answers (1)

Sheedo
Sheedo

Reputation: 554

You should be able to use get_current_user_id() to (as the name of the function suggests) get the current user's id, and then perform a simple if statement to compare it with a specific ID number.

Example:

function wpa66834_role_admin_body_class( $classes ) {
    $user_id = get_current_user_id();
    // Assuming 13 is the user's ID you're targeting
    if( $user_id == 13 )
        $classes .= ' custom-class-for-user-13';
    return trim( $classes );
}
add_filter( 'admin_body_class', 'wpa66834_role_admin_body_class' );

More info on how to use the function can be found here.

Upvotes: 1

Related Questions