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