{"id":241,"date":"2011-10-17T20:22:08","date_gmt":"2011-10-18T01:22:08","guid":{"rendered":"http:\/\/www.compdigitec.com\/labs\/?p=241"},"modified":"2011-10-17T20:22:30","modified_gmt":"2011-10-18T01:22:30","slug":"adding-custom-profile-fields-in-wordpress-fully-automatic","status":"publish","type":"post","link":"http:\/\/www.compdigitec.com\/labs\/2011\/10\/17\/adding-custom-profile-fields-in-wordpress-fully-automatic\/","title":{"rendered":"Adding custom profile fields in WordPress (fully automatic)"},"content":{"rendered":"<p>Custom profile fields are additional custom-defined fields in the WordPress usermeta database that permit us to store additional information about a user. To add custom profile fields, add the following line to your functions.php in your theme folder:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">include('customfields.php');<\/pre>\n<p>Now create a new file called <strong>customfields.php<\/strong> in the theme folder (the same folder as function.php) with the following contents:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">&lt;?php\r\n\r\nfunction get_extra_profile_list() {\r\nreturn Array(\r\n\/* Add your custom fields, here like follows:\r\n'slug_of_the_field_here' =&gt; 'Field name for display',\r\n *\/\r\n'address' =&gt; 'Address',\r\n'favouritecolour' =&gt; 'Favourite Colour'\r\n);\r\n}\r\n\r\nadd_action( 'show_user_profile', 'extra_user_profile_fields' );\r\nadd_action( 'edit_user_profile', 'extra_user_profile_fields' );\r\n\r\nfunction extra_user_profile_fields( $user ) { ?&gt;\r\n&lt;h3&gt;&lt;?php _e(&quot;Extra profile information&quot;, &quot;blank&quot;); ?&gt;&lt;\/h3&gt;\r\n\r\n&lt;table class=&quot;form-table&quot;&gt;\r\n&lt;?php\r\nforeach(get_extra_profile_list() as $key =&gt; $value) {\r\n?&gt;\r\n&lt;tr&gt;\r\n&lt;th&gt;&lt;label for=&quot;&lt;?php echo $key; ?&gt;&quot;&gt;&lt;?php _e($value); ?&gt;&lt;\/label&gt;&lt;\/th&gt;\r\n&lt;td&gt;\r\n&lt;input type=&quot;text&quot; name=&quot;&lt;?php echo $key; ?&gt;&quot; id=&quot;&lt;?php echo $key; ?&gt;&quot; value=&quot;&lt;?php echo esc_attr( get_the_author_meta( $key, $user-&gt;ID ) ); ?&gt;&quot; class=&quot;regular-text&quot; \/&gt;&lt;br \/&gt;\r\n&lt;span class=&quot;description&quot;&gt;&lt;?php _e(&quot;Please enter your $value.&quot;); ?&gt;&lt;\/span&gt;\r\n&lt;\/td&gt;\r\n&lt;\/tr&gt;\r\n&lt;?php\r\n}\r\n?&gt;\r\n&lt;\/table&gt;\r\n&lt;?php }\r\n\r\nadd_action( 'personal_options_update', 'save_extra_user_profile_fields' );\r\nadd_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );\r\n\r\nfunction save_extra_user_profile_fields( $user_id ) {\r\n\r\nif ( !current_user_can( 'edit_user', $user_id ) ) { return false; }\r\n\r\nforeach(get_extra_profile_list() as $key =&gt; $value) {\r\nupdate_usermeta( $user_id, $key, $_POST[$key] );\r\n}\r\n}\r\n?&gt;\r\n<\/pre>\n<p>Now if you go to &#8220;Users&#8221; under your administration and edit a user, the additional profile fields will show up at the bottom of the edit page.<\/p>\n<p><a href=\"http:\/\/www.compdigitec.com\/labs\/wp-content\/uploads\/2011\/10\/extraprofile.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-242\" title=\"Extra Profile Fields in WordPress\" src=\"http:\/\/www.compdigitec.com\/labs\/wp-content\/uploads\/2011\/10\/extraprofile.png\" alt=\"\" width=\"619\" height=\"102\" \/><\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>Custom profile fields are additional custom-defined fields in the WordPress usermeta database that permit us to store additional information about a user. To add custom profile fields, add the following line to your functions.php in your theme folder: Now create a new file called customfields.php in the theme folder (the same folder as function.php) with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[754,763,762,764,152],"_links":{"self":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts\/241"}],"collection":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/comments?post=241"}],"version-history":[{"count":0,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts\/241\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/media?parent=241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/categories?post=241"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/tags?post=241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}