BuddyDev

Search

[Resolved] WordPress Username Availability Checker + Restrict Usernames Comparability

  • Participant
    Level: Initiated
    Posts: 11
    Mocha on #23112

    I blacklisted certain usernames such that any usernames that include “mod” or “admin”, or any swear words would not be able to register using the “Restrict Usernames” plugin. That portion works: http://prntscr.com/nr2dqb

    However, when typing in the username, those blacklisted ones still shows as available. For instance, if I tried to input the name “MochaMod”, the username checker should say that the username cannot be used, but instead it says this: http://prntscr.com/nr2e7t

    However, when I fill out everything and press submit to register, this happens: http://prntscr.com/nr2eid, where it correctly blocks the user from registering with that username. However, it ends up double posting, with “Sorry, this username is invalid. Please choose another.” on the top, and “Username is available” on the bottom.

    I am not sure why this problem occurs. If anyone could provide me some guidance, it would be great. I’ve already tried disabling Youzer (community & user profile wordpress plugin) but the problem still occurs.

  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #23114

    Hi Mocha,
    Welcome to BuddyDev.

    We are using validate_username() core WordPress function for validating username. It seems the Restrict Username is not using the filter provided by it and using something else to generate error.

    Can you please contact the developer of restrict username and ask them is there any reason for not using that filter? Is it because that does not allow returning custom error?

    Also, If you can ask them what function we should use to validate the username, I will add compatibility.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 11
    Mocha on #23124

    Brajesh,

    I have contacted the developer and will let you know as soon as I get their replay. In the mean time I restricted the usernames by inserting the following code into functions.php

    function wpsnippet_validate_username($valid, $username) {
        $restricted = array('profile', 'directory', 'domain', 'download', 'downloads', 'edit', 'editor', 'email', 'ecommerce', 'forum', 'forums', 'favorite', 'feedback', 'follow', 'files', 'gadget', 'gadgets', 'games', 'guest', 'group', 'groups', 'homepage', 'hosting', 'hostname', 'httpd', 'https', 'information', 'image', 'images', 'index', 'invite', 'intranet', 'indice', 'iphone', 'javascript', 'knowledgebase', 'lists','websites', 'webmaster', 'workshop', 'yourname', 'yourusername', 'yoursite', 'yourdomain', 'mod');
        $pages = get_pages();
        foreach ($pages as $page) {
            $restricted[] = $page->post_name;
        }
        if(!$valid || is_user_logged_in() && current_user_can('create_users') ) return $valid;
        $username = strtolower($username);
        if ($valid && strpos( $username, ' ' ) !== false) $valid=false;
        if ($valid && in_array( $username, $restricted )) $valid=false;
        if ($valid && strlen($username) < 5) $valid=false;
        return $valid;
    }
    add_filter('validate_username', 'wpsnippet_validate_username', 10, 2);
     
    function wpsnippet_registration_errors($errors) {
        if ( isset( $errors->errors['invalid_username'] ) )
            $errors->errors['invalid_username'][0] = __( 'ERROR: Invalid username.', 'wpsnippet' );
        return $errors;
    }
    add_filter('registration_errors', 'wpsnippet_registration_errors');
    

    Taken from:
    https://wp-snippet.com/snippets/how-to-restrict-usernames-in-wordpress-without-a-plugin/

    However, the same issue happens where a blacklisted username (mod) first shows as available: https://prnt.sc/nrpy0p,
    then it shows it as being blocked AFTER I click register: http://prntscr.com/nrpygr

    Could you kindly provide some guidance as to why this happens?
    Thank you so much.

  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #23129

    Hi Mocha ,
    Thank you.
    The code is on the correct lines(filtering validate_username) and it should work. Please allow me to test it in couple of hours and get back to you.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 11
    Mocha on #23144

    Brajesh,
    Thank you so much for helping me with this.

  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #23150

    Hi Mocha,
    Here is the error I see
    https://i.imgur.com/jX1NCTW.png

    Are you using it on multisite? I tested on normal WordPress site and it is generating the error as expected.

    Thank you
    Brajesh

    PS:- I could not test with ‘mod’ as I got the error that minimum 4 letters are required for username.

  • Participant
    Level: Initiated
    Posts: 11
    Mocha on #23159

    Brajesh,

    I am not using it on multisite. This is what I see upon typing the word ‘image’ it in: http://prntscr.com/nshmb1
    This is what happens after I press ‘register’: http://prntscr.com/nshnsy
    And upon typing the word image again, this happens: http://prntscr.com/nsho2e

    I am quite unsure as to why this is happening.

    Thank you!

    P.S: I originally had modified the code to allow a minimum of 3 character letters but I changed it back to see if that could be causing the error. Unfortunately, the error still happened.

  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #23167

    Hi Mocha,
    Please check the link in my next reply. I have put the availability checker and your code on this site and you can see it working.

    It seems to me, something else is filtering on validation too on your site.

    Regards
    Brajesh

  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #23168
    This reply has been marked as private.
  • Participant
    Level: Initiated
    Posts: 11
    Mocha on #23189

    Brajesh,

    I found out what was filtering the validation as well and fixed it. It works perfectly now!

    Thank you so much for your help!

The topic ‘ [Resolved] WordPress Username Availability Checker + Restrict Usernames Comparability’ is closed to new replies.

This topic is: resolved