WP_User example

// Remove parenthesized timezone string if it exists, as this confuses strtotime().                 $ddate           = preg_replace( '!\s*\(.+\)\s*$!', '', $ddate );
                $ddate_timestamp = strtotime( $ddate );
                $post_date       = gmdate( 'Y-m-d H:i:s', $ddate_timestamp + $time_difference );
                $post_date_gmt   = gmdate( 'Y-m-d H:i:s', $ddate_timestamp );
            }
        }
    }

    // Set $post_status based on $author_found and on author's publish_posts capability.     if ( $author_found ) {
        $user        = new WP_User( $post_author );
        $post_status = ( $user->has_cap( 'publish_posts' ) ) ? 'publish' : 'pending';
    } else {
        // Author not found in DB, set status to pending. Author already set to admin.         $post_status = 'pending';
    }

    $subject = trim( $subject );

    if ( 'multipart/alternative' === $content_type ) {
        $content = explode( '--' . $boundary$content );
        $content = $content[2];

        

function wp_delete_user( $id$reassign = null ) {
    global $wpdb;

    if ( ! is_numeric( $id ) ) {
        return false;
    }

    $id   = (int) $id;
    $user = new WP_User( $id );

    if ( ! $user->exists() ) {
        return false;
    }

    // Normalize $reassign to null or a user ID. 'novalue' was an older default.     if ( 'novalue' === $reassign ) {
        $reassign = null;
    } elseif ( null !== $reassign ) {
        $reassign = (int) $reassign;
    }

    
foreach ( $this->results as $result ) {
                $result->id = $result->ID;
            }
        } elseif ( 'all_with_meta' === $qv['fields'] || 'all' === $qv['fields'] ) {
            if ( function_exists( 'cache_users' ) ) {
                cache_users( $this->results );
            }

            $r = array();
            foreach ( $this->results as $userid ) {
                if ( 'all_with_meta' === $qv['fields'] ) {
                    $r[ $userid ] = new WP_User( $userid, '', $qv['blog_id'] );
                } else {
                    $r[] = new WP_User( $userid, '', $qv['blog_id'] );
                }
            }

            $this->results = $r;
        }
    }

    /** * Retrieves query variable. * * @since 3.5.0 * * @param string $query_var Query variable key. * @return mixed */
'icon'          => wp_mime_type_icon( $attachment->ID ),
        'dateFormatted' => mysql2date( __( 'F j, Y' )$attachment->post_date ),
        'nonces'        => array(
            'update' => false,
            'delete' => false,
            'edit'   => false,
        ),
        'editLink'      => false,
        'meta'          => false,
    );

    $author = new WP_User( $attachment->post_author );

    if ( $author->exists() ) {
        $author_name            = $author->display_name ? $author->display_name : $author->nickname;
        $response['authorName'] = html_entity_decode( $author_name, ENT_QUOTES, get_bloginfo( 'charset' ) );
        $response['authorLink'] = get_edit_user_link( $author->ID );
    } else {
        $response['authorName'] = __( '(no author)' );
    }

    if ( $attachment->post_parent ) {
        $post_parent = get_post( $attachment->post_parent );
        


    // return a comma-separated list of role names for the given user     public static function get_user_roles( $user_id ) {
        $comment_user = null;
        $roles = false;

        if ( !class_exists('WP_User') )
            return false;

        if ( $user_id > 0 ) {
            $comment_user = new WP_User( $user_id );
            if ( isset( $comment_user->roles ) )
                $roles = implode( ',', $comment_user->roles );
        }

        if ( is_multisite() && is_super_admin( $user_id ) ) {
            if ( empty( $roles ) ) {
                $roles = 'super_admin';
            } else {
                $comment_user->roles[] = 'super_admin';
                $roles = implode( ',', $comment_user->roles );
            }
        }
$email_password = true;
            $user_created   = true;
        } elseif ( ! $user_id ) {
            // Password has been provided.             $message      = '<em>' . __( 'Your chosen password.' ) . '</em>';
            $user_id      = wp_create_user( $user_name$user_password$user_email );
            $user_created = true;
        } else {
            $message = __( 'User already exists. Password inherited.' );
        }

        $user = new WP_User( $user_id );
        $user->set_role( 'administrator' );

        if ( $user_created ) {
            $user->user_url = $guessurl;
            wp_update_user( $user );
        }

        wp_install_defaults( $user_id );

        wp_install_maybe_enable_pretty_permalinks();

        
global $current_user;

        // If `$id` matches the current user, there is nothing to do.         if ( isset( $current_user )
        && ( $current_user instanceof WP_User )
        && ( $id == $current_user->ID )
        && ( null !== $id )
        ) {
            return $current_user;
        }

        $current_user = new WP_User( $id$name );

        setup_userdata( $current_user->ID );

        /** * Fires after the current user is set. * * @since 2.0.1 */
        do_action( 'set_current_user' );

        return $current_user;
    }

function wp_authenticate_cookie( $user$username$password ) {
    if ( $user instanceof WP_User ) {
        return $user;
    }

    if ( empty( $username ) && empty( $password ) ) {
        $user_id = wp_validate_auth_cookie();
        if ( $user_id ) {
            return new WP_User( $user_id );
        }

        global $auth_secure_cookie;

        if ( $auth_secure_cookie ) {
            $auth_cookie = SECURE_AUTH_COOKIE;
        } else {
            $auth_cookie = AUTH_COOKIE;
        }

        if ( ! empty( $_COOKIE[ $auth_cookie ] ) ) {
            
$media_dims = '';
    $meta       = wp_get_attachment_metadata( $attachment_id );

    if ( isset( $meta['width']$meta['height'] ) ) {
        $media_dims .= "<span id='media-dims-$attachment_id'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
    }
    /** This filter is documented in wp-admin/includes/media.php */
    $media_dims = apply_filters( 'media_meta', $media_dims$post );

    $att_url = wp_get_attachment_url( $attachment_id );

    $author = new WP_User( $post->post_author );

    $uploaded_by_name = __( '(no author)' );
    $uploaded_by_link = '';

    if ( $author->exists() ) {
        $uploaded_by_name = $author->display_name ? $author->display_name : $author->nickname;
        $uploaded_by_link = get_edit_user_link( $author->ID );
    }
    ?> <div class="misc-pub-section misc-pub-uploadedby"> <?php if ( $uploaded_by_link ) { ?>
function update_user_status( $id$pref$value$deprecated = null ) {
    global $wpdb;

    _deprecated_function( __FUNCTION__, '5.3.0', 'wp_update_user()' );

    if ( null !== $deprecated ) {
        _deprecated_argument( __FUNCTION__, '3.0.2' );
    }

    $wpdb->update( $wpdb->users, array( sanitize_key( $pref ) => $value ), array( 'ID' => $id ) );

    $user = new WP_User( $id );
    clean_user_cache( $user );

    if ( 'spam' === $pref ) {
        if ( $value == 1 ) {
            /** This filter is documented in wp-includes/user.php */
            do_action( 'make_spam_user', $id );
        } else {
            /** This filter is documented in wp-includes/user.php */
            do_action( 'make_ham_user', $id );
        }
    }

    


        if ( isset( $request['post'] ) ) {
            $prepared_comment['comment_post_ID'] = (int) $request['post'];
        }

        if ( isset( $request['parent'] ) ) {
            $prepared_comment['comment_parent'] = $request['parent'];
        }

        if ( isset( $request['author'] ) ) {
            $user = new WP_User( $request['author'] );

            if ( $user->exists() ) {
                $prepared_comment['user_id']              = $user->ID;
                $prepared_comment['comment_author']       = $user->display_name;
                $prepared_comment['comment_author_email'] = $user->user_email;
                $prepared_comment['comment_author_url']   = $user->user_url;
            } else {
                return new WP_Error(
                    'rest_comment_author_invalid',
                    __( 'Invalid comment author ID.' ),
                    array( 'status' => 400 )
                );

function wpmu_delete_user( $id ) {
    global $wpdb;

    if ( ! is_numeric( $id ) ) {
        return false;
    }

    $id   = (int) $id;
    $user = new WP_User( $id );

    if ( ! $user->exists() ) {
        return false;
    }

    // Global super-administrators are protected, and cannot be deleted.     $_super_admins = get_super_admins();
    if ( in_array( $user->user_login, $_super_admins, true ) ) {
        return false;
    }

    

function user_can( $user$capability, ...$args ) {
    if ( ! is_object( $user ) ) {
        $user = get_userdata( $user );
    }

    if ( empty( $user ) ) {
        // User is logged out, create anonymous user object.         $user = new WP_User( 0 );
        $user->init( new stdClass() );
    }

    return $user->has_cap( $capability, ...$args );
}

/** * Retrieves the global WP_Roles instance and instantiates it if necessary. * * @since 4.3.0 * * @global WP_Roles $wp_roles WordPress role management object. * * @return WP_Roles WP_Roles global instance if not already instantiated. */
Home | Imprint | This part of the site doesn't use cookies.