DesignMe Creative Group

207.776.5416 • MAINE & ARIZONA

fb pintrest rssicon
  • Home
  • Services
    • Websites & Social Media
    • Marketing & E-Newsletters
    • Logos & Print
    • Training & Support
  • Portfolio
    • Website Design
    • Creative Design & Branding
    • Social Media & E-Marketing
  • The Team
  • Why DesignMe?
    • Our Philosophy
    • Affordable Prices
    • Getting Started
  • Blog
  • Payment
  • Contact

August 17, 2015 By Debbie 1 Comment

Force WordPress Displayname Format

s2memberlogoDesignMe recently upgraded a website that uses our developers PRO version of s2Member. Part of the project was creating a directory using the plugin’s Member List function. However, the sort option was for display name … and the display name was set to different formats. I needed it to display and sort by last name. After spending a long while looking for solutions, I found this bit of code which forces a the format of the displayname to LAST NAME/FIRST NAME:

(place this in your function.php file)

//Sets the user's display name (always) to first name last name, when it's avail.
add_action ('admin_head','make_display_name_f_name_last_name');
function make_display_name_f_name_last_name(){
 
    $users = get_users(array('fields'=>'all'));
 
    foreach($users as $user){
        $user = get_userdata($user->ID);    
 
        $display_name = $user->last_name . ", " . $user->first_name;
 
        if($display_name!=' ') wp_update_user( array ('ID' => $user->ID, 'display_name' => $display_name) );
            else wp_update_user( array ('ID' => $user->ID, 'display_name' => $user->display_login) );
 
        if($user->display_name == '')
            wp_update_user( array ('ID' => $user->ID, 'display_name' => $user->display_login) );
    }
}

Happy Coding!

Filed Under: Code Tips, TechTalk, WordPress

Comments

  1. Damian says

    September 21, 2016 at 5:27 am

    Just want give my 2 cents on this:

    The above code when implemented slowed my Admin Dashboard to a crawl (only the backend, frontend was fine).
    Using Query Monitor, I realized that this code was the culprit as it accounted for more than 14s worth of query time.
    If your website has a large number of members like mine, this is what will happen to you too.

    I found an alternative method that works just as well without the load.

    http://geektamin.com/blog/533/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead/

    Cheers

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Site Search

From Our Blog

  • 10 Reasons You Need a Custom Website
  • The Squatty Dog
  • The “ME” In Our Name Really Means YOU
  • Behind Your Website
  • I Have a Website – Now What?

Tech Talk

  • Enqueue CSS2 Google Fonts
  • Redirect to the BuddyPress Profile Page
  • “Going Live”
  • Insert Soliloquy Shortcode Using Advanced Custom Fields
  • Redirect My Website To https://

Creative Corner

  • Unique Websites Deserve a Unique Login
  • Adding A Shortcode To The Post Title
  • Favicons – A Tiny Graphic With Big Impact
  • Social Media Icons – So Many Options!
  • Savage Chicken iMugs

View Topics

creative

Functional

affordable

Copyright © 2025 · Custom Website by DesignMe Creative Group · Log in
Hosted Locally by Maine Hosting Solutions