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. change Dashboard default page on login
  28. How to get a list of all possible values of a specific user meta key?
  29. Hide wordpress field if data is empty in post!
  30. Change the site tagline (or similar) based on current page
  31. Is it necessary to sanitize wp_set_password user input?
  32. Trouble creating conditional PHP for nav menu items with children for custom Walker
  33. Setting custom canonical urls
  34. Only show search results with if current date is between two dates?
  35. WordPress custom login form using Ajax
  36. database interactions using OOP
  37. Customizer: Output default value in Customizer CSS
  38. How to add just one specific page to widget without plugin?
  39. Remove one value in dismissed_wp_pointers?
  40. create a select input with menus created on a custom options page
  41. Taxonomy linked to pages
  42. How to give new users two specific user role options upon WordPress user registration
  43. Create another “Display Site Title and Tagline” checkbox, “Header Text Color” setting and control
  44. Accessing data from a non-WP database/table within a page content
  45. WordPress blog post url on category page
  46. Changing layout with wp_customise
  47. WP dynamic featured image – Can’t get second featured image url
  48. Converting HTML Template to WordPress Theme
  49. Extending the WP_Widget_Text class
  50. Wp-login appears White Screen, Error: Cannot modify header information
  51. User’s total comment count excluding comments on their own posts
  52. How to: PHP Log Out Link?
  53. Login/logout in header
  54. Selectbox in admin panel function linking to CSS
  55. Why is my cookie not unsetting upon logout? [closed]
  56. Can I install/embed WordPress on a ‘single page’?
  57. Show all posts by author
  58. How to check if a meta value has already been assigned to any user?
  59. How to hide products that do not have an image from a slider carousel for an ecommerce webpage?
  60. Custom user login page by creating a plugin
  61. Customize Theme comment template to Insert VoteUp and VoteDown buttons
  62. filter default query to show just selected level of child pages in wordpress
  63. Retrieve $_POST data submitted from external URL in WordPress(NOT API)
  64. Trying to build simple deposit code that hooks into woocommerce
  65. How to pick the default selected value in wordpress dropdown?
  66. Cannot access wp-admin/wp-login.php (WordPress backend) anymore, what could be wrong?
  67. How to debug my custom login form looping intermittently
  68. Send an email to specific adress when button is clicked?
  69. Modify user profile data through scripting?
  70. Customizer: active_callback and sanitize_callback incompatibility?
  71. Saving and Restoring a Canvas on A Individual User Basis
  72. Redirect specific user role to specific site after login?
  73. Send multiple attachments with wp_mail in PHP
  74. WordPress admin pointers tour bug?
  75. getting logged in user info (wp-load.php) from parent directory
  76. Get users that likes the post
  77. custom url rewrite for wordpress
  78. Extend plugin Class through functions.php
  79. Open post-content in archive page in a Modal box with bootstrap
  80. How to add a do_action on refreshing of WP customizer?
  81. custom user role wordpress – grant guest access to edit.php without insert/update/delete
  82. add_cap not working
  83. Customize position of social icons in upme plugin [closed]
  84. Weird 404 URL Problem – domain name being placed at end of urls
  85. How to get the value of input hidden html from text editor to custom page template?
  86. Make a list of sites for each user in WPMU – switch_to_blog (display in SITE_ID: 1)
  87. Menu jumping when calling it via PHP
  88. Do not execute If User is login as Administrator or any Specific Role
  89. Output Video Download Link in template
  90. Modify custom field input data before creating a new post
  91. Output custom field value to post string
  92. Compare the old get_theme_mod($name) to the new get_theme_mod($name) return value
  93. User can not login
  94. Using $wpdb (WPDB class) ‘replace’ with multiple WHERE criteria problem
  95. Media Upload , file name changed automatically
  96. wordpress archive a user
  97. Replace shortcode in substring
  98. Custom posts in different columns style
  99. Problem with login form
  100. Best practice for show data to one specific user?
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
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 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