StrictThemes - WordPress Themes

[WordPress] Add extra user info

<?php if ( ! defined( 'ABSPATH' ) ) { return; } if ( ! class_exists( 'XX_User' ) ) { /** * Extra User Fields * * @version 1.0 * * @license GPL 2 * * 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. */ final class XX_User { /** * Instance * * @since 1.0 * * @var object The instance of the class */ private static $_instance; /** * User Profile Fields * * @since 1.0 * * @var array The list of the additional profile fields */ private $_fields; /** * Setup User Fields * * Create the default user fields, them can be filtered by the theme. * * @since 1.0 * * @param null * @return void */ private function _setup_user_fields() { $this->_fields = array( 'socials' => array( '_table_info' => array( 'title' => __( 'User Social Links', 'xx' ), 'description' => '', ), 'facebook' => array( 'label' => __( 'Facebook Link', 'xx' ), 'description' => __( 'Enter your facebook user name', 'xx' ), ), 'twitter' => array( 'label' => __( 'Twitter Link', 'xx' ), 'description' => __( 'Enter your twitter user name without the @', 'xx' ), ), ), ); /** * User Fields * * @since 1.0 * * @param array $this->_fields The user fields to filter */ $this->_fields = apply_filters( 'at_user_fields', $this->_fields ); } /** * Update User Options * * @since 1.0 * * @param int $id The user id to update * @return void */ public function save_user_options( $id ) { if ( ! current_user_can( 'edit_user', $id ) ) { return; } if ( sizeof( $this->_fields ) > 0 ) { foreach( $this->_fields as $group => $field ) { foreach ( $field as $meta => $value ) { if ( '_table_info' !== $meta ) { update_user_meta( $id, $meta, $_POST[ $meta ] ); } } } } } /** * Show User Options * * @since 1.0 * * @param object $user The WP_User object * @return void */ public function show_user_options( $user ) { $output = ''; // The html output if ( $user ) { foreach( $this->_fields as $group => $field ) { $output .= '<h3>' . $field['_table_info']['title'] . '</h3>'; if ( array_key_exists( 'description', $field['_table_info'] ) ) { $output .= '<p class="description">' . $field['_table_info']['description'] . '</p>'; } $output .= '<table class="form-table">'; $output .= '<tbody>'; foreach ( $field as $meta => $data ) { if ( '_table_info' !== $meta ) { $label = '<label for="' . $meta . '">' . ucwords( $meta ) . '</label>'; $description = '<p class="description">' . ucfirst( $data['description'] ) . '</p>'; $input_value = esc_attr( get_user_meta( $user->ID, $meta, true ) ); $input = '<input type="text" class="regular-text" name="' . $meta . '" id="' . $meta . '" value="' . $input_value . '">'; $output .= "<tr><th>$label</th><td>$input $description</td></tr>"; } } $output .= '</tbody>'; $output .= '</table>'; } echo $output; } else { // Add WP Error? } } /** * Get User Card * * Create an user card with the meta of the user like: avatar, bio and social links * * @since 1.0 * * @param int $id The user ID * @return array|bool $user_card The array that contain the user info for the user card * False if the user doesn't exists or the $user_card is empty */ public function get_user_card_data( $id ) { if ( ! $id ) { return false; } $user = get_user_by( 'id', $id ); if ( $user ) { $output = ''; $user_card = array(); /** * Avatar Size * * This filter is applied to the avatar size for the user card data * * @since 1.0 * * @param int int Size of Gravatar to return (max is 512) * */ $avatar_size = apply_filters( 'xx_user_card_avatar_size', 96 ); // Add default info $user_card['user'] = array( 'name' => $user->display_name, 'email' => $user->user_email, 'description' => get_user_meta( $user->ID, 'description', true ), 'avatar' => get_avatar( $user->ID, $avatar_size ), ); if ( sizeof( $this->_fields ) > 0 ) { foreach( $this->_fields as $group => $field ) { foreach( $field as $meta => $value ) { $curr = get_user_meta( $user->ID, $meta, true ); if ( $curr ) { $user_card[ $group ][ $meta ] = $curr; } } } } // Ok, now we have the user meta, let's create the card if ( sizeof( $user_card ) > 0 ) { return $user_card; } } return false; } /** * Instance * * @since 1.0 * * @param null * @return object The Class Instance */ public static function get_instance() { if ( null === self::$_instance ) { self::$_instance = new self; } return self::$_instance; } private function __construct() { $this->_setup_user_fields(); add_action( 'show_user_profile', array( $this, 'show_user_options' ) ); // In user profile page add_action( 'edit_user_profile', array( $this, 'show_user_options' ) ); // In edit page, commonly for admins add_action( 'personal_options_update', array( $this, 'save_user_options' ) ); // In user profile page add_action( 'edit_user_profile_update', array( $this, 'save_user_options' ) ); // In edit page, commonly for admins } } }
Wordpress - Add extra User Infos

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.