0

I am building a custom login and register form in the theme of my client's website, and I need to process any error messages into a variable that can be displayed on the page within the form. I am using shortcodes for the forms themselves, so using the 'after_setup_theme' action executes after the shortcodes are rendered, meaning any variables I set can't be used in the shortcode itself. This also means that on a successful login, is_user_logged_in() was already checked beforehand, so it still thinks the user is not actually logged in.

Is there another action that takes place BEFORE 'after_setup_theme' that I can use to process the login and store any error variables?

EDIT: Here is the custom function for processing the login and the action hook. For the moment, I have the error printing on the page and moving it into the form with jQuery, but I'd rather find a solution that allows me to print it where it belongs, so I don't have to manipulate the DOM elements on load.

 function custom_login() { if(!is_admin() && !preg_match('*wp-login.php*', $_SERVER['REQUEST_URI'])) { $username = isset($_POST['log']) && strlen($_POST['log']) ? $_POST['log'] : ''; $password = isset($_POST['pwd']) && strlen($_POST['pwd']) ? $_POST['pwd'] : ''; if(strlen($username)) { $creds = array(); $creds['user_login'] = $username; $creds['user_password'] = $password; $creds['remember'] = true; $user = wp_signon($creds, false); if (is_wp_error($user)) { $login_error = '<div style="display: none;" id="login-error">'.$user->get_error_message().'</div>'; echo $login_error; } else { if(isset($_POST['redirect_to']) && strlen($_POST['redirect_to']) > 3) { wp_redirect($_POST['redirect_to']); } else { wp_redirect(home_url().'/login/'); } } } } } // run it before the headers and cookies are sent if(isset($_POST['frontend_login'])) { add_action('after_setup_theme', 'custom_login'); } 
2
  • 1
    Can you update the question with the relevant codes? that would help. Commented Feb 3, 2014 at 6:19
  • Sorry about that Maruti. I just posted the function in question above. Commented Feb 3, 2014 at 7:07

1 Answer 1

1

after_setup_theme should not execute after the shortcodes process. Shortcodes process when the post body renders. after_setup_theme runs well before that. Your analysis of the problem is incorrect.

This looks like a variable scope problem to me. None of the variables inside your custom_login function are going to be accessible outside that function. Their "scope" is inside the function.

To solve this you need to declare your variables global or wrap the whole thing in a class so that you can get to the variable using class syntax. Proof of concept:

class test_class { static $test = 'hi'; public function change_var() { static::$test = 'hello'; } } add_action('after_setup_theme','test_class::change_var'); 

There are other ways to do it.

1
  • I need a stupid hammer to smack me upside the head, I hadn't even thought about using a global...jeez, I'm losing it lol. Nevertheless, thanks for the response and helping me figure out the problem. Looks like I'm too newb on the site to upvote your answer, but you definitely have my upvote if I could give it. Thanks! Commented Feb 3, 2014 at 16:19

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.