I need to write code before user can enter username and password. So, I have code written just after the following line in wp-login.php page:
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'login';
The above line is somewhere # 372.
My line:
if ($action == 'login') {
// My code
}
When done like this, script run before even username and pwd box comes. I am thinking of adding hook etc so that I do not need to modify wp-login.php page.
Any idea?
1 Answer
If you don’t care about the actual action you can use login_init
action like this:
add_action( 'login_init', 'wpse8170_login_init' );
function wpse8170_login_init() {
// do your stuff here
}
But if you want to handle only special actions, then you need to hook into login_form_{action}
action, like this:
add_action( 'login_form_login', 'wpse8170_login_form_login' );
function wpse8170_login_form_login() {
// do your login stuff here
}
Or like this:
add_action( 'login_form_register', 'wpse8170_login_form_register' );
function wpse8170_login_form_register() {
// do your register stuff here
}
If you need to add some CSS or JS to your login page, then you need to use login_enqueue_scripts
action:
add_action( 'login_enqueue_scripts', 'wpse8170_login_enqueue_scripts' );
function wpse8170_login_enqueue_scripts() {
wp_enqueue_script( ... );
wp_enqueue_style( ... );
}