Skip to:
Content
Pages
Categories
Search
Top
Bottom

Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • @saulcoz

    Participant

    damn it – badly formated the code – try this:

    <?php
    /*
    Plugin Name: XProfile to username
    Plugin URI: http://
    Description: Created for a specific project, this plugin copies a field from the user's Xprofile to their username, ensuring first that it is unique by adding a  digit to te end
    Version: 0.1
    Author: Saul Cozens
    Author URI: http://saulcozens.co.uk/wordpress
    
    */
    
    function xprofile_to_username($xp) {
    	global $wpdb;
    
    	$field_id = BP_XProfile_Field::get_id_from_name('Name/Company Name');
    	if(!$field_id || $xp==null) {
    		return;
    	}
    	if($field_id == $xp->field_id) { // cover me, I'm going in
    		$new_username=strtolower(preg_replace('/[^\w\d]/','-', $xp->value));
    		$existing_user_by_that_username=get_user_by('login', $new_username);
    		if($existing_user_by_that_username && ($existing_user_by_that_username->ID !== $xp->user_id )) { // another user already has that login
    			$i=1;
    			while(get_user_by('login', $new_username."-$i")) {
    				$i+=1;
    			}
    			$new_username.="-$i";
    		}
    		$wpdb->update($wpdb->users, array('user_login' => $new_username), array('ID' => $xp->user_id));
    		return;
    	}	
    }
    add_action('xprofile_data_after_save','xprofile_to_username');
    
    ?>
    

    @saulcoz

    Participant

    Well I have a plugin – except that I can’t be arsed to release it as a plugin at the moment so I’ll just cut&paste the code here.

    2 things:
    1. it doesn’t replace the wp-email-login plugin or the login for hack above – it adds to them by simply forcing the username to be the same a field pulled from the Xprofile.
    2. It’s hardwired to a specific field name in @applegateian’s project. You’ll need to edit the code to use your own field name because, like I say, I haven’t got time to turn it into a proper plugin. When I find the enthusiasm to do it properly I’ll create a plugin settings menu and stuff.

    <?php
    

    /*

    Plugin Name: XProfile to username
    

    Plugin URI: http://
    Description: Created for a specific project, this plugin copies a field from the user's Xprofile totheir username, ensuring first that it is unique by adding a digit to te end

    Version: 0.1
    

    Author: Saul Cozens

    Author URI: http://saulcozens.co.uk/wordpress
    
    */
    
    function xprofile_to_username($xp) {
    

    global $wpdb;

    
    

    $field_id = BP_XProfile_Field::get_id_from_name(‘Name/Company Name’);

    	if(!$field_id || $xp==null) {
    

    return;

    	}
    

    if($field_id == $xp->field_id) { // cover me, I’m going in

    		$new_username=strtolower(preg_replace('/[^\w\d]/','-', $xp->value));
    

    $existing_user_by_that_username=get_user_by(‘login’, $new_username);

    		if($existing_user_by_that_username && ($existing_user_by_that_username->ID !== $xp->user_id )) { // another user already has that login
    

    $i=1;

    			while(get_user_by('login', $new_username."-$i")) {
    

    $i+=1;

    			}
    

    $new_username.=”-$i”;

    		}
    

    $wpdb->update($wpdb->users, array(‘user_login’ => $new_username), array(‘ID’ => $xp->user_id));

    		return;
    

    }

    }
    

    add_action(‘xprofile_data_after_save’,’xprofile_to_username’);

    
    

    ?>

    Just put this is a file called xprofile-to-username.php in a folder called xprofile-to-username, activate it and Bob’s your uncle. Every time the user’s profile gets updated, their username will mirror the xprofile field (with hyphen instead of funny characters and numbers to remove any duplicates).

    Enjoy.

Viewing 2 replies - 1 through 2 (of 2 total)
Skip to toolbar