Skip to content

Instantly share code, notes, and snippets.

@mehrshaddarzi
Created March 22, 2025 08:57
Show Gist options
  • Save mehrshaddarzi/e94023f0d772bff360f4739c915ebf7a to your computer and use it in GitHub Desktop.
Save mehrshaddarzi/e94023f0d772bff360f4739c915ebf7a to your computer and use it in GitHub Desktop.
Prevent WP Login WordPress
<?php
// Hook the appropriate WordPress action
add_action('init', 'prevent_wp_login');
function prevent_wp_login() {
// WP tracks the current page - global the variable to access it
global $pagenow;
// Check if a $_GET['action'] is set, and if so, load it into $action variable
$action = (isset($_GET['action'])) ? $_GET['action'] : '';
// Check if we're on the login page, and ensure the action is not 'logout'
if( $pagenow == 'wp-login.php' && ( ! $action || ( $action && ! in_array($action, array('logout', 'lostpassword', 'rp', 'resetpass'))))) {
// Load the home page url
$page = get_bloginfo('url');
// Redirect to the home page
wp_redirect($page);
// Stop execution to prevent the page loading for any reason
exit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment