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: http://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: http://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.

Technorati Tags: ,

This entry was posted in Programming. Bookmark the permalink.

43 Responses to Add Data to User Field in WordPress

  1. 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.

  2. Pingback: Web Glad.Com » Blog Archive » WP User Profile Functionality

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Code: To include code, bracket it with [code language="php"] and [/code].
Example: [code language="php"] echo "Hello, World!"; [/code]

Plugin Help: If your requesting help about a plugin, please provide as much information as possible. At the very least, include your WordPress Version and the Plugin Version!