Skip to content

Instantly share code, notes, and snippets.

@thefuxia
Created January 2, 2011 03:28
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thefuxia/762247 to your computer and use it in GitHub Desktop.
Save thefuxia/762247 to your computer and use it in GitHub Desktop.
Class to add custom fields to the user profile in WordPress.
<?php
/**
* Adds a form field to the edit profile page.
*
* Usage:
add_action( 'init', 'init_profile_addons' );
function init_profile_addons()
{
$GLOBALS['extended_author_text'] = new TTT_User_Profile_Addon(
array (
'name' => 'extended_author_text'
, 'label' => 'Second text field'
, 'description' => '<p>This text will be shown at the top of your author archive. you may use HTML.</p>'
, 'markup' => '<table class="form-table"><tr><th>%label%<br /><br />%description%</th>
<td><textarea name="%name%" id="%id%" rows="10" cols="30">%content%</textarea></td></tr></table>'
)
);
}
*
* @author Thomas Scholz http://toscho.de
* @version 1.2
*
*/
class TTT_User_Profile_Addon
{
public $settings = array (
/* The name attribute. */
'name'
, 'label' => ''
, 'description' => ''
/* You may use the following placeholder:
* %name% - name attribute
* %label% - label text
* %description% - additional text
* To use more placeholders, extend markup_filter().
*/
, 'markup' => ''
/* If both are not FALSE, they will replace the 'markup', and a
* table will be created. Uses the same placeholders as 'markup'.
*/
, 'th' => FALSE
, 'td' => FALSE
/* Capabilities to show and edit the field.
* Useful, if want to add a field that only administrators or editors
* may edit or view.
*/
, 'cap_show' => 'read'
, 'cap_save' => 'edit_user'
);
/**
* Constructor
* @param array $args See settings. 'name' and 'markup' required.
*/
public function __construct( $args )
{
$this->settings = array_merge( $this->settings, $args );
// The id attribute should be different to name, otherwise it doesn’t
// work in Opera.
empty ( $this->settings['id'] )
and $this->settings['id'] = $this->settings['name'] . '_id';
FALSE !== $this->settings['th'] and FALSE !== $this->settings['td']
and $this->settings['markup'] = '<table class="form-table"><tr><th>'
. $this->settings['th'] . '</th><td>' . $this->settings['td']
. '</td></tr></table>';
add_action( 'show_user_profile', array ( $this, 'show' ) );
add_action( 'edit_user_profile', array ( $this, 'show' ) );
add_action( 'personal_options_update', array ( $this, 'save' ) );
add_action( 'edit_user_profile_update', array ( $this, 'save' ) );
}
/**
* Prints the form.
* @param object $user
* @return void
*/
public function show( $user )
{
if ( ! current_user_can( $this->settings['cap_show'], $user->ID ) )
{
return;
}
$label = "<label for='{$this->settings['id']}'>{$this->settings['label']}</label>";
$markup = strtr( $this->settings['markup'],
array (
'%name%' => $this->settings['name']
, '%id%' => $this->settings['id']
, '%label%' => $label
, '%description%' => $this->settings['description']
)
);
$old_val = trim( get_the_author_meta( $this->settings['name'], $user->ID ) );
$markup = $this->markup_filter( $markup, $old_val );
print $markup;
}
/**
* Saves the data.
* @param int $user_id
* @return void
*/
public function save( $user_id )
{
if ( ! current_user_can( $this->settings['cap_save'], $user_id ) )
{
return;
}
$input = empty ( $_POST[ $this->settings['name'] ] ) ? '' : $_POST[ $this->settings['name'] ];
$input = $this->prepare_input( $input );
update_user_meta( $user_id, $this->settings['name'], $input );
}
/**
* Prepares the user input. For extensions.
* @param string $input
* @return string
*/
public function prepare_input( $input )
{
return $input;
}
/**
* Prepares the form markup
* @param string $markup
* @param string $old_val
* @return string
*/
public function markup_filter( $markup, $old_val )
{
$old_val = htmlspecialchars( $old_val, ENT_QUOTES, 'utf-8', FALSE );
return str_replace( '%content%', $old_val, $markup );
}
}
<?php
/**
* Template for a simple checkbox.
*/
class TTT_User_Profile_Checkbox extends TTT_User_Profile_Addon
{
public function prepare_input( $input )
{ // Convert the checkbox value to integer
return '' == trim( $input ) ? 0 : 1;
}
public function markup_filter( $markup, $old_value )
{ // Preselect the checkbox if neccessary.
$checked = 1 == $old_value ? ' checked="checked"' : '';
return str_replace( '%checked%', $checked, $markup );
}
}
<?php
// Usage example for the functions.php
add_action( 'init', 'ttt_init_profile_addons' );
/**
* Registers the extra fields for the user profile editor.
*/
function ttt_init_profile_addons()
{
$GLOBALS['ttt_show_profile'] = new TTT_User_Profile_Checkbox(
array (
'name' => 'ttt_show_profile'
, 'label' => 'Show a short profile box on my posts.'
, 'th' => ''
, 'td' => '<input type="checkbox" name="%name%" id="%id%" %checked% /> %label%'
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment