{"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

Firstly the problem:<\/h2>\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

update_user_meta<\/strong><\/h2>\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

Use\u00a0pre_user_display_name instead<\/h2>\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

\r\n<input class=\"input-text\" name=\"billing_first_name\" type=\"text\" value=\"\" \/>\r\n<\/code>\r\n<\/pre>\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

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

 <\/p>\n

Or alternatively, if you want the display name to be “firstname lastname” use this code:<\/p>\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

 <\/p>\n

Has this article been helpful? \u00a0Please leave a comment below.<\/p>\n

If you would like to know how to add extra fields to your WooCommerce registration page here is a great article ><\/a><\/p>\n","protected":false},"excerpt":{"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. For me searching for how to use\u00a0update_user_meta display_name\u00a0to change the default display name when a new user creates an account was […]<\/p>\n","protected":false},"author":5,"featured_media":535,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-533","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress"],"yoast_head":"\nHow to set the display name in Wordpress pre_user_display_name<\/title>\n<meta name=\"description\" content=\"In Wordpress you can't use update_user_meta function to set the display name. You need to use pre_user_display_name instead. Use this code to set first name as the display name.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to set the display name in Wordpress pre_user_display_name\" \/>\n<meta property=\"og:description\" content=\"In Wordpress you can't use update_user_meta function to set the display name. You need to use pre_user_display_name instead. Use this code to set first name as the display name.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/\" \/>\n<meta property=\"og:site_name\" content=\"Geektactics\" \/>\n<meta property=\"article:published_time\" content=\"2015-04-16T03:18:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-06-13T21:53:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2015\/04\/user-meta.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"388\" \/>\n\t<meta property=\"og:image:height\" content=\"262\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Josh Moore\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Josh Moore\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/\"},\"author\":{\"name\":\"Josh Moore\",\"@id\":\"https:\/\/geektactics.co.nz\/#\/schema\/person\/6bb496f93975e50731c7b169c39bc4ab\"},\"headline\":\"Why update_user_meta doesn’t work with display_name in WordPress and how to use pre_user_display_name instead\",\"datePublished\":\"2015-04-16T03:18:24+00:00\",\"dateModified\":\"2024-06-13T21:53:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/\"},\"wordCount\":433,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\/\/geektactics.co.nz\/#organization\"},\"image\":{\"@id\":\"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2015\/04\/user-meta.jpg\",\"articleSection\":[\"Wordpress\"],\"inLanguage\":\"en-NZ\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/\",\"url\":\"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/\",\"name\":\"How to set the display name in Wordpress pre_user_display_name\",\"isPartOf\":{\"@id\":\"https:\/\/geektactics.co.nz\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2015\/04\/user-meta.jpg\",\"datePublished\":\"2015-04-16T03:18:24+00:00\",\"dateModified\":\"2024-06-13T21:53:08+00:00\",\"description\":\"In Wordpress you can't use update_user_meta function to set the display name. You need to use pre_user_display_name instead. Use this code to set first name as the display name.\",\"breadcrumb\":{\"@id\":\"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/#breadcrumb\"},\"inLanguage\":\"en-NZ\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-NZ\",\"@id\":\"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/#primaryimage\",\"url\":\"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2015\/04\/user-meta.jpg\",\"contentUrl\":\"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2015\/04\/user-meta.jpg\",\"width\":388,\"height\":262},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/geektactics.co.nz\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Why update_user_meta doesn’t work with display_name in WordPress and how to use pre_user_display_name instead\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/geektactics.co.nz\/#website\",\"url\":\"https:\/\/geektactics.co.nz\/\",\"name\":\"Geektactics\",\"description\":\"Interesting Stuff for Geeks\",\"publisher\":{\"@id\":\"https:\/\/geektactics.co.nz\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/geektactics.co.nz\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-NZ\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/geektactics.co.nz\/#organization\",\"name\":\"Geektactics\",\"url\":\"https:\/\/geektactics.co.nz\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-NZ\",\"@id\":\"https:\/\/geektactics.co.nz\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2024\/06\/geektactics-logo.png\",\"contentUrl\":\"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2024\/06\/geektactics-logo.png\",\"width\":2001,\"height\":831,\"caption\":\"Geektactics\"},\"image\":{\"@id\":\"https:\/\/geektactics.co.nz\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/geektactics.co.nz\/#\/schema\/person\/6bb496f93975e50731c7b169c39bc4ab\",\"name\":\"Josh Moore\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-NZ\",\"@id\":\"https:\/\/geektactics.co.nz\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2bea43488633fc79ace340300f8ab6d6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2bea43488633fc79ace340300f8ab6d6?s=96&d=mm&r=g\",\"caption\":\"Josh Moore\"},\"description\":\"I love marketing and technology. I run Duoplus Online Marketing and enjoy helping companies grow their income especially through using online marketing effectively.\",\"sameAs\":[\"https:\/\/www.duoplus.nz\"],\"url\":\"https:\/\/geektactics.co.nz\/blog\/author\/josh\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to set the display name in Wordpress pre_user_display_name","description":"In Wordpress you can't use update_user_meta function to set the display name. You need to use pre_user_display_name instead. Use this code to set first name as the display name.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/","og_locale":"en_US","og_type":"article","og_title":"How to set the display name in Wordpress pre_user_display_name","og_description":"In Wordpress you can't use update_user_meta function to set the display name. You need to use pre_user_display_name instead. Use this code to set first name as the display name.","og_url":"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/","og_site_name":"Geektactics","article_published_time":"2015-04-16T03:18:24+00:00","article_modified_time":"2024-06-13T21:53:08+00:00","og_image":[{"width":388,"height":262,"url":"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2015\/04\/user-meta.jpg","type":"image\/jpeg"}],"author":"Josh Moore","twitter_misc":{"Written by":"Josh Moore","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/#article","isPartOf":{"@id":"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/"},"author":{"name":"Josh Moore","@id":"https:\/\/geektactics.co.nz\/#\/schema\/person\/6bb496f93975e50731c7b169c39bc4ab"},"headline":"Why update_user_meta doesn’t work with display_name in WordPress and how to use pre_user_display_name instead","datePublished":"2015-04-16T03:18:24+00:00","dateModified":"2024-06-13T21:53:08+00:00","mainEntityOfPage":{"@id":"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/"},"wordCount":433,"commentCount":4,"publisher":{"@id":"https:\/\/geektactics.co.nz\/#organization"},"image":{"@id":"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/#primaryimage"},"thumbnailUrl":"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2015\/04\/user-meta.jpg","articleSection":["Wordpress"],"inLanguage":"en-NZ","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/","url":"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/","name":"How to set the display name in Wordpress pre_user_display_name","isPartOf":{"@id":"https:\/\/geektactics.co.nz\/#website"},"primaryImageOfPage":{"@id":"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/#primaryimage"},"image":{"@id":"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/#primaryimage"},"thumbnailUrl":"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2015\/04\/user-meta.jpg","datePublished":"2015-04-16T03:18:24+00:00","dateModified":"2024-06-13T21:53:08+00:00","description":"In Wordpress you can't use update_user_meta function to set the display name. You need to use pre_user_display_name instead. Use this code to set first name as the display name.","breadcrumb":{"@id":"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/#breadcrumb"},"inLanguage":"en-NZ","potentialAction":[{"@type":"ReadAction","target":["https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/"]}]},{"@type":"ImageObject","inLanguage":"en-NZ","@id":"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/#primaryimage","url":"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2015\/04\/user-meta.jpg","contentUrl":"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2015\/04\/user-meta.jpg","width":388,"height":262},{"@type":"BreadcrumbList","@id":"https:\/\/geektactics.co.nz\/blog\/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/geektactics.co.nz\/"},{"@type":"ListItem","position":2,"name":"Why update_user_meta doesn’t work with display_name in WordPress and how to use pre_user_display_name instead"}]},{"@type":"WebSite","@id":"https:\/\/geektactics.co.nz\/#website","url":"https:\/\/geektactics.co.nz\/","name":"Geektactics","description":"Interesting Stuff for Geeks","publisher":{"@id":"https:\/\/geektactics.co.nz\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/geektactics.co.nz\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-NZ"},{"@type":"Organization","@id":"https:\/\/geektactics.co.nz\/#organization","name":"Geektactics","url":"https:\/\/geektactics.co.nz\/","logo":{"@type":"ImageObject","inLanguage":"en-NZ","@id":"https:\/\/geektactics.co.nz\/#\/schema\/logo\/image\/","url":"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2024\/06\/geektactics-logo.png","contentUrl":"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2024\/06\/geektactics-logo.png","width":2001,"height":831,"caption":"Geektactics"},"image":{"@id":"https:\/\/geektactics.co.nz\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/geektactics.co.nz\/#\/schema\/person\/6bb496f93975e50731c7b169c39bc4ab","name":"Josh Moore","image":{"@type":"ImageObject","inLanguage":"en-NZ","@id":"https:\/\/geektactics.co.nz\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2bea43488633fc79ace340300f8ab6d6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2bea43488633fc79ace340300f8ab6d6?s=96&d=mm&r=g","caption":"Josh Moore"},"description":"I love marketing and technology. I run Duoplus Online Marketing and enjoy helping companies grow their income especially through using online marketing effectively.","sameAs":["https:\/\/www.duoplus.nz"],"url":"https:\/\/geektactics.co.nz\/blog\/author\/josh\/"}]}},"_links":{"self":[{"href":"https:\/\/geektactics.co.nz\/wp-json\/wp\/v2\/posts\/533"}],"collection":[{"href":"https:\/\/geektactics.co.nz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geektactics.co.nz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geektactics.co.nz\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/geektactics.co.nz\/wp-json\/wp\/v2\/comments?post=533"}],"version-history":[{"count":6,"href":"https:\/\/geektactics.co.nz\/wp-json\/wp\/v2\/posts\/533\/revisions"}],"predecessor-version":[{"id":20282,"href":"https:\/\/geektactics.co.nz\/wp-json\/wp\/v2\/posts\/533\/revisions\/20282"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geektactics.co.nz\/wp-json\/wp\/v2\/media\/535"}],"wp:attachment":[{"href":"https:\/\/geektactics.co.nz\/wp-json\/wp\/v2\/media?parent=533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geektactics.co.nz\/wp-json\/wp\/v2\/categories?post=533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geektactics.co.nz\/wp-json\/wp\/v2\/tags?post=533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}