Add Data to User Field in WordPress

The times they are a-changin’.

This post seems to be older than 17 years—a long time on the internet. It might be outdated.

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.

0