Add Data to User Field in WordPress

My solution to:
Add Custom Fields to User Profile & Registration Pages
http://wordpress.org/support/topic/55434

Copy and paste this into a php file in your plugins directory. I’ll get a file you can download setup shortly.

Update: Here download it => here.

<?php
/*
Plugin Name: Add To User
Plugin URI: https://www.andrewferguson.net/wordpress-plugins/
Plugin Description: Allows adding mySQL Data fields to store/add more user info
Version: 0.1
Author: Andrew Ferguson
Author URI: https://www.andrewferguson.net/
*/

/*Use: Allows adding mySQL Data fields to store/add more user info
/*


Add To User - Allows adding mySQL Data fields to store/add more user info
Copyright (c) 2006 Andrew Ferguson

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

*/ 
$fergcorp_addField = array(
				array(	"legend"=>"Favorite ColorLegend",
						"description"=>"What is your favorite color:",
						"label"=>"Color",
						"inputName"=>"wp_favColor",
						"inputType"=>"text",
						)
			);

add_action('show_user_profile', 'fergcorp_addToUser_addFields');
add_action('personal_options_update', 'fergcorp_addToUser_updateFields');

function fergcorp_addToUser_addFields(){ 
	global $wpdb, $user_ID, $fergcorp_addField;
	
	foreach($fergcorp_addField as $thisField){
		?>
		
		<fieldset>
		<legend><?php _e($thisField['legend']); ?></legend>
		<p class="desc"><?php _e($thisField['description']); ?></em></p>
		<p><label><?php _e($thisField['label']); ?><br />
		<input name="<?php echo $thisField['inputName']; ?>" type="<?php echo $thisField['inputType']; ?>" value="<?php echo $wpdb->get_var("SELECT ".$thisField["inputName"]." FROM $wpdb->users WHERE ID = ".$user_ID." LIMIT 1"); ?>" />
		</label></p>
		</fieldset>
		<? 
	}
		
		}

function fergcorp_addToUser_updateFields(){
	global $wpdb, $current_user, $fergcorp_addField;

	foreach($fergcorp_addField as $thisField){
		$wpdb->query("UPDATE $wpdb->users SET ".$thisField["inputName"]." = '".$_POST[$thisField["inputName"]]."' WHERE ID = ".	$current_user->id." LIMIT 1");
	}
}	


?>

Notes:
This is a very alpha version. It works and you shouldn’t have any issues with it. You’ll need to manually add the field to your wp_users table. I will fix that in a future release. The data is set via an array within an array. To add another data field, just add a second array within the first. Again, I’ll make that more user friendly in a future release.

46 Replies to “Add Data to User Field in WordPress”

  1. Pingback: MEDIATRICKS
  2. Well well, it seems to be working very fine !
    (and without any problems with displaying Japanese)

    In a future release, I hope that adding “required” fields will be possible easily.

    Keep good working;-) !

  3. OK, I am going to give this a try… you say “You’ll need to manually add the field to your wp_users table.” what field?

    And, can you provide an example that shows how to extract and display the user’s data from these fields?

  4. The field that you’re wanting to add to user management page.

    To get data from a single field for a single user, use this:

    $wpdb->get_var("SELECT *FIELD_NAME* FROM $wpdb->users WHERE ID = ".$user_ID." LIMIT 1");
    

    Replace *FIELD_NAME* with the name of the field. You’ll also have to set:

    global $wpdb, $user_ID;
    

    to get access to the global versions of the variables.

  5. Yes, albeit indirectly. I’m using it on a site that has the potential to store hundreds of user photos on it, so I didn’t really want to store everything as a BLOB in mySQL. So I ended up using a modified version of the code above (actually, the code above is a modified version of the code I’m taking about) to upload a file to a directory and rename it to the username of the person logged in. If you’re interested in that code, I can extract it and post it for you.

  6. Hello,

    I’m very interested in the code to let users upload a photo, and display it in a trombinoscope.

    I’ve been looking for this feature for a long time and haven’t found an easy way to set it up in wordpress. Been trying with usermeta / userextra which work fine except for a picture field.

    Thanks in advance.

  7. Seb, I wrote a plugin quite some time ago that I called headshot. It lets a person upload their own profile photo, and it will autocreate the thumbnail. And if you change the thumbnail size in the admin settings, it will automatically resize them as they are displayed. It works good, but the code is still kind of messy. It doesn’t use the database at all, instead relying on a folder that contains photo corresponding to the user name with a .jpg extension. I think it currently only supports .jpg, which it checks for (I think). Anyway, if you’re interested, I might clean it up and publish it. But I have no idea what a trombinoscope is? πŸ™‚

    The reason I’m here though is for adding extra data fields to a user profile. I had written one a long time ago, but I had some quirks I hadn’t worked out. It might have worked with an earlier version, and not with 2.x. Anyway, I see you require us to create the database entry, which isn’t how I would prefer to do this. If I get mine working, I can publish it too, and we can compare notes if you like.

  8. Hi manstraw, “trombinoscope” is a french word, though I thought you could use it in english… My best guess in english would be “user gallery”, a page showing user profiles with pictures.

    If you have such a plugin avalaible, I’d be glad you shared it. πŸ™‚

  9. I’d love to get this to work on our site… but not having done any WP development I’m a little confused about where to start implementing the plugin. Could you post a few pointers for somebody who might need to know where to start getting this to work?

    I’m so glad to see this project started. We needed it! Thank you!

  10. I’m on hiatus from PHP development right now while I’m working at Boeing. Programing all day long doesn’t bode well for my brain πŸ˜‰

    If you still need help, contact me at the begining of September and I’ll be in a much better spot to help.

  11. I too want to be able to give each WordPress registered user the ability to upload a photo of themselves – so if you do have suitable code I’d be very interested….

    P.S. A quick Google translate of sites featuring “trombinoscopes” appears to reveal them as a Rogue’s Gallery!

  12. It would have been a very nice plugin addition to my site. But it is beyond me to do the manual addition stuff.
    I require this plugin badly. I hope you complete this project soon.

  13. Great idea – I have added four fields with this and updated the code to auto add columns in the database.

    The plugin actually combines this with a map plugin to create a member map but if you download my new code you will quickly see how easy it would be to edit the required files to include your own fields.

    GREAT WORK – Thanks for the inspiration!

  14. Hi Andrew,

    if you manage to put an eye on it, I’m still interested by your piece of code that lets registered WP users upload a file to a directory and automatically renames it to the username of the person logged in.

    Could you find the time to publish it, I’m sure it’ll help a lot of people looking for this feature.

    Thanx!

  15. Seb, I have code that does that, but am hella busy right now. It’s needs to be cleaned up a little and tested with the latest wordpress. I can be convinced to do that in a few days or a week, once my current panic is overwith. Comment this post again to remind me!

  16. hi folks
    i am going into user page in my wordpress admin.

    i cannot see where to add

    i cannot see where to do anything … at all.

    i am flabbergasted, i usually know how to at least make a start.

    i wish to add some more fields to the register fields.

    ie

    first name
    postcode
    country

    does anyone have any clues for how i should get started … ah um

    in newbie language???

    alto

  17. hi again folks: coulld you tell me in newbie language …
    i have looked in my wp admin in the user section …
    cant see what to do …. ?

    where do i put this …

    $wpdb->get_var(“SELECT *FIELD_NAME* FROM $wpdb->users WHERE ID = “.$user_ID.” LIMIT 1″);

    and this ….

    global $wpdb, $user_ID;

    alto

  18. Hey Andrew. I’ve been looking for a plugin like this for some time so thank you so much. But I’m having an issue as I think some people above are figuring out how once we get it installed and activated…where to go from there. Can you give me a short explanation please. Thanks in advance.

  19. It works fine as it displays thew new field on the user profile.

    But it doesn’t show up in user registration form.

    also, i just add new fields as :

      array(  "legend"=>"Occupation",
                 "description"=>"What is your occupation:",
                 "label"=>"occupation",
                 "inputName"=>"wp_occupation",
                 "inputType"=>"text",
                )
    
    

    ?
    any way to add drop downs or radio checks with limited options?

  20. Andrew, I’ve downloaded and installed your add-data-to-user plugin, and activated it. But what do I do next? What do I run to add firstname and lastname fields, and then how do I get them to show up on wp-register.php? I’ve currently got commenting turned off until I get this worked out, as I don’t want anyone registering until I can make then put in their first and last names. I’ve very much of a novice at this. I’ve got the actual blogs working on some sites but have comments turned off on all of them until I get this plugin working (this first try is http://altadenabaptist.org/wordpress)
    Thanks for building the plug in, and thanks in advance for some babystep instructions on what to do next.

  21. Hi Kathryn,

    As I stated, the code I released above is “very alpha”. If you don’t have a decent grasp of PHP and mySQL, this is not the plugin for you (yet). When time permits, I really am planning on releasing a muuuch more friendlier version of this code.

    First off, this is NOT the right program to force a user to add their first and last name. There are already spots designated my WordPress. This plugin is designed to add fields for data that does not inherently exist in WordPress.

    There is a halfway hack. The field that controls what the username is displayed (in the comments section, for example) is called “display_name”. In the array (see below), if you set the “inputName” to “display_name”, that will over ride the setting (in theory).

    Back to your original question and assuming you wanted to add a field(s) that didn’t already exists:

    In order to have more than one meta value, take a look at the $fergcorp_addField array. If you note, it’s actually an array within an array. To create additional meta fields, add a second array within the first array (i.e. copy lines 34-39, add a comma after the parenthesis on line 39, and then paste the copied lines. Change values as needed).

    There is already a hook in place for data to show up in user profile page. It shouldn’t be too hard to modify that for registration. First off, wp-register.php is actually depreciated. You’ll need to replace two hooks:

    1) On line 42, replace “show_user_profile” with “register_form”
    2) On line 43, replace “personal_options_update” with “register_post”

    Note: all I did to find these “magic phrases” was to look at wp-login.php for the hook, denoted by do_action(“hook_name_here”).

    As a word of caution, I actually haven’t tried doing any of these things. So they make not work and, in a very unlikely scenario, screw something up.

  22. I did a bit more reading. The two hooks I mentioned above, “register_form” and “register_post”, aren’t available until WP 2.1. I don’t think it would work anyway because “register_post” is triggered before the initial user creation.

  23. Andrew, I appreciate the time you took to respond. I think you were right when you said “this is not the plugin for you (yet)” as you lost me right away and you can probably take away the (yet) and say it’s not the plugin for me. I’d appreciate if you can take a little more time to answer this next paragraph.

    You said that there is already a place for the first and last names? The default registration doesn’t have that, so where is it found? Here’s the url for another site, where the registration IS turned on, and this is the same registration page that is available on the other site once I turn on allowing comments.
    http://www.arleneeakle.com/wordpress/wp-register.php
    As you see, it’s only Username and Email address. What am I missing?

  24. The fields for the first and last name I mentioned earlier are only available after the user is registered. To change them, the user logs in and edits their profile.

    There is some good news, I’m currently toying around with some code that would do what you need. The bad news is that it will only be available if you’re running WP 2.1 or higher.

  25. WordPress 2.0.6 is the latest stable version. WordPress 2.1 is the next version, which is supposed to be released around the 22nd of January. But don’t upgrade just yet, wait for the stable version of WP 2.1 to come out.

  26. A while back, Seb said:
    “I’m still interested by your piece of code that lets registered WP users upload a file to a directory and automatically renames it to the username of the person logged in.

    Could you find the time to publish it, I’m sure it’ll help a lot of people looking for this feature.”

    Yes, I’ll try to release that soon and make another post here and on the home page announcing that.

Leave a Reply

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