Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

How to block specific user id in custom login form?

You can add a low priority authenticate hook that runs after all the other hooks and rejects authentication for a specific user ID:

function wpse_384212_ban_user_id( $user, $username, $password ) {
    // At this point $user is either a WP_User object if they've successfully logged in,
    // or null or a WP_Error if they haven't. We want to reject users by ID.

    if ( $user instanceof WP_User ) {
        // This is a successfully authenticated user. Check their ID.
        $banned_user_ids = array( 3, 6, 10 ); // could also store these in a site option

        if ( in_array( $user->ID, $banned_user_ids ) ) {
            return new WP_Error( 'user_banned', 'You are banned' );
        }
    }

    // Else return the user or error from previous authentication filters
    return $user;
}

add_filter( 'authenticate', 'wpse_384212_ban_user_id', 999, 3 );

This rejects users who have otherwise logged in successfully. If you wanted to reject them whether they managed to log in or not, you’ll need to look up the user to fetch their ID for the extra check:

function wpse_384212_ban_user_id( $user, $username, $password ) {
    // At this point $user is either a WP_User object if they've successfully logged in,
    // or null or a WP_Error if they haven't. We want to reject users by ID.
    $banned_user_ids = array( 3, 6, 10 ); // could also store these in a site option

    if ( $user instanceof WP_User ) {
        // This is a successfully authenticated user. Check their ID.

        if ( in_array( $user->ID, $banned_user_ids ) ) {
            return new WP_Error( 'user_banned', 'You are banned' );
        }
    } elseif ($username) {
        // Unsuccessful login but we have a username.
        // Look up the user by username or email to see if they are a banned user
        $login_user = get_user_by( 'login', $username );
        if ( ! $login_user && is_email( $username ) ) {
            $login_user = get_user_by( 'email', $username );
        }
        if ( $login_user instanceof WP_User ) {
            // This was a failed login for a valid user. Check the user's ID            
            if ( in_array( $login_user->ID, $banned_user_ids ) ) {
                return new WP_Error( 'user_banned', 'You are banned' );
            }
        }
    }

    // Else return the user or error from previous authentication filters
    return $user;
}

add_filter( 'authenticate', 'wpse_384212_ban_user_id', 999, 3 );

If this is a multisite then these would need to go in a mu-plugin.

Related Posts:

  1. Integrating WordPress to my website, while keeping my own authentication system
  2. Changing user_nicename
  3. Authenticating to WordPress, using my own authentication two-factor system
  4. automated tests as a user?
  5. there’s a way to include a minimal WP for check only the current user, its roles (caps?) and then release/free it?
  6. get_users(…) only returns one user
  7. User management system similar to wordpress one?
  8. Programmatic Login from 3rd Party site
  9. Add New User, extra fields which are required?
  10. user_profile_update_errors hook not executing
  11. Change CSS based on is_user_logged_in
  12. “operation successful” message
  13. create front-end users post list by specific category
  14. php return username of currently viewed author profile
  15. is_user_logged_in returning nothing on custom page
  16. Removing “There is no account with that username or email address.” error message in “/wp-login.php?action=lostpassword”
  17. Admin Panel 404 Error after login
  18. Get current user id in function php
  19. Having trouble creating two shortcodes, one for logged in user and one for visitors
  20. Call WP Rest-Api to GET /users/me returned NOTHING in console
  21. Redirect after login depending on the URL
  22. Redirecting the lost password page request when using a custon login page
  23. is_user_logged_in() not working in homepage
  24. List users in a dropdown for login
  25. main menu page redirects to user ID
  26. Display a list of users with avatar filterable with alphabets
  27. Adding “Remember Me” in custom login
  28. Include a external PHP file into a WordPress Custom Template
  29. How to change the wp-login.php page title?
  30. Remove option to allow trackbacks/pingbacks from post page options
  31. How to remove hardcoded characters from playlists?
  32. Display user’s total comment count outside The Loop
  33. Admin Bar (Toolbar) not showing on custom PHP file that loads WordPress
  34. Search and Replace in database: How to replace data in SQL dump file on Windows?
  35. How do I hide specific user profile fields?
  36. Infinite-Scroll Plugin and Jetpack Infinite Scroll Plugin – Adding to “Thoughts” Theme
  37. Using a nonce in a Custom Login Form
  38. Is it recommended to pass some data to scripts in `wp_enqueue_scripts`?
  39. Last time a user logged in
  40. List User order by ID in Descending order (Backend)
  41. Change the site tagline (or similar) based on current page
  42. Is it necessary to sanitize wp_set_password user input?
  43. create a select input with menus created on a custom options page
  44. Taxonomy linked to pages
  45. How to give new users two specific user role options upon WordPress user registration
  46. Converting HTML Template to WordPress Theme
  47. Custom user login page by creating a plugin
  48. How to pick the default selected value in wordpress dropdown?
  49. Customize position of social icons in upme plugin [closed]
  50. Using $wpdb (WPDB class) ‘replace’ with multiple WHERE criteria problem
  51. Media Upload , file name changed automatically
  52. Replace shortcode in substring
  53. Call to undefined function get_userdata() in plugin
  54. Log in / Log Out Custom Button
  55. How to obtain the current website URL in my theme?
  56. Hook called before text widget save
  57. customize wordpress database error page
  58. Echo title attribute php
  59. str_replace with the_content is not working
  60. Shortcode to log user into current URL
  61. List of Events with Multiple Dates: Only NEXT Date
  62. Uses for function: wp_update_user
  63. How can I add diffrent editable text fields?
  64. Allow a user or role to view drafts and previews, but not other admin privileges?
  65. Hide A Class and Add Custom HTML Code Using WordPress
  66. PHP warning – Use of undefined constant ‘FORCE_SSL_LOGIN’ ‘FORCE_SSL_ADMIN’ on wp-config.php
  67. Trying to get post ID outside loop on blog page
  68. How can I update the price when someone enters postcode or zip code in woocommerce checkout page?
  69. Trying to get property ‘ID’ and ‘post_author’ of non-object error
  70. Display specific page if user signed in
  71. Allow specific user to edit a specific page
  72. Display current user metadata on WordPress page
  73. Need help transforming echo to return for use with shortcode
  74. Noob question: want to remove the “site identity” logo for specific part of website only
  75. Refresh page after login with litespeed cache
  76. Need help with AJAX login to call php in functions.php to handle redirects based on user cap (role)
  77. Real time notification on user profile after new comment
  78. Can’t get_users info by using json_encode
  79. Check if a user is logged into my WordPress site which is on a different server
  80. How to add text before posts
  81. Add two or multiple functions in WordPress Post or Page
  82. How to associate dynamic PHP page for chosen WordPress tag?
  83. Newbie question. Login/Registration. New PHP page
  84. WordPress error on my website
  85. How to set max users to 17.000
  86. Best way to define a database with product codes and back-end support?
  87. wp_customize_image_control default value
  88. How to add button to top of theme customizer?
  89. How can I add more code to this?
  90. Edit the Publish Widget Options
  91. Cross origin ajax request always returns 0 when calling get_current_user_id();
  92. New to WordPress & Freelancing [closed]
  93. login redirect based on user role not work as expected
  94. How to lock users account until approvation
  95. How to create a User Role and give permission to only use Web Stories plugin?
  96. How to add custom user role into wordpress
  97. Redirect to current URL and append specified URL parameter on unsuccessful login through Elementor login form widget
  98. How to change product title color in shop page if product has specific product tag (Woocommerce)?
  99. Trying to GET data with ajax from database and show in fullcalendar
  100. Custom WordPress Customizer Control for Typography Presets renders blank section or fallback , despite correct class registration
Categories PHP Tags customization, id, login, php, users
Defer Parsing of “createjs.min.js” not working
WordPress GitHub Workflow

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress