We help IT Professionals succeed at work.

Install Password Strength Meter on New Page

OmniUnlimited
on
Medium Priority
1,803 Views
Last Modified: 2012-05-07
I am trying to set up a page on my WordPress site so that a user can change their name, email address and password instead of them accessing their profile in the Dashboard.  I would like to install the password strength meter used on the Profile page.  I have tried copying over the following code from the wp-admin/user-edit.php file, but I don't know enough about WordPress' actions, api's and wp_enqueue_script's to know where I would need to make changes in order for it to work.

Can anyone give me a clue as to what to do?
/**
 * Display JavaScript for profile page.
 *
 * @since 2.5.0
 */
function profile_js ( ) {
?>
<script type="text/javascript">
(function($){
 
	function check_pass_strength () {
 
		var pass = $('#pass1').val();
		var user = $('#user_login').val();
 
		$('#pass-strength-result').removeClass('short bad good strong');
		if ( ! pass ) {
			$('#pass-strength-result').html( pwsL10n.empty );
			return;
		}
 
		var strength = passwordStrength(pass, user);
 
		if ( 2 == strength )
			$('#pass-strength-result').addClass('bad').html( pwsL10n.bad );
		else if ( 3 == strength )
			$('#pass-strength-result').addClass('good').html( pwsL10n.good );
		else if ( 4 == strength )
			$('#pass-strength-result').addClass('strong').html( pwsL10n.strong );
		else
			// this catches 'Too short' and the off chance anything else comes along
			$('#pass-strength-result').addClass('short').html( pwsL10n.short );
 
	}
 
	function update_nickname () {
 
		var nickname = $('#nickname').val();
		var display_nickname = $('#display_nickname').val();
 
		if ( nickname == '' ) {
			$('#display_nickname').remove();
		}
		$('#display_nickname').val(nickname).html(nickname);
 
	}
 
	$(document).ready( function() {
		$('#nickname').blur(update_nickname);
		$('#pass1').val('').keyup( check_pass_strength );
		$('.color-palette').click(function(){$(this).siblings('input[name=admin_color]').attr('checked', 'checked')});
    });
})(jQuery);
</script>
<?php
}
 
if ( $is_profile_page ) {
	add_action('admin_head', 'profile_js');
	wp_enqueue_script('jquery');
	wp_enqueue_script('password-strength-meter');
}

Open in new window

Comment
Watch Question

Commented:
wp_enqueue_script is a new way for you to only include scripts that are needed, when they are needed.

I think the problem is that you are checking IF it is a profile page, then include the scripts (if they are not included already)  Since the page you are using is not a profile page, no scripts.

Try removing that check and just call the enqueue functions

so instead of

if ( $is_profile_page ) {
        add_action('admin_head', 'profile_js');
        wp_enqueue_script('jquery');
        wp_enqueue_script('password-strength-meter');
}

just use

        wp_enqueue_script('jquery');
        wp_enqueue_script('password-strength-meter');

Author

Commented:
Thank you, qwkq, for being my first response!  I was beginning to think I was not going to get any help on this issue.  You are right, in looking over the code I saw several elements that I thought were not necessary for the new page (among them the need to check on the nickname), so I went ahead and modified the code according to what I believe is needed.  Unfortunately, it still does not work.  Attached is the code as I have modified, incorporating into it the changes you suggested.
/**
 * Display JavaScript for profile page.
 *
 * @since 2.5.0
 */
function profile_js ( ) {
?>
<script type="text/javascript">
 
(function($){
 
	function check_pass_strength () {
 
		var pass = $('#pass1').val();
		var user = $('#user_login').val();
 
		$('#pass-strength-result').removeClass('short bad good strong');
		if ( ! pass ) {
			$('#pass-strength-result').html( pwsL10n.empty );
			return;
		}
 
		var strength = passwordStrength(pass, user);
 
		if ( 2 == strength )
			$('#pass-strength-result').addClass('bad').html( pwsL10n.bad );
		else if ( 3 == strength )
			$('#pass-strength-result').addClass('good').html( pwsL10n.good );
		else if ( 4 == strength )
			$('#pass-strength-result').addClass('strong').html( pwsL10n.strong );
		else
			// this catches 'Too short' and the off chance anything else comes along
			$('#pass-strength-result').addClass('short').html( pwsL10n.short );
 
	}
 
	$(document).ready( function() {
		$('#pass1').val('').keyup( check_pass_strength );
    });
})(jQuery);
</script>
 
<?php
}
wp_enqueue_script('jquery');
wp_enqueue_script('password-strength-meter');

Open in new window

Commented:
Sorry, try getting rid of the opening

function profile_js ( ) {
?>

and the ending

<?php
}

Now that you removed add_action('admin_head', 'profile_js'); there is nothing calling the profile_js function, so none of the code inside of that function is being executed.

Author

Commented:
Sorry, it wouldn't run without the ending and starting php encoding (<?, <?php).  I tried it just eliminating the function declaration and end brace, but it still does not work.

Commented:
Your best bet is probably to just find a separate password strength meter and incorporating into your page.

http://www.webresourcesdepot.com/10-password-strength-meter-scripts-for-a-better-registration-interface/

Commented:
The reason I say that is because there are too many other variables and other files involved that could be causing this not to work.
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
I just found out that the two wp_enqueue_script's are not necessary either.

Commented:
Very good.  Thank you for posting your solution... I'm sure this will be useful to others as well.

Author

Commented:
Thank you gwkg.  I hope it will be useful to someone as well.  I look forward to your help on future problems.  Do you know anything about the php pdf library, tcpdf?
Jon JaquesInformation Technologist

Commented:
Hello, Try the "Register Plus" plugin, it has the code already in it to do this for you, and you don't even have to modify the WordPress code.

Author

Commented:
Thanks for your input, CoastalData.  As a matter of fact, my site already uses the "Register Plus" plugin, and I had already gone over the code before I posted my question.   I chose to use the code from the profile page because the strength meter is located on a page that for me is easier to modify ("Register Plus" loads the password strength meter onto a page based in wp-login.php and for me was not as elegant a code.)  But I'm sure that for others that may wish to do the same as I have done, "Register Plus" is definitely a viable option.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.