WordPress.org

Make WordPress Core

Opened 3 months ago

#42412 new defect (bug)

Calling get_current_user_id() and setting $current_user gives unexpected behaviour

Reported by: gertmeeusen Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.8.3
Component: General Keywords:
Focuses: Cc:

Description

Steps to reproduce, paste this code in functions.php

<?php
if ( ! function_exists( 'wp_get_current_user' ) ) {
    die('The file with wp_get_current_user() has not loaded yet into memory.');
}else{
    
    echo '----echo get_current_user_id()----</br>';
    
    echo '$current_user global? ';
    echo array_key_exists('current_user', $GLOBALS)?"true</br>":"false</br>";

    echo get_current_user_id() . '</br>';
    
    echo '$current_user global? ';
    echo array_key_exists('current_user', $GLOBALS)?"true</br>":"false</br>";

    echo get_current_user_id() . '</br>';
    echo get_current_user_id() . '</br>';
    
    echo '----echo $current_user after 1 call----</br>';
    $current_user = get_current_user_id();
    echo $current_user . '</br>';
    echo $current_user . '</br>';
    echo $current_user . '</br>';
    
    echo '----echo $current_user, 1 call each time----</br>';
    $current_user = get_current_user_id();
    echo $current_user . '</br>';
    $current_user = get_current_user_id();
    echo $current_user . '</br>';
    $current_user = get_current_user_id();
    echo $current_user . '</br>';
    $current_user = get_current_user_id();
    echo $current_user . '</br>';
    $current_user = get_current_user_id();
    echo $current_user . '</br>';
    
    echo '----echo $current_user, $current_user = 5 before each call----</br>';
    $current_user = 5;
    $current_user = get_current_user_id();
    echo $current_user . '</br>';
    $current_user = 5;
    $current_user = get_current_user_id();
    echo $current_user . '</br>';
    $current_user = 5;
    $current_user = get_current_user_id();
    echo $current_user . '</br>';
    $current_user = 5;
    $current_user = get_current_user_id();
    echo $current_user . '</br>';
    $current_user = 5;
    $current_user = get_current_user_id();
    echo $current_user . '</br>';

    echo '----echo $current_user, $current_user = 5 NO call----</br>';
    $current_user = 5;
    echo $current_user . '</br>';
    $current_user = 5;
    echo $current_user . '</br>';
    $current_user = 5;
    echo $current_user . '</br>';
    $current_user = 5;
    echo $current_user . '</br>';
    $current_user = 5;
    echo $current_user . '</br>';

    echo '----echo $cu, 1 call each time---</br>';
    $cu = get_current_user_id();
    echo $cu . '</br>';
    $cu = get_current_user_id();
    echo $cu . '</br>';
    $cu = get_current_user_id();
    echo $cu . '</br>';
    $cu = get_current_user_id();
    echo $cu . '</br>';
    
    echo '----echo $current_user_1, 1 call each time----</br>';
    $current_user_1 = get_current_user_id();
    echo $current_user_1 . '</br>';
    $current_user_1 = get_current_user_id();
    echo $current_user_1 . '</br>';
    $current_user_1 = get_current_user_id();
    echo $current_user_1 . '</br>';
    $current_user_1 = get_current_user_id();
    echo $current_user_1 . '</br>';
    
                    
    echo '----echo $current_user->ID ----</br>';
    $current_user = wp_get_current_user();
    echo $current_user->ID . '</br>';
    $current_user = wp_get_current_user();
    echo $current_user->ID . '</br>';
    $current_user = wp_get_current_user();
    echo $current_user->ID . '</br>';
    $current_user = wp_get_current_user();
    echo $current_user->ID . '</br>';
    
    echo '----echo $current_user_obj->ID ----</br>';
    $current_user_obj = wp_get_current_user();
    echo $current_user_obj->ID . '</br>';
    $current_user_obj = wp_get_current_user();
    echo $current_user_obj->ID . '</br>';
    $current_user_obj = wp_get_current_user();
    echo $current_user_obj->ID . '</br>';
    $current_user_obj = wp_get_current_user();
    echo $current_user_obj->ID . '</br>';
}

That outputs:

----echo get_current_user_id()----
$current_user global? false
1
$current_user global? true
1
1
----echo $current_user after 1 call----
1
1
1
----echo $current_user, 1 call each time----
0
1
0
1
0
----echo $current_user, $current_user = 5 before each call----
0
0
0
0
0
----echo $current_user, $current_user = 5 NO call----
5
5
5
5
5
----echo $cu, 1 call each time---
0
0
0
0
----echo $current_user_1, 1 call each time----
0
0
0
0
----echo $current_user->ID ----
0
0
0
0
----echo $current_user_obj->ID ----
0
0
0
0

Issue in _wp_get_current_user() ?

Using php 7.0

Change History (0)

Note: See TracTickets for help on using tickets.