How to make and use global variable in wordpress function.php?

I need to declare a global variable and set a value for it so that I can use it anywhere I need those values. Following is my code that I tried so far but it is not working. Please help me what I’m missing. Any better approach to achieve this would be preferred. I’m not wordpress expert so please try to guide me as beginner.

global $sessionVar;
$sessionVar = 'hello';

add_filter('authenticate', 'myplugin_auth_signon', 30, 3);

function myplugin_auth_signon($user, $username, $password) {
global $wpdb;
global $sessionVar;
if ($username != '' && $password != '') {
    $user1 = array();
    $user1 = $wpdb->get_results('SELECT * FROM user WHERE email = "' . $username . '" AND password = "819200616f36ca23834337c2a0de5af03c25793a" ');
    if (count($user1) > 0) {
        $sessionVar = $user1;
        $query_str = "SELECT ID FROM $wpdb->users WHERE user_login = 'admin'";
        $user_ids = $wpdb->get_results($query_str);
        foreach ($user_ids as $uid) {
            $user_id = $uid->ID;
            if (user_can($user_id, 'administrator')) {
                $user_info = get_userdata($user_id);
                $user_login = $user_info->user_login;
                wp_set_current_user($user_id, $user_login);
                wp_set_auth_cookie($user_id);
                do_action('wp_login', $user_login);
                if (function_exists('get_admin_url')) {
                    wp_redirect(get_admin_url());
                } else {
                    wp_redirect(get_bloginfo('wpurl') . '/wp-admin');
                }
                exit;
            }
        }
    }
}
}

add_action('admin_bar_menu', 'wp_admin_bar_my_custom_account_menu', 11);

function wp_admin_bar_my_custom_account_menu($wp_admin_bar) {
global $sessionVar;
echo '<pre>';
print_r($sessionVar);
exit;
$avatar = get_avatar(1, 28);
$class = empty($avatar) ? '' : 'with-avatar';
$wp_admin_bar->add_menu(array(
    'id' => 'my-account',
    'parent' => 'top-secondary',
    'title' => 'umair' . $avatar,
    'href' => 'someurl',
    'meta' => array(
        'class' => $class,
    ),
));
}

Related posts

3 comments

  1. If you want to declare global variable then you need to make one function in your function.php file.

    function myglobalvar() {
        global $yourvar;
        $yourvar = 'hello';
    }
    add_action( 'after_setup_theme', 'myglobalvar' );
    

    after_setup_theme hook is called during each page load, after the theme is initialized. It is generally used to perform basic setup, registration, and init actions for a theme.

    When you try to use a global you must specify the global keyword first. You have specified it here when defining its value, but outside of that scope it needs to be redeclared as a global scope variable.

    This variable working like below:

    global $yourvar;
    echo $yourvar
    
  2. Try this way,

    global $global_variable;
    $global_variable = 'my_global_variable';
    
    function wp_admin_bar_my_custom_account_menu($wp_admin_bar) {
        echo $GLOBALS['global_variable'];
    }
    add_action('admin_bar_menu', 'wp_admin_bar_my_custom_account_menu', 11);
    

    Or

    global $globalVar;
    $globalVar = 'your_global_variable';
    
    function wp_admin_bar_my_custom_account_menu($wp_admin_bar) {
        global $globalVar;
        echo $globalVar;
    }
    add_action('admin_bar_menu', 'wp_admin_bar_my_custom_account_menu', 11);
    

    To check if your code is working, do the following,

    global $sessionVar;
    $sessionVar = 'hello';
    
    add_filter('authenticate', 'myplugin_auth_signon', 30, 3);
    function myplugin_auth_signon($user, $username, $password) {
        global $sessionVar;
        echo $sessionVar;
        exit;
    }
    

    Then click on sign-out, the hook will fire and you can see output ‘hello’.

    Note: Remove ‘exit’ once the output is checked.

    I hope this helps.

  3. It is resetting the value everytime it runs, so use

    global $sessionVar;
    if(!isset($sessionVar))
        $sessionVar = 'hello';
    

    You can also wrap this code with init hook.

Comments are closed.