__( 'The provided password is an invalid application password.' )
/** This action is documented in wp-includes/user.php */
do_action( 'application_password_failed_authentication', $error );
* Validates the application password credentials passed via Basic Authentication.
* @param int|false $input_user User ID if one has been determined, false otherwise.
* @return int|false The authenticated user ID if successful, false otherwise.
function wp_validate_application_password( $input_user ) {
// Don't authenticate twice.
if ( ! empty( $input_user ) ) {
if ( ! wp_is_application_passwords_available() ) {
// Both $_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW'] must be set in order to attempt authentication.
if ( ! isset( $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'] ) ) {
$authenticated = wp_authenticate_application_password( null, $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'] );
if ( $authenticated instanceof WP_User ) {
return $authenticated->ID;
// If it wasn't a user what got returned, just pass on what we had received originally.
* For Multisite blogs, checks if the authenticated user has been marked as a
* spammer, or if the user's primary blog has been marked as spam.
* @param WP_User|WP_Error|null $user WP_User or WP_Error object from a previous callback. Default null.
* @return WP_User|WP_Error WP_User on success, WP_Error if the user is considered a spammer.
function wp_authenticate_spam_check( $user ) {
if ( $user instanceof WP_User && is_multisite() ) {
* Filters whether the user has been marked as a spammer.
* @param bool $spammed Whether the user is considered a spammer.
* @param WP_User $user User to check against.
$spammed = apply_filters( 'check_is_user_spammed', is_user_spammy( $user ), $user );
return new WP_Error( 'spammer_account', __( '<strong>Error:</strong> Your account has been marked as a spammer.' ) );
* Validates the logged-in cookie.
* Checks the logged-in cookie if the previous auth cookie could not be
* This is a callback for the {@see 'determine_current_user'} filter, rather than API.
* @param int|false $user_id The user ID (or false) as received from
* the `determine_current_user` filter.
* @return int|false User ID if validated, false otherwise. If a user ID from
* an earlier filter callback is received, that value is returned.
function wp_validate_logged_in_cookie( $user_id ) {
if ( is_blog_admin() || is_network_admin() || empty( $_COOKIE[ LOGGED_IN_COOKIE ] ) ) {
return wp_validate_auth_cookie( $_COOKIE[ LOGGED_IN_COOKIE ], 'logged_in' );
* Gets the number of posts a user has written.
* @since 4.1.0 Added `$post_type` argument.
* @since 4.3.0 Added `$public_only` argument. Added the ability to pass an array
* of post types to `$post_type`.
* @global wpdb $wpdb WordPress database abstraction object.
* @param int $userid User ID.
* @param array|string $post_type Optional. Single post type or array of post types to count the number of posts for. Default 'post'.
* @param bool $public_only Optional. Whether to only return counts for public posts. Default false.
* @return string Number of posts the user has written in this post type.
function count_user_posts( $userid, $post_type = 'post', $public_only = false ) {
$post_type = array_unique( (array) $post_type );
$where = get_posts_by_author_sql( $post_type, true, $userid, $public_only );
$query = "SELECT COUNT(*) FROM $wpdb->posts $where";
$last_changed = wp_cache_get_last_changed( 'posts' );
$cache_key = 'count_user_posts:' . md5( $query ) . ':' . $last_changed;
$count = wp_cache_get( $cache_key, 'post-queries' );
if ( false === $count ) {
$count = $wpdb->get_var( $query );
wp_cache_set( $cache_key, $count, 'post-queries' );
* Filters the number of posts a user has written.
* @since 4.1.0 Added `$post_type` argument.
* @since 4.3.1 Added `$public_only` argument.
* @param int $count The user's post count.
* @param int $userid User ID.
* @param string|array $post_type Single post type or array of post types to count the number of posts for.
* @param bool $public_only Whether to limit counted posts to public posts.
return apply_filters( 'get_usernumposts', $count, $userid, $post_type, $public_only );
* Gets the number of posts written by a list of users.
* @global wpdb $wpdb WordPress database abstraction object.
* @param int[] $users Array of user IDs.
* @param string|string[] $post_type Optional. Single post type or array of post types to check. Defaults to 'post'.
* @param bool $public_only Optional. Only return counts for public posts. Defaults to false.
* @return string[] Amount of posts each user has written, as strings, keyed by user ID.
function count_many_users_posts( $users, $post_type = 'post', $public_only = false ) {
if ( empty( $users ) || ! is_array( $users ) ) {
* Filters whether to short-circuit performing the post counts.
* When filtering, return an array of posts counts as strings, keyed
* @param string[]|null $count The post counts. Return a non-null value to short-circuit.
* @param int[] $users Array of user IDs.
* @param string|string[] $post_type Single post type or array of post types to check.
* @param bool $public_only Whether to only return counts for public posts.
$pre = apply_filters( 'pre_count_many_users_posts', null, $users, $post_type, $public_only );
$userlist = implode( ',', array_map( 'absint', $users ) );
$where = get_posts_by_author_sql( $post_type, true, null, $public_only );
$result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N );
$count = array_fill_keys( $users, 0 );
foreach ( $result as $row ) {
$count[ $row[0] ] = $row[1];
// User option functions.
* Gets the current user's ID.
* @return int The current user's ID, or 0 if no user is logged in.
function get_current_user_id() {
if ( ! function_exists( 'wp_get_current_user' ) ) {
$user = wp_get_current_user();
return ( isset( $user->ID ) ? (int) $user->ID : 0 );
* Retrieves user option that can be either per Site or per Network.
* If the user ID is not given, then the current user will be used instead. If
* the user ID is given, then the user data will be retrieved. The filter for
* the result, will also pass the original option name and finally the user data
* object as the third parameter.
* The option will first check for the per site name and then the per Network name.
* @global wpdb $wpdb WordPress database abstraction object.
* @param string $option User option name.
* @param int $user Optional. User ID.
* @param string $deprecated Use get_option() to check for an option in the options table.
* @return mixed User option value on success, false on failure.
function get_user_option( $option, $user = 0, $deprecated = '' ) {
if ( ! empty( $deprecated ) ) {
_deprecated_argument( __FUNCTION__, '3.0.0' );
$user = get_current_user_id();
$user = get_userdata( $user );
$prefix = $wpdb->get_blog_prefix();
if ( $user->has_prop( $prefix . $option ) ) { // Blog-specific.
$result = $user->get( $prefix . $option );
} elseif ( $user->has_prop( $option ) ) { // User-specific and cross-blog.
$result = $user->get( $option );
* Filters a specific user option value.
* The dynamic portion of the hook name, `$option`, refers to the user option name.
* @param mixed $result Value for the user's option.
* @param string $option Name of the option being retrieved.
* @param WP_User $user WP_User object of the user whose option is being retrieved.
return apply_filters( "get_user_option_{$option}", $result, $option, $user );
* Updates user option with global blog capability.
* User options are just like user metadata except that they have support for
* global blog options. If the 'is_global' parameter is false, which it is by default,
* it will prepend the WordPress table prefix to the option name.
* Deletes the user option if $newvalue is empty.
* @global wpdb $wpdb WordPress database abstraction object.
* @param int $user_id User ID.
* @param string $option_name User option name.
* @param mixed $newvalue User option value.
* @param bool $is_global Optional. Whether option name is global or blog specific.
* Default false (blog specific).
* @return int|bool User meta ID if the option didn't exist, true on successful update,
function update_user_option( $user_id, $option_name, $newvalue, $is_global = false ) {
$option_name = $wpdb->get_blog_prefix() . $option_name;
return update_user_meta( $user_id, $option_name, $newvalue );
* Deletes user option with global blog capability.
* User options are just like user metadata except that they have support for
* global blog options. If the 'is_global' parameter is false, which it is by default,
* it will prepend the WordPress table prefix to the option name.
* @global wpdb $wpdb WordPress database abstraction object.
* @param int $user_id User ID
* @param string $option_name User option name.
* @param bool $is_global Optional. Whether option name is global or blog specific.
* Default false (blog specific).
* @return bool True on success, false on failure.
function delete_user_option( $user_id, $option_name, $is_global = false ) {
$option_name = $wpdb->get_blog_prefix() . $option_name;
return delete_user_meta( $user_id, $option_name );
* Retrieves user info by user ID.
* @param int $user_id User ID.
* @return WP_User|false WP_User object on success, false on failure.
function get_user( $user_id ) {
return get_user_by( 'id', $user_id );
* Retrieves list of users matching criteria.
* @param array $args Optional. Arguments to retrieve users. See WP_User_Query::prepare_query()
* for more information on accepted arguments.
* @return array List of users.
function get_users( $args = array() ) {
$args = wp_parse_args( $args );
$args['count_total'] = false;
$user_search = new WP_User_Query( $args );
return (array) $user_search->get_results();
* Lists all the users of the site, with several options available.
* @param string|array $args {
* Optional. Array or string of default arguments.
* @type string $orderby How to sort the users. Accepts 'nicename', 'email', 'url', 'registered',
* 'user_nicename', 'user_email', 'user_url', 'user_registered', 'name',
* 'display_name', 'post_count', 'ID', 'meta_value', 'user_login'. Default 'name'.
* @type string $order Sorting direction for $orderby. Accepts 'ASC', 'DESC'. Default 'ASC'.
* @type int $number Maximum users to return or display. Default empty (all users).
* @type bool $exclude_admin Whether to exclude the 'admin' account, if it exists. Default false.
* @type bool $show_fullname Whether to show the user's full name. Default false.
* @type string $feed If not empty, show a link to the user's feed and use this text as the alt
* parameter of the link. Default empty.
* @type string $feed_image If not empty, show a link to the user's feed and use this image URL as
* clickable anchor. Default empty.
* @type string $feed_type The feed type to link to, such as 'rss2'. Defaults to default feed type.
* @type bool $echo Whether to output the result or instead return it. Default true.
* @type string $style If 'list', each user is wrapped in an `<li>` element, otherwise the users
* will be separated by commas.
* @type bool $html Whether to list the items in HTML form or plaintext. Default true.
* @type string $exclude An array, comma-, or space-separated list of user IDs to exclude. Default empty.
* @type string $include An array, comma-, or space-separated list of user IDs to include. Default empty.
* @return string|null The output if echo is false. Otherwise null.
function wp_list_users( $args = array() ) {
'show_fullname' => false,
$parsed_args = wp_parse_args( $args, $defaults );
$query_args = wp_array_slice_assoc( $parsed_args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
$query_args['fields'] = 'ids';
* Filters the query arguments for the list of all users of the site.
* @param array $query_args The query arguments for get_users().
* @param array $parsed_args The arguments passed to wp_list_users() combined with the defaults.
$query_args = apply_filters( 'wp_list_users_args', $query_args, $parsed_args );
$users = get_users( $query_args );
foreach ( $users as $user_id ) {
$user = get_userdata( $user_id );
if ( $parsed_args['exclude_admin'] && 'admin' === $user->display_name ) {
if ( $parsed_args['show_fullname'] && '' !== $user->first_name && '' !== $user->last_name ) {
/* translators: 1: User's first name, 2: Last name. */
_x( '%1$s %2$s', 'Display name based on first name and last name' ),
$name = $user->display_name;
if ( ! $parsed_args['html'] ) {
continue; // No need to go further to process HTML.
if ( 'list' === $parsed_args['style'] ) {
if ( ! empty( $parsed_args['feed_image'] ) || ! empty( $parsed_args['feed'] ) ) {
if ( empty( $parsed_args['feed_image'] ) ) {
$row .= '<a href="' . get_author_feed_link( $user->ID, $parsed_args['feed_type'] ) . '"';
if ( ! empty( $parsed_args['feed'] ) ) {
$alt = ' alt="' . esc_attr( $parsed_args['feed'] ) . '"';
$name = $parsed_args['feed'];
if ( ! empty( $parsed_args['feed_image'] ) ) {
$row .= '<img src="' . esc_url( $parsed_args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
if ( empty( $parsed_args['feed_image'] ) ) {
$return .= ( 'list' === $parsed_args['style'] ) ? '</li>' : ', ';
$return = rtrim( $return, ', ' );
if ( ! $parsed_args['echo'] ) {