3

I've hidden the username column from users administration with the following code:

function modify_user_columns($column) {
    $column = array(
        "cb" => "<input type=\"checkbox\" />",
        "username" => __('Username'),
        "name" => __('Name'),
        "email" => "E-mail",
        "birthdate" => "Narozeniny",
        "sleva_moto" => "Sleva moto"


    );
    return $column;
}
add_filter('manage_users_columns','modify_user_columns');

The problem is I cannot enter details of any user now, as normally the user detatils are accessed when clicking on the username. Is there a way to add the link to detail page to the Name column?

1

There are no direct filters using which we can change the content of Name column. So to get what you want, we should remove 'Name' column too and create our own Name column. Therefore, modify_user_columns function will be like this

function modify_user_columns($column) {
    $column = array(
        "cb" => "<input type=\"checkbox\" />",
        "wdm_name" => __('Name'),
        "email" => "E-mail",
        "birthdate" => "Narozeniny",
        "sleva_moto" => "Sleva moto"


    );
    return $column;
}

Now, we have a control on what we can show in the Name column. We can add content in that Name column using filter manage_users_custom_column.

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

function wdm_display_name_with_edit_link($content, $column_name, $user_object_id){
    if ($column_name == 'wdm_name' && 
        current_user_can( 'edit_user',  $user_object_id) ) {

        //get info of user
        $user_object = get_userdata($user_object_id); 

        //generate edit link
        $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user_object->ID ) ) ); 

        //Generate the complete Name with edit link
        $content = "<strong><a href=\"$edit_link\">$user_object->first_name $user_object->last_name</a></strong><br/>"; 
    } else {
        //If logged in user does not have rights 
        //to edit users, just show Name of users
        $content = "<strong>$user_object->first_name $user_object->last_name</strong><br />"; 
    }

    return $content;
}

Now it will show Names with Edit link. Hope this helps. :)

0

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.