{"id":533,"date":"2015-04-16T15:18:24","date_gmt":"2015-04-16T03:18:24","guid":{"rendered":"http:\/\/geektactics.geektamin.com\/?p=533"},"modified":"2024-06-14T09:53:08","modified_gmt":"2024-06-13T21:53:08","slug":"why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead","status":"publish","type":"post","link":"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/","title":{"rendered":"Why update_user_meta doesn’t work with display_name in WordPress and how to use pre_user_display_name instead"},"content":{"rendered":"
WordPress is powerful … but occasionally you’ll hit a problem where you hunt for hours online trying to find\u00a0a solution. You may find\u00a0plenty of other people\u00a0searching for the some thing too, but no decent\u00a0answers.<\/p>\n
For me searching for how to use\u00a0update_user_meta display_name<\/strong>\u00a0to change the default display name<\/strong> when a new user creates an account was one of those searches.<\/p>\n In case you’re struggling with this too, hopefully this quick post will help you.<\/p>\n <\/p>\n When a new user registers on the front end of your site, WordPress often decides to make the user name their display name. \u00a0Which doesn’t give the user a good experience. \u00a0Depending on the site I’m developing, I prefer to have the display name set to Firstname Lastname or just Firstname.<\/p>\n In WordPress the\u00a0update_user_meta() function\u00a0can be used to update a user’s first_name, billing_first_name, nickname and many other fields. \u00a0So it would be natural to expect to be able to update their display_name the same way, right?<\/p>\n Well unfortunately display_name can’t be updated with update_user_meta.<\/p>\n The pre_user_display_name hook filters a user\u2019s display name before<\/strong> the user is created or updated. That’s what we need to use.<\/p>\n Here’s example code I used\u00a0when I had added a\u00a0“First Name” field to the registration form. \u00a0I wanted the display name to be set as their first name.<\/p>\n Note – This was on a WooCommerce site, and the name<\/strong> of the first field on the registration form was “billing_first_name”. \u00a0This can work on any WordPress site – you just need to find what the name of the field is on the form.<\/p>\n This was the html on the registration form – you can see that “billing_first_name” was the name<\/strong> of the form field on the registration form where they inserted their first name.<\/p>\n This code was added into my\u00a0functions.php<\/strong> file in my child theme, which sets the display name to be the first name.<\/p>\n <\/p>\n Or alternatively, if you want the display name to be “firstname lastname” use this code:<\/p>\n <\/p>\n Has this article been helpful? \u00a0Please leave a comment below.<\/p>\nFirstly the problem:<\/h2>\n
update_user_meta<\/strong><\/h2>\n
Use\u00a0pre_user_display_name instead<\/h2>\n
\r\n<input class=\"input-text\" name=\"billing_first_name\" type=\"text\" value=\"\" \/>\r\n<\/code>\r\n<\/pre>\n
add_filter('pre_user_display_name','default_display_name');\r\n\r\nfunction default_display_name($name) {\r\n\r\nif ( isset( $_POST['billing_first_name'] ) ) {\r\n$name = sanitize_text_field( $_POST['billing_first_name'] );\r\n}\r\nreturn $name;\r\n}\r\n<\/code><\/pre>\n
\r\nadd_filter('pre_user_display_name','default_display_name');\r\nfunction default_display_name($name) {\r\nif ( isset( $_POST['billing_first_name'] ) ) {\r\n$firstname = sanitize_text_field( $_POST['billing_first_name'] );\r\n}\r\nif ( isset( $_POST['billing_last_name'] ) ) {\r\n$lastname = sanitize_text_field( $_POST['billing_last_name'] );\r\n}\r\n$name = $firstname . ' ' . $lastname;\r\nreturn $name;\r\n}\r\n<\/code><\/pre>\n