WordPress 4.5 deprecated get_currentuserinfo()

The answer lies within the wp_get_current_user() pluggable function. I simply changed the function name get_currentuserinfo() to wp_get_current_uesr() then made sure the returns were not boolean but return $current_user.

This seems to be working well, including caching, etc.

Hopefully this helps others.

if ( ! function_exists( 'wp_get_current_user' ) ) :

/**
 * This replacement function will no longer work after WordPress 4.5
 * The pluggable function was deprecated in WP 4.5
 *
 * @return void|boolean
 *
 * @since 2.5.1.03
 *        Added apply_filter to match WP get_currentuserinfo()
 *
 * @since 3.0.2.01
 *        wp_get_current_user instead of get_currentuserinfo()
 */

function wp_get_current_user() {
    global $current_user;

    if ( ! empty( $current_user ) ) {
        if ( $current_user instanceof WP_User ) {
            return $current_user;
        }

        // Upgrade stdClass to WP_User
        if ( is_object( $current_user ) && isset( $current_user->ID ) ) {
            $cur_id       = $current_user->ID;
            $current_user = null;
            wp_set_current_user( $cur_id );
            return $current_user;
        }

        // $current_user has a junk value. Force to WP_User with ID 0.
        $current_user = null;
        wp_set_current_user( 0 );
        return $current_user;
    }

    if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
        wp_set_current_user( 0 );
        return $current_user;
    }

    $visitor = XenWord::getVisitor();

    $user_id = $visitor['user_id'];

    // Conditional for no XenForo user is logged in
    if ( $user_id == 0 ) {
        $current_user = null;
        wp_set_current_user( 0 );
        return $current_user;
    }

    /**
     * Filter the current user.
     *
     * The default filters use this to determine the current user from the
     * request's cookies, if available.
     *
     * Returning a value of false will effectively short-circuit setting
     * the current user.
     *
     * @since 3.9.0
     *
     * @param int|bool $user_id User ID if one has been determined, false otherwise.
     */
    $user_id = apply_filters( 'determine_current_user', false );
    if ( ! $user_id ) {
        wp_set_current_user( 0 );
        return $current_user;
    }

    $current_user = get_userdata( $user_id );

    wp_set_current_user( $user_id );

    wp_validate_auth_cookie($cookie="", $scheme="");

    // Check to determine if adding XF users to WP database
    XenWord_XF_Users::check_options();

    return $current_user;
}

endif;

Leave a Comment