Actions/Filters
Below is a list of WordPress hooks that you can use to add/alter behaviours to Login With AJAX
This list is not exhaustive and does not document every filter/action in the plugin. If you feel we're missing a hook, we're happy to add one if it makes sense, but please first check the PHP code in case it exists but isn't documented. We aim to include all the known hooks over time.
lwa_loaded action
Fired when the plugin has finished loading its core files. Use this when adding extra functionality that depends on LWA core classes and functions that have been loaded or initialized.
Example
Here we disable logout redirects entirely for the ip address 123.123.123.123 by disabling the action added by LWA during initialization.
function my_lwa_loaded(){
if( $_SERVER['REMOTE_ADDR'] == '123.123.123.123' ){
remove_action('wp_logout', 'LoginWithAjax::logoutRedirect');
}
}
add_action('lwa_loaded', 'my_lwa_loaded');
lwa_login_form action
Allows adding extra content to the bottom of the login form, such as text or extra input fields. No parameters are passed.
Note You can also use the native WordPress action login_form, which would also add the same content to the regular WP login page/form.
Example
Here we will add a hidden input field to the login form, which could be used for further action during the login phase.
function my_lwa_login_form(){
echo '<input name="some_field" type="hidden" value="'. generate_special_value() .'">';
}
add_action('lwa_login_form', 'my_lwa_login_form');
lwa_lostpassword_form action
Allows adding extra content to the bottom of the password recovery form, such as text or extra input fields. No parameters are passed.
Note You can also use the native WordPress action lostpassword_form, which would also add the same content to the regular WP password recovery form.
Example
Here we will add a hidden input field to the login form, which could be used for further action during the password recovery phase.
function my_lwa_lostpassword_form(){
echo '<input name="some_field" type="hidden" value="'. generate_special_value() .'">';
}
add_action('lwa_lostpassword_form', 'my_lwa_lostpassword_form');
lwa_register_form action
Allows adding extra content to the bottom of the registration form, such as text or extra input fields. No parameters are passed.
Note You can also use the native WordPress action register_form, which would also add the same content to the regular WP registration form.
Example
Here we will add a hidden input field to the registration form, which could be used for further action during the registration phase.
function my_lwa_register_form(){
echo '<input name="some_field" type="hidden" value="'. generate_special_value() .'">';
}
add_action('lwa_register_form', 'my_lwa_register_form');
lwa_before_get_templates action
Fired before templates are scanned for and loaded into the plugin. You could use this to optimize the plugin loading process should you have specific templates your environment needs to use and want to remove any further templates from being considered as options.
Example
This will add a default theme with a specific template path. If LoginWithAJAX::$templates
has been defined after this hook has fired, no further templates are searched or loaded.
function my_lwa_before_get_templates(){
LoginWithAJAX::$templates = array( 'default' => '/some/template/directory/path/' );
}
add_action('lwa_before_get_templates', 'my_lwa_before_get_templates');
lwa_after_get_templates action
Fired after templates are scanned for and loaded into the plugin. This may be useful if you want to add an additional template in a specific location outside of your theme or wp-content/plugin-templates/login-with-ajax/
, or replace the templates list entirely.
Example
This will change the default theme location that ships with LWA by default to /some/template/directory/path/
.
function my_lwa_after_get_templates(){
LoginWithAJAX::$templates['default'] = '/some/template/directory/path/';
}
add_action('lwa_after_get_templates', 'my_lwa_after_get_templates');
lwa_js_vars filter
Add or modify custom JS variables which are localized to the JS object LWA, which can then be used by your custom JS or alter or add extra functionality to LWA.
Example
This will change the url used by LWA to send AJAX requests to, by adding a custom query parameter, as well as adding a custom property to the LWA variable. LWA.custom
can then be accessed by custom JS
function my_lwa_js_vars( $vars ){
$js_vars['ajaxurl'] = add_query_arg('custom', 'value', admin_url('admin-ajax.php', $schema));
$js_vars['custom'] = 'value';
}
add_action('lwa_js_vars', 'my_lwa_js_vars');
lwa_ajax_login filter
Modify the response returned by LWA during an AJAX call where the user has attempted to log in.
Note If you want to run verification checks before a login occurs, we recommend hooking into WP standard hooks and returning an error there, since this hook is fired after a login has been attempted. The example below is merely for reference of what is possible.
The response will contain an associative array with at least the result key and either error or message key depending on the result:
array(
'action' => 'login', // always returned for logins
'result' => true, // or false
'message' => 'If result is true, this is used',
'error' => 'If result is false, this is used.',
'redirect' => 'https://...', // if included, user is redirected
'widget' => 'https://...', // if refresh/redirect is disabled, load widget dynamically
);
Example
In this ficticious example, if the user login is successful (meaning other checks have passed) but a special nonce you've added to the login form doesn't match your nonce check, then you log the user out immediately and return an error message. However, for validation we'd recommend hooking into the WP action authenticate instead.
function my_lwa_ajax_login( $response ){
if( $response['result'] && $_REQUEST['nonce'] !== some_nonce_check() ){
$return['result'] = false;
$return['error'] = 'Some special nonce check has failed';
remove_action('wp_logout', 'LoginWithAjax::logoutRedirect');
wp_logout();
}
return $response;
}
add_action('lwa_ajax_login', 'my_lwa_ajax_login');
lwa_ajax_remember filter
Modify the response returned by LWA during an AJAX call where the user has attempted to request a password recovery.
Note If you want to run verification checks before providing password recovery, we recommend hooking into WP standard hooks and returning an error there, since this hook is fired after a password request has been attempted.
The response will contain an associative array with at least the result key and either error or message key depending on the result:
array(
'action' => 'remember',
'result' => true, // or false
'message' => 'If result is true, this is used',
'error' => 'If result is false, this is used.',
);
Example
In this ficticious example, if the user password recovery was successful, we'll replace the success message.
function my_lwa_ajax_remember( $response ){
if( $response['result'] ){
$return['message'] = 'We sent you an email... Please check your spam folder too!';
}
return $response;
}
add_action('my_lwa_ajax_remember', 'my_lwa_ajax_remember');
lwa_ajax_register filter
Modify the response returned by LWA during an AJAX call where the user has attempted to register an account.
Note If you want to run verification checks before registering, we recommend hooking into WP standard hooks and returning an error there, since this hook is fired after a registration has been attempted.
The response will contain an associative array with at least the result key and either error or message key depending on the result:
array(
'action' => 'register',
'result' => true, // or false
'message' => 'If result is true, this is used',
'error' => 'If result is false, this is used.',
);
Example
In this ficticious example, if the user password recovery was successful, we'll replace the success message.
function my_lwa_ajax_register( $response ){
if( $response['result'] ){
$return['message'] = 'We sent you an email... Please check your spam folder too!';
}
return $response;
}
add_action('lwa_ajax_register', 'my_lwa_ajax_register');
lwa_output_data filter
Modify the $args array passed to the LoginWithAJAX::output() function, which would also override all shortcode, block and widget options.
Example
The snippet below will change all avatar sizes to 100px and override the setting of any shortcode/widget/block.
function my_lwa_output_data( $args ){
$args['avatar_size'] = 100;
return $args;
}
add_action('lwa_output_data', 'my_lwa_output_data');
lwa_get_template_data_{template_name} filter
Modify the label and location data of a template where the folder/name of the template is {template_name}. Particularly useful if you'd like to add a label to your custom template.
Example
The snippet below will add a label for the custom template, which would correspond to a custom template folder created by you or your theme.
function my_lwa_get_template_data_custom( $template ){
$template['label'] = 'My Custom Template';
return $template;
}
add_action('lwa_get_template_data_custom', 'my_lwa_get_template_data_custom');