Install Password Strength Meter on New Page

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

LVL 17
OmniUnlimitedAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gwkgCommented:
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');

0
OmniUnlimitedAuthor 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

0
gwkgCommented:
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.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

OmniUnlimitedAuthor 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.
0
gwkgCommented:
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/

0
gwkgCommented:
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.
0
OmniUnlimitedAuthor Commented:
Well, qwkq.  I am so sorry you were unable to come up with a solution.  I do appreciate your willingness to help.  The good news is that I was able to come up with a working solution.  I found out that admin functions are just too separate from the main website for me to be able to realistically use them, and also the syntax of the jQuery function was not one I recognized (being somewhat of a newbe at jQuery), so I changed the syntax of the function to one I did recognize, transferred the passwordStrength function over to my user.js file, and added definitions for the pwsL10n variable, and viola!  It works!

Below you can see the updated function:
wp_enqueue_script('jquery');
wp_enqueue_script('password-strength-meter');
?>
<script type="text/javascript">
     jQuery(document).ready( function () {
 
         jQuery('#pass1').val('').keyup(function(){	
 
	var pass = jQuery('#pass1').val();
	var user = jQuery('#user_login').val();
	var pwsL10n = {
		empty: "Strength indicator",
		short: "Very weak",
		bad: "Weak",
		good: "Medium",
		strong: "Strong"
	}
 
	jQuery('#pass-strength-result').removeClass('short bad good strong');
	if ( ! pass ) {
	         jQuery('#pass-strength-result').html( pwsL10n.empty );
	         return;
	}
 
	var strength = passwordStrength(pass, user);
 
	if ( 2 == strength )
          	jQuery('#pass-strength-result').addClass('bad').html( pwsL10n.bad );
	else if ( 3 == strength )
		jQuery('#pass-strength-result').addClass('good').html( pwsL10n.good );
	else if ( 4 == strength )
		jQuery('#pass-strength-result').addClass('strong').html( pwsL10n.strong );
	else
		// this catches 'Too short' and the off chance anything else comes along
		jQuery('#pass-strength-result').addClass('short').html( pwsL10n.short );
 
           });
 
     });
</script>
<?php
 
// In the page I had to add the following coding:
 
global $current_user;
get_currentuserinfo();
$username = ucwords($current_user->display_name);
?>
<input type="hidden" name="user_login" id="user_login" value="<?php echo $username ?>" />
 
<!-- And of course the following: -->
<input type="password" name="pass1" id="pass1" size="25" />
<input type="password" name="pass2" id="pass2" size="25" />
<div id="pass-strength-result"><?php _e('Strength indicator'); ?></div>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OmniUnlimitedAuthor Commented:
I just found out that the two wp_enqueue_script's are not necessary either.
0
gwkgCommented:
Very good.  Thank you for posting your solution... I'm sure this will be useful to others as well.
0
OmniUnlimitedAuthor 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?
0
Jon JaquesInformation TechnologistCommented:
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.
0
OmniUnlimitedAuthor 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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WordPress

From novice to tech pro — start learning today.