WordPress.org

Ready to get started?Download WordPress

Forums

Allow only specific usernames (6 posts)

  1. JeremyPark123999
    Member
    Posted 1 year ago #

    Hi, I am trying to set up a website for my minecraft server. I want to set up the buddypress registration to only allow users to register if their username reads as a premium account on the minecraft.net server. I found a plugin that was meant to do that, but it hasn't been updated for a long time, and it no longer works. I have been trying to alter the plugin, but so far, it hasn't shown any results. As far as I can tell, the plugin is not blocking users, and that is all. Could someone tell me if I have done something wrong?
    I have changed the code a lot from the original, but I don't think that I have damaged anything, let me know if you need the original, and I will post it.

    EDIT: It turns out that I did damage some of the code, I was getting a very small error on the wordpress plugin page, I fixed that, and upaded the code below.

    Here is the plugin's code:

    <?php
    /*
       Plugin Name: Minecraft Validator
       Description: Simple plugin to verify new WordPress accounts against the Minecraft user database. If the username doesn't show up as a valid Minecraft player, it won't let them register.
       Version: 1.4
       Author: Ghost1227
       Author URI: http://www.ghost1227.com
    */
    
    /* Run activation hook when plugin is activated */
    register_activation_hook(__FILE__, 'get_wp_version');
    
    /* Rewrite registration form */
    function mcv_registration_form() {
    	wp_enqueue_script( 'login_form', plugins_url() . '/minecraft-validator/usernamerewrite.js', array('jquery'), false, false );
    }
    add_action('login_head', 'mcv_registration_form');
    
    /* Get WordPress version */
    function get_wp_version() {
        global $wp_version;
        if ( version_compare ( $wp_version, '3.1', '<')) {
            exit ( "<div style='font-size: 13px; font-family: 'HelveticaNeue-Light', 'Helvetica Neue Light', 'Helvetica Neue', sans-serif;'><strong>Attention:</strong> This plugin will not work with your version of WordPress.</div>" );
        }
    }
    
    /* Register actions */
    add_action('register_post', 'verify_mc_account', 10, 3);
    add_action('admin_menu', 'add_mcval_options');
    
    /* Check account on minecraft.net */
    function verify_mc_account($login, $email, $errors) {
    	$user_info = get_userdata(1);
    	$options = array(
            'timeout' => 5,
        );
        $mcacct = wp_remote_get('http://www.minecraft.net/haspaid.jsp?user='.$user_info->user_login);
    
        if ( $mcacct == 'false' ) {
            if ( $mcacct == 'false' ) {
                $errors->add('mc_error',__('<strong>ERROR:</strong> Minecraft account is invalid.'));
                return $errors;
            } else {
                $errors->add('mc_error',__('<strong>ERROR:</strong> Unable to contact minecraft.net.'));
                return $errors;
            }
            add_filter('registration_errors', 'verify_mc_account', 10, 3);
        }
    }
    /* Activation/Deactivation */
    function set_mcval_options() {
        add_option('hide_me', 'false');
    }
    
    function unset_mcval_options() {
        delete_option('hide_me');
    }
    
    register_activation_hook(__FILE__, 'set_mcval_options');
    register_deactivation_hook(__FILE__, 'unset_mcval_options');
    
    /* Add admin menu */
    function add_mcval_options() {
        if ( get_option('hide_me') != "true" ) {
            add_options_page('Minecraft Validator Options', 'Minecraft Validator', 8, 'mcval-options', 'mcval_options');
        }
    }
    
    /* Display options page */
    function mcval_options() {
    
        ?>
    
        <div class="wrap">
            <h2>Minecraft Validator</h2>
    
        <?php
            if ( $_REQUEST['submit'] ) {
                update_mcval_options();
                ?>
                    <script type="text/javascript">
                    <!--
                        window.location = <?php echo "'options-general.php'"; ?>
                    //-->
                    </script>
                <?php
            }
            print_mcval_form();
        ?>
    
        </div>
    
    <?php }
    
    function update_mcval_options() {
        update_option( 'hide_me', 'true' );
    }
    
    function print_mcval_form() {
        ?>
    
    <?php }
  2. bcworkz
    Member
    Posted 1 year ago #

    If the account check fails, the 'registration_errors' hook is never added so the registration is allowed to continue. This strikes me as a convoluted way to do things. First of all, the same function is added to two hooks that have different parameters, so it cannot work right anyway. Why not hook 'verify_mc_account' to 'registration_errors' straight away and not bother with 'register_post'? Then the verification checks the account and adds the error on failure, otherwise all is good. Simple and straight forward.

  3. JeremyPark123999
    Member
    Posted 1 year ago #

    I am not entirely sure what you mean, as I am still rather new to PHP code, would you be able to update that code, and post it for me?

  4. bcworkz
    Member
    Posted 1 year ago #

    This segment is the only part modified. It should work for you, but I have no easy way to test this myself.

    /* Register actions */
    add_action('registration_errors', 'verify_mc_account', 10, 3);
    add_action('admin_menu', 'add_mcval_options');
    
    /* Check account on minecraft.net */
    function verify_mc_account( $errors, $login, $email ) {
    	//$user_info = get_userdata(1);
    	$user_info = get_user_by('login', $login );
    	$options = array(
            'timeout' => 5,
        );
        $mcacct = wp_remote_get('http://www.minecraft.net/haspaid.jsp?user='.$user_info->user_login);
    
        if ( $mcacct == 'false' ) {
            if ( $mcacct == 'false' ) {
                $errors->add('mc_error',__('<strong>ERROR:</strong> Minecraft account is invalid.'));
            } else {
                $errors->add('mc_error',__('<strong>ERROR:</strong> Unable to contact minecraft.net.'));
            }
        }
        return $errors;
    }
    /* Activation/Deactivation */
    function set_mcval_options() {
        add_option('hide_me', 'false');
    }

    I'm confused by the line $user_info = get_userdata(1);. I commented this line out and replaced it with what seemed more appropriate. Change it back if it is what you really need.

    get_userdata(1) returns the user object for user ID 1 regardless of which user is logged in. Thus you are checking account status for user ID 1 for all users, not checking their own account status.

    You don't really need to get the user object anyway, you only need the user login, which is passed in $login. You could remove all $user_info related code and just supply $login for the minecraft user URL parameter.

  5. JeremyPark123999
    Member
    Posted 1 year ago #

    Sorry for the late reply, my website was not allowed access to external sites, so I couldn't test this. Unfortunately it didn't work, so maybe I will just figure it out as I continue to learn PHP. Thanks for the help!

  6. Azinfiro
    Member
    Posted 10 months ago #

    Hi, did you figure this out?

    Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic