Wordpress username....

Status
Not open for further replies.

CloudShadow

Active Member
2,292
2010
198
0
Hi,

Can someone please tell me that how can i limit the characters in the username of newly registered users ( on wordpress). Currently people can make as long of a username as they want and these are not going with my theme and things are looking messy.

So if someone can tell me a little tweak or maybe a plugin, i'll be grateful.
 
9 comments
Changed the line in function validate_username( $username ) in wp-includes/registration.php, around line 25 for WP 2.2
from:
if ( $name != $username )
to:
if (($name != $username) || (strlen($username) > 10))
It works.
 
First off lets look at the straightforward HTML file. We set up a form to hold our input fields, only the 'textarea' is used in this example.
Here is the Html File which has the form we will use.

PHP:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
	"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<title>maxchars</title>
	<script type="text/javascript" src="maxchars.js"></script>
	<link type="text/css" rel="stylesheet" href="maxchars.css">
</head>
<body>
	<div id="wrapper">
		<form id="myform" action="submit.php">
			<p><label for="name">Name</label>
				<input type="text" name="name" id="name"></p>

			<p><label for="email">Email</label>
				<input type="text" name="email" id="email"></p>

			<p><label for="msg">Message</label>
				<textarea name="message" id="message" cols="50" 
					rows="8" maxlength="50"></textarea>
				<span id="limiter"></span></p>
		</form>
	</div>

</body>
</html>

The 'maxlength' attribute is deprecated, but for this example we will use it. :)
That is all there is to the HTML, next we begin working with the Javascript.
For this example we will 'wrap' the entire script into a master object, this is done to keep all variable self-contained and will not interfere with other scripts on the page.
Here is the Javacript in full for all those copy & paste people.


PHP:
var maxChars = {
        // cross-browser event handling for IE5+, NS6 and Mozilla 
        // By Scott Andrew 
	addEvent: function(elm, evType, fn, useCapture) {
		if(elm.addEventListener) {
			elm.addEventListener(evType, fn, useCapture);
			return true;
		} else if(elm.attachEvent) {
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		} else {
			elm['on' + evType] = fn;
		}
	},

	attVal: function(element, attName) {
	  return parseInt(element.getAttribute(attName));
	},

	init: function() {
		if(!document.getElementsByTagName || !document.getElementById) {
			return;
		}
		
		maxChars.form = document.getElementById('myform');
		maxChars.textarea = document.getElementById('message');
		maxChars.maxlength = maxChars.attVal(maxChars.textarea, 'maxlength');
		maxChars.limit_span = document.getElementById('limiter');
		maxChars.limit_span.innerHTML = '<strong>' + maxChars.maxlength + '</strong>' 
			+ ' characters remaining.';
		
		maxChars.addEvent(maxChars.textarea, 'keyup', maxChars.countlimit, false);
	},

	countlimit: function(e) {
		var placeholder;
		var lengthleft = maxChars.maxlength - maxChars.textarea.value.length;

		if(e && e.target) {
			placeholder = e.target;
		}

		if(window.event && window.event.srcElement) {
			placeholder = window.event.srcElement;
		}

		if(!placeholder) {
			return;
		} else if(lengthleft < 0) {
			maxChars.textarea.value = maxChars.textarea.value
				.substring(0, maxChars.maxlength);
		} else if(lengthleft > 1) {
			maxChars.limit_span.innerHTML = '<strong>' + lengthleft + '</strong>' 
				+ ' characters remaining.';
		} else {
			maxChars.limit_span.innerHTML = '<strong>' + lengthleft + '</strong>' 
				+ ' character remaining.';
		}
	}

}

maxChars.addEvent(window, 'load', maxChars.init, false);

Now lets run through the code to get an understanding of how it works.
This is used to define the object and get things started


PHP:
var maxChars = {

The initial function called when the page loads. Check to see if 'DOM' elements are available and if they aren't exit the script. We then grab the id of the form, textarea and span tag that display the remaining characters. The last step in this function is to set up a key listener event.

PHP:
init: function() {
	if(!document.getElementsByTagName || !document.getElementById) {
		return;
	}
	
	maxChars.form = document.getElementById('myform');
	maxChars.textarea = document.getElementById('message');
	maxChars.maxlength = maxChars.attVal(maxChars.textarea, 'maxlength');
	maxChars.limit_span = document.getElementById('limiter');
	maxChars.limit_span.innerHTML = '<strong>' + maxChars.maxlength + '</strong>' 
		+ ' characters remaining.';
	
	maxChars.addEvent(maxChars.textarea, 'keyup', maxChars.countlimit, false);
},

For multiple browser support we will use a function to assign event listeners. IE uses on'eventname' where standards browsers use 'eventname'.

PHP:
// cross-browser event handling for IE5+, NS6 and Mozilla 
// By Scott Andrew 
addEvent: function(elm, evType, fn, useCapture) {
	if(elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if(elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	} else {
		elm['on' + evType] = fn;
	}
},

This function will be used to get our 'deprecated' element later on in the script. It simply returns the value of the passed in element.


PHP:
attVal: function(element, attName) {
  return parseInt(element.getAttribute(attName));
},

Our last function in this script is to count and return the remaining characters.


PHP:
countlimit: function(e) {
	var placeholder;
	var lengthleft = maxChars.maxlength - maxChars.textarea.value.length;

Check to see if the element is available. We use the tertiary condition to simply check for the event in a cross-broswer manner.


PHP:
	if(e && e.target) {
		placeholder = e.target;
	}

	if(window.event && window.event.srcElement) {
		placeholder = window.event.srcElement;
	}

Check to see if an event exists and if it does continue on with the rest of the script. If the remaining length is less than 0(zero) change the string to be grammatically correct


PHP:
	if(!placeholder) {
		return;
	} else if(lengthleft < 0) {
		maxChars.textarea.value = maxChars.textarea.value
			.substring(0, maxChars.maxlength);
	} else if(lengthleft > 1) {
		maxChars.limit_span.innerHTML = '<strong>' + lengthleft 
			+ '</strong>' + ' characters remaining.';
	} else {
		maxChars.limit_span.innerHTML = '<strong>' + lengthleft 
			+ '</strong>' + ' character remaining.';
	}
}

Finally end the object


PHP:
}

thanks
 
or you can follow this simple method too

-------------------------------------------------------------------------

I’ve been working on a pretty complex project with WordPress MultiUser (soon to be MultiSite). This client needs several sites with hundreds of users divided into each site. I will be integrating the backend authentication with LDAP and discovered that a small percentage of their users have usernames with fewer than four characters.

WordPress MU currently has a minimum limit of four characters set in its core. Unfortunately, this limit is still imposed in WordPress MS 3.0. The limit is probably there because usernames were used for the domain too and WP-Devs didn’t want to conflict with country codes. But that is not an issue for my client, so I wanted to kill the limit (without touching core).


Basically, I wrote a quick mu-plugin that unset the error message when someone tries to add a user with fewer than four characters. Doing this removes any halts that would stop processing the new user. Here is my code:

PHP:
function remove_username_char_limit($result) {
  if ( is_wp_error( $result[ 'errors' ] ) && !empty( $result[ 'errors' ]->errors ) ) {

    // Get all the error messages from $result
    $messages = $result['errors']->get_error_messages();
    $i = 0;
    foreach ( $messages as $message ) {

      // Check if any message is the char limit message
      if ( 0 == strcasecmp("Username must be at least 4 characters", $message)) {
        // Unset whole 'user_name' error array if only 1 message exists
        // and that message is the char limit error
        if ( 1 == count($messages) ) {
          unset( $result['errors']->errors['user_name'] );
        } else {
          // Otherwise just unset the char limit message
          unset( $result['errors']->errors['user_name'][$i] );
        }
      }	

      $i++;
    }
  }

  return $result;
}
add_action('wpmu_validate_user_signup', 'remove_username_char_limit');

enjoy :)
 
@everyone

I have already tried all the methods listed above( apart from CyberAff's). The one he posted is way too complex for such a simple thing, i'd rather not mess with so many things.


EDIT:

@Cyber

The last method you posted is i think for removing the minimum character limit, rather than placing a maximum character one.
 
@everyone

I have already tried all the methods listed above( apart from CyberAff's). The one he posted is way too complex for such a simple thing, i'd rather not mess with so many things.

yes i know thats why i posted 2nd method see it and try to do it

it will work :)

thanks
 
Status
Not open for further replies.
Back
Top