WP_User::__construct() WordPress Method

When a new WP_User object is created, the __construct() method is called. This method sets up the new user object with the correct user data.

WP_User::__construct( int|string|stdClass|WP_User $id, string $name = '', int $site_id = '' ) #

Constructor.


Description

Retrieves the userdata and passes it to WP_User::init().


Top ↑

Parameters

$id

(int|string|stdClass|WP_User)(Required)User's ID, a WP_User object, or a user object from the DB.

$name

(string)(Optional) User's username

Default value: ''

$site_id

(int)(Optional)Site ID, defaults to current site.

Default value: ''


Top ↑

Source

File: wp-includes/class-wp-user.php

	public function __construct( $id = 0, $name = '', $site_id = '' ) {
		if ( ! isset( self::$back_compat_keys ) ) {
			$prefix                 = $GLOBALS['wpdb']->prefix;
			self::$back_compat_keys = array(
				'user_firstname'             => 'first_name',
				'user_lastname'              => 'last_name',
				'user_description'           => 'description',
				'user_level'                 => $prefix . 'user_level',
				$prefix . 'usersettings'     => $prefix . 'user-settings',
				$prefix . 'usersettingstime' => $prefix . 'user-settings-time',
			);
		}

		if ( $id instanceof WP_User ) {
			$this->init( $id->data, $site_id );
			return;
		} elseif ( is_object( $id ) ) {
			$this->init( $id, $site_id );
			return;
		}

		if ( ! empty( $id ) && ! is_numeric( $id ) ) {
			$name = $id;
			$id   = 0;
		}

		if ( $id ) {
			$data = self::get_data_by( 'id', $id );
		} else {
			$data = self::get_data_by( 'login', $name );
		}

		if ( $data ) {
			$this->init( $data, $site_id );
		} else {
			$this->data = new stdClass;
		}
	}


Top ↑

Changelog

Changelog
VersionDescription
2.0.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.