0

I need to set a global variable that will be used in the page set on wp_redirect

global $open;
$open = FALSE;    

function open_menu($user_login) {
   global $open;
   $open = TRUE;
   wp_redirect(home_url());
   exit;
}
add_action('wp_login', 'open_menu');

After redirect, I'm using the variable to open dropdown menu:

<div class="dropdown login-dropdown<?php  global $open; if($open == TRUE){echo " open";} ?>">

But is not working

  • I'm not at all sure what the purpose of such a variable would be but you need to declare your global variable outside of your function, declare it again inside your function (for scope) then set it. – Howdy_McGee Jul 30 '14 at 13:42
  • @Howdy_McGee I update my question. – marcelo2605 Jul 30 '14 at 16:53
1

I think wp_login is executing before functions.php is actually opened, thus not setting the global variable. One possible solution to this is instead append to the URL:

function open_menu($user_login) {
    wp_redirect(home_url().'?menu_open=true');
    exit;
}
add_action('wp_login', 'open_menu');

Then you could get that value via

if(isset($_GET['menu_open']) && $_GET['menu_open'])
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.