Skip to content

Instantly share code, notes, and snippets.

@attitude
Last active December 19, 2015 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save attitude/5902166 to your computer and use it in GitHub Desktop.
Save attitude/5902166 to your computer and use it in GitHub Desktop.
Add/Remove User Contact Methods WordPress Snipet/Plugin
<?php
/*
Plugin Name: Snipet: Add/Remove User Contact Methods
Plugin URI: http://www.attitude.sk
Description: Adds phone field to the user edit screen
Version: 0.1.1
Author: Martin Adamko
Author URI: http://www.attitude.sk
License: The MIT License (MIT)
Copyright (c) 2013 Mgr. art. Martin Adamko
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/**
* Micro-plugin/snippet to add/remove user contact methods
*
* @author Mgr. art. Martin Adamko <margin@attitude.sk>
* @version 0.1.1
*
*/
// Shortcut Function(s) -------------------------------------------------------
if (!function_exists('setup_user')) {
/**
* Setup user shorthand function
*
* Setup all user data on WP_User object
*
* In case function already exists use
* {@see addRemoveContactMethods::setup_user() static class method}
*
* @param object|null $obj Instance of WP_User object. If null, thand current user is used
* @param array $fields Array of fields to return
* @return void Function modifies object passed by reference
*
*/
function setup_user(& $obj=NULL,$fields=array())
{
return WP_addRemoveContactMethods::setup_user($obj,$fields);
}
}
// Class ----------------------------------------------------------------------
/**
* Add and remove User methods for WordPress
*
* Use {@link http://codex.wordpress.org/Function_Reference/apply_filters apply_filters()}
* or extend this class and replace {@link WP_addRemoveContactMethods::get_fields_to_add()},
* {@link WP_addRemoveContactMethods::get_fields_to_remove()} with your custom methods.
*
*/
class WP_addRemoveContactMethods
{
/**
* Private class constructor (class is used statically)
*
*/
private function __construct() {}
/**
* Return array of fields to add
*
* ### Example usage using class
*
* <code>
* class addMyContactMethods extends WP_addRemoveContactMethods
* {
* static function get_fields_to_add()
* {
* return array('phone'=>__('Phone'), 'skype' => 'Skype', 'linkedin' => 'LinkedIn');
* }
* }
* </code>
*
* ### Example usage using add_filters()
*
* <code>
* add_filter('WP_addRemoveContactMethods/get_fields_to_add', function() {
* return array('phone'=>__('Phone'), 'skype' => 'Skype', 'linkedin' => 'LinkedIn');
* });
* </code>
* @returns array Array of pairs of field and field label to add
*
*/
static public function get_fields_to_add()
{
return apply_filters('WP_addRemoveContactMethods/get_fields_to_add', array());
}
/**
* Return array of fields to remove
*
* ### Example usage using class
*
* <code>
* class addMyContactMethods extends WP_addRemoveContactMethods
* {
* static function get_fields_to_remove()
* {
* return array('url', 'aim', 'yim', 'jabber');
* }
* }
* </code>
*
* ### Example usage using add_filters()
*
* <code>
* add_filter('WP_addRemoveContactMethods/get_fields_to_remove', function() {
* return array('url', 'aim', 'yim', 'jabber');
* });
* </code>
*
* @returns array Array of fields to remove
*
*/
static function get_fields_to_remove()
{
return apply_filters('WP_addRemoveContactMethods/get_fields_to_remove', array());
}
/**
* Adds and removes contact methods for WP_User objects
*
* @param array $user_contactmethods Passed user contact methods by WordPress `apply_filters()`
* @return array Modified array of user contact methods
*
*/
static public function add_remove_contact_methods($user_contactmethods)
{
foreach (static::get_fields_to_add() as $field_name => $field_label) {
if (!empty($field_name) && is_string($field_name) && !empty($field_label) && is_string($field_label)) {
$user_contactmethods[$field_name] = $field_label;
}
}
foreach (static::get_fields_to_remove() as $field_name) {
if (!empty($field_name) && is_string($field_name)) {
if ($field_name == 'url') {
add_action('edit_user_profile', array(__CLASS__,'hide_url'));
} else {
unset($user_contactmethods[$field_name]);
}
}
}
return $user_contactmethods;
}
/**
* Hide URL from User admin interface
*
*/
static public function hide_url()
{
echo '<script>jQuery(document).ready(function(){jQuery("#url").closest("tr").hide();});</script>';
}
/**
* Setup all user data on WP_User object
*
* @param object|null $obj Instance of WP_User object. If null, thand current user is used
* @param array $fields Array of fields to return
* @return void Function modifies object passed by reference
*
*/
static public function setup_user(& $obj=NULL, $fields=array())
{
if ($obj===NULL || empty($obj)) {
global $current_user;
get_currentuserinfo();
$obj = &$current_user;
}
if (!is_object($obj) || !isset($obj->ID) || !($obj instanceof WP_User)) {
return $obj;
}
global $wpdb;
$fields = (!empty($fields) && is_array($fields)) ? " AND `meta_key` IN ('".implode("', '", $fields)."')" : '';
$sql = "SELECT `meta_key`, `meta_value` FROM `$wpdb->usermeta` WHERE `user_id`=$obj->ID".$fields;
$metas = $wpdb->get_results($sql);
if(!$metas) return;
foreach ($metas as $meta) {
if (!empty($meta->meta_value) && !isset($obj->data->{$meta->meta_key})) {
$obj->data->{$meta->meta_key} = $meta->meta_value;
}
}
}
}
// Register Filter ------------------------------------------------------------
add_filter('user_contactmethods', array('WP_addRemoveContactMethods', 'add_remove_contact_methods'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment