1

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,
    ),
));
}

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

1

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 Comments

I tried your code but it always print my_global_variable. How can I set it's value in this function:- function myplugin_auth_signon($user, $username, $password) and then use it where i need it.
I've updated the answer, adjust according to your needs it will work.
I check my code, yes it's working. I'm then setting global variable value in signon filter function like this:- $sessionVar = $user1; and when I print it again it has the expected recod in it. But when i print this global variable in wp_admin_bar_my_custom_account_menu function, it again print just "Hello".
0

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.

2 Comments

did you log out and logged in again before testing? Authenticate hook only runs when any user log into wordpress.
yes, I did logged out and logged in again for testing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.