Skip to content

Instantly share code, notes, and snippets.

@RalfAlbert
Created December 2, 2012 14:05
Show Gist options
  • Save RalfAlbert/4188847 to your computer and use it in GitHub Desktop.
Save RalfAlbert/4188847 to your computer and use it in GitHub Desktop.
Insert custom fields at user registration v2
<?php
/**
* Plugin Name: Custom user registration fields
* Plugin URI: http://yoda.neun12.de
* Description: Add custom fields to the user registration
* Version: 0.1
* Author: Ralf Albert
* Author URI: http://yoda.neun12.de
* Text Domain:
* Domain Path:
* Network:
* License: GPLv3
*/
add_action(
'plugins_loaded',
array( 'WP_Custom_User_Registration_Fields', 'init' ),
10,
0
);
class WP_Custom_User_Registration_Fields
{
/**
* Textdomain
* @var string
*/
const WP_CURF_L10N = 'wp_curf_l10n';
/**
* Key for metadata
* @var string
*/
const META_KEY = 'department';
/**
* Key for post array
* @var unknown
*/
const POST_KEY = 'department';
/**
* Legend for field
* @var array
*/
protected static $legend = array();
/**
* Table markup
* @var string
*/
protected static $table =
'<h3>%1$s</h3>
<table class="form-table">
<tr>
<th>
<label for="%2$s">%1$s</label>
</th>
<td>
<input type="text" name="%2$s" id="%2$s" value="%3$s" class="regular-text" />
<br />
<span class="description">%4$s</span>
</td>
</tr>
</table>';
/**
* Initialisation
* Defines label and description, adding hooks
*/
public static function init(){
self::$legend = array(
'label' => __( 'Department', self::WP_CURF_L10N ),
'description' => __( 'Please specify the department to which the user belongs.', self::WP_CURF_L10N ),
'postkey' => self::POST_KEY
);
self::add_hooks();
}
/**
* Adding needed action hooks, enqueueing javascript
*/
protected static function add_hooks(){
add_action(
'admin_print_scripts-user-new.php',
function (){
wp_enqueue_script(
'add_custom_user_registration_field',
plugins_url( 'wp_curf.js', __FILE__ ),
array( 'jquery' ),
false,
true
);
wp_localize_script(
'add_custom_user_registration_field',
'wp_curf_l10n',
self::$legend
);
}
);
foreach( array( 'show_user_profile', 'edit_user_profile' ) as $hook )
add_action( $hook, array( __CLASS__, 'add_custom_user_profile_fields' ), 10, 1 );
foreach( array( 'personal_options_update', 'edit_user_profile_update', 'user_register' ) as $hook )
add_action( $hook, array( __CLASS__, 'save_custom_user_profile_fields' ), 10, 1 );
}
/**
* Output table
* @param object $user User object
*/
public static function add_custom_user_profile_fields( $user ){
printf(
self::$table,
self::$legend['label'],
self::POST_KEY,
esc_attr( get_the_author_meta( self::META_KEY, $user->ID ) ),
self::$legend['description']
);
}
/**
* Saving field data
* @param integer $user_id User ID
* @return boolean bool True on success, else false
*/
public static function save_custom_user_profile_fields( $user_id ){
if( !current_user_can( 'edit_user', $user_id ) )
return false;
update_user_meta( $user_id, self::META_KEY, $_POST[self::POST_KEY] );
return true;
}
}
/**
* jQuery part of Costum User Registration Field
*
* @author Ralf Albert
* @version 0.1
*/
jQuery( document ).ready(
function($){
var insertElements =
'<tr class="form-field">' +
' <th scope="row"><label for="' + wp_curf_l10n.postkey + '">' + wp_curf_l10n.label + '</label></th>' +
' <td><input type="text" name="' + wp_curf_l10n.postkey + '" id="' + wp_curf_l10n.postkey + '" size="30"><br /><span class="description">' + wp_curf_l10n.description + '</span></td>' +
'</tr>';
$( '#createuser .form-table tbody' ).append( insertElements );
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment