Skip to content

ねんでぶろぐ

twitter facebook rss
WordPress Fudousan Plugin for Real Estate
  • TopPage >
  • WordPress >
  • WordPress パスワードに 文字数制限 をかける

WordPress パスワードに 文字数制限 をかける

Posted on 2018年5月18日 Update 2018年5月18日 by admin

WordPress のパスワードの文字数は 新規登録時では24文字自動生成していますが ユーザーはたいてい自分用のに変更していると思います。

パスワードリセット時では「ヒント:パスワードは少なくとも7文字以上であるべきです。」と表示していますが 制限がかっていませんので1文字でも登録できてしまいます。
※原文ではは「$hint = __( ‘Hint: The password should be at least twelve characters long. ・・」と12文字以上になってます。

何文字以上がセーフなのかというのは置いておいても1文字でも登録できてしまうのはまずいので パスワードの文字数制限をかけるスクリプトを用意しました。

パスワードを変更できる所は「ユーザー新規登録」「ユーザー編集」「ユーザープロフィール」「パスワードリセット」画面です。
それぞれ jQuery で画面上でチェックして 少ない場合は ボタンを押せなくするようにしました。

 

コード

以下のコードを my-plugin.php に張り付けてみてください。

/*
 * パスワード再発行時に文字数制限を掛ける
 * for wp-login.php
 *
 * License: GPLv2 or later
 */
function nendebcom_wp_login_password_length_check(){

	//パスワード長さ指定
	$pass_length = 7;

	$action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : '';

	if ( isset( $_GET['key'] ) ) {
		$action = 'resetpass';
	}

	if( $action == 'rp' || $action == 'resetpass' ){
		?>
		<script type="text/javascript">

		jQuery(document).ready(function($) {

			$(function(){
				var submit_btn = $('#wp-submit');

				$('#pass1-text').keyup(function() {

					//脆弱確認チェックボックス表示
					pw_weak_class   = $('.pw-weak').attr('style'); 

					//脆弱確認チェックボックス状態
					pw_weak_checkbox = $("[name=pw_weak]").prop("checked");

					//パスワード文字数
					pass_length = $('#pass1-text').val().length; 

					//文字数チェック
					if( pass_length >= <?php echo $pass_length; ?> ){

						if( pw_weak_checkbox == true || pw_weak_class == 'display: none;' ) {
							submit_btn.removeAttr('disabled').val('パスワードをリセット');
						}

						if( pw_weak_checkbox == false && pw_weak_class == 'display: block;' ){
							submit_btn.attr('disabled', 'disabled').val('パスワードをリセット');
						}

					}else{
						submit_btn.attr('disabled', 'disabled').val('<?php echo $pass_length; ?>文字以上入力してください');
					}

				}).keyup();

				$('input[name="pw_weak"]').change(function() {

					//脆弱確認チェックボックス表示
					pw_weak_class   = $('.pw-weak').attr('style'); 
					//脆弱確認チェックボックス状態
					pw_weak_checkbox = $("[name=pw_weak]").prop("checked");

					//パスワード文字数
					pass_length = $('#pass1-text').val().length; 

					//文字数チェック
					if( pass_length >= <?php echo $pass_length; ?> ){

						if( pw_weak_checkbox == true || pw_weak_class == 'display: none;'){
							submit_btn.removeAttr('disabled').val('パスワードをリセット');
						}

						if( pw_weak_checkbox == false && pw_weak_class == 'display: block;' ){
							submit_btn.attr('disabled', 'disabled').val('パスワードをリセット');
						}

					}else{
						submit_btn.attr('disabled', 'disabled').val('<?php echo $pass_length; ?>文字以上入力してください');
					}
				});
			});
		});
		</script>
		<?php
	}
}
add_action('login_footer', 'nendebcom_wp_login_password_length_check');


/**
 * パスワードヒント 変更
 *
 * @since 4.1.0
 * apply_filters( 'password_hint', $hint );
 * @param string $hint The password hint text.
 *
 * License: GPLv2 or later
 */
function nendebcom_password_hint_word( $hint ) {

	//パスワード長さ指定
	$pass_length = 7;

	$hint = 'ヒント: パスワードは' . $pass_length . '文字以上にしてください。より強固にするためには大文字と小文字、数字、 ! " ? $ % ^ & ) のような記号を使いましょう。';
	return $hint;
}
add_filter( 'password_hint', 'nendebcom_password_hint_word' );




/*
 * ユーザー新規・編集 パスワード長さチェック
 * for user-new.php profile.php user-edit.php
 *
 * License: GPLv2 or later
 */
function nendebcom_profile_password_length_check(){

	global $hook_suffix;

	//パスワード長さ指定
	$pass_length = 7;

	if( !$hook_suffix )
		return;

	//新規
	if( $hook_suffix == 'user-new.php' ){
		$submit_btn_id = '#createusersub';
		$submit_btn_txt = '新規ユーザーを追加';
	}

	//プロフィール・ユーザーを編集
	if( $hook_suffix == 'profile.php' || $hook_suffix == 'user-edit.php' ){
		$submit_btn_id = '#submit';
		$submit_btn_txt = 'ユーザーを更新';
	}

	?>
	<script type="text/javascript">

		jQuery(document).ready(function($) {

			$(function(){
				var submit_btn = $('<?php echo $submit_btn_id;?>');

				$('#pass1-text').keyup(function() {

					//脆弱確認チェックボックス表示
					pw_weak_class   = $('.pw-weak').attr('style'); 

					//脆弱確認チェックボックス状態
					pw_weak_checkbox = $("[name=pw_weak]").prop("checked");

					//パスワード設定 アクティブ
					pass_wp_pwd = $('.wp-pwd').attr('style'); 

					//パスワード文字数
					pass_length = $('#pass1-text').val().length; 

					//文字数チェック
					if( pass_length >= <?php echo $pass_length; ?> ){

						if( pw_weak_checkbox == true || pw_weak_class == 'display: none;' ) {
							submit_btn.removeAttr('disabled').val('<?php echo $submit_btn_txt;?>');
						}else{
							submit_btn.val('<?php echo $submit_btn_txt;?>');
						}

						if( pw_weak_checkbox == false && pw_weak_class == 'display: block;' ){
							submit_btn.attr('disabled', 'disabled').val('<?php echo $submit_btn_txt;?>');
						}else{
							submit_btn.val('<?php echo $submit_btn_txt;?>');
						}

					}else{
						if( pass_wp_pwd != 'display: none;' ){
							submit_btn.attr('disabled', 'disabled').val('<?php echo $pass_length; ?>文字以上入力してください');
						}
					}

				}).keyup();

				$('input[name="pw_weak"]').change(function() {

					//脆弱確認チェックボックス表示
					pw_weak_class   = $('.pw-weak').attr('style'); 
					//脆弱確認チェックボックス状態
					pw_weak_checkbox = $("[name=pw_weak]").prop("checked");

					//文字数チェック
					if($('#pass1-text' ).val().length >= <?php echo $pass_length; ?>){

						if( pw_weak_checkbox == true || pw_weak_class == 'display: none;'){
							submit_btn.removeAttr('disabled').val('<?php echo $submit_btn_txt;?>');
						}

						if( pw_weak_checkbox == false && pw_weak_class == 'display: block;' ){
							submit_btn.attr('disabled', 'disabled').val('<?php echo $submit_btn_txt;?>');
						}

					}else{
						submit_btn.attr('disabled', 'disabled').val('<?php echo $pass_length; ?>文字以上入力してください');
					}
				});

			});
		});
	</script>
	<?php
}
add_action( 'admin_footer-user-new.php', 'nendebcom_profile_password_length_check' );
add_action( 'admin_footer-profile.php', 'nendebcom_profile_password_length_check' );
add_action( 'admin_footer-user-edit.php', 'nendebcom_profile_password_length_check' );

※制限文字数を変えたい場合は「//パスワード長さ指定」のところを変更してください。(3か所あります)

 
 

参考

login_footer | Hook | WordPress Developer Resources
https://developer.wordpress.org/reference/hooks/login_footer/

password_hint | Hook | WordPress Developer Resources
https://developer.wordpress.org/reference/hooks/password_hint/

admin_footer-{$hook_suffix} | Hook | WordPress Developer Resources
https://developer.wordpress.org/reference/hooks/admin_footer-hook_suffix/




スポンサードリンク



Posted in WordPressTagged admin_footer-$hook_suffix, login_footer, password_hint

投稿ナビゲーション

← プラグインが 利用中の WordPressバージョン で動作確認済かチェックして警告する
マイ・プラグインを最初に読み込むようにする →

関連記事

WordPress 6.8 をチェックしています

WordPress6.8 RC1 が 公開されました。 WordPress6.8 は 2025年4月15日に リリースされる予定なので いろいろチェックをしています。 その中の WordPress6.8 で気がついたところを取り上 ...

WordPress6.7 をチェックしています

WordPress6.7 RC1 が 公開されました。 WordPress6.7 は 2024年11月12日に リリースされる予定なので いろいろチェックをしています。 その中の WordPress6.7 で気がついたところを取り ...

WordPress 6.6 をチェックしています

WordPress6.6 RC1 が 公開されました。 WordPress6.6 は 2024年7月16日に リリースされる予定なので いろいろチェックをしています。 その中の WordPress6.6 で気がついたところを取 ...

WordPress 6.5 をチェックしています

WordPress6.5 RC1 が 公開されました。 WordPress6.5 は 2024年3月26日に リリースされる予定なので いろいろチェックをしています。 その中の WordPress6.5 で気がついたところを取り上 ...

WordPress6.4 を チェックしています

WordPress6.4 RC1 が 公開されました。 WordPress6.4 は 2023年11月07日に リリースされる予定なので いろいろチェックをしています。 その中の WordPress6.4 で気がついたところを取り ...


スポンサードリンク

カテゴリー

  • WordPress
  • その他
  • セキュリティ
  • テーマ
  • プラグイン

Recent Posts

  • WordPress 6.8 をチェックしています
  • WordPress6.7 新デフォルトテーマ Twenty Twenty-Five をチェックしています
  • WordPress6.7 をチェックしています
  • WordPress 6.6 をチェックしています
  • WordPress 6.5 をチェックしています
  • WordPress6.4 を チェックしています
  • WordPress6.4 新デフォルトテーマ Twenty Twenty-Four をチェックしています
  • WordPress 6.3 を チェックしています

タグクラウド

activated_plugin activate_tinymce_for_media_description add_meta_boxes add_options_page add_theme_support admin_body_class admin_email_check_interval admin_enqueue_scripts admin_footer-$hook_suffix admin_head-$hook_suffix admin_init admin_menu admin_notices admin_print_styles-$hook_suffix after_setup_theme allowed_block_types_all appearance-tools appearanceTools apply_filters apply_shortcodes blockEditor.useSetting.before block_categories_all block_core_navigation_listable_blocks block_core_navigation_render_inner_blocks block_editor_settings_all bulk_actions-{screen_id} bulk_edit_custom_box bulk_edit_posts comment_form_after_fields compare_key Contact Form 7 custom-spacing customize_register customize_save_after date_i18n default-font-sizes default-spacing-sizes deleted_post_{$post->post_type} delete_post_{$post->post_type} delete_user delete_user_form deprecated_function_trigger_error document_title_parts document_title_separator editor_script_handles editor_style_handles edit_user_profile_update Embed embed_head embed_template embed_thumbnail_image_shape embed_thumbnail_image_size emoji enqueue_block_editor_assets fluid Fluid typography Fonts API Footnotes get_category_parents get_currentuserinfo get_file_data get_option get_parent_post get_parent_theme_file_path get_parent_theme_file_uri get_permalink get_query_template get_term_parents_list get_theme_file_path get_theme_file_uri get_the_content get_the_post_thumbnail get_the_title get_users Google APPs Google Fonts Google Maps Gutenberg has_post_parent has_post_thumbnail hooked_block/hooked_block_{$block_type} illegal_user_logins init is_admin_screen is_customize_preview is_post_status_viewable is_post_type_viewable is_singular is_user_logged_in jQuery Lazy-Loading login_footer login_form login_init login_site_html_link Masonry max-image-preview my-plugin new_admin_email_subject paginate_links_output password_hint personal_options personal_options_update phpmailer_init plugins_list plugin_row_meta post_password_expires post_password_required post_search_columns post_thumbnail_id post_thumbnail_url pre_comment_on_post pre_get_document_title pre_months_dropdown_query pre_wp_mail pre_wp_unique_post_slug prime_options prime_options_by_group protected_title_format quick_edit_dropdown_authors_args RECOVERY_MODE_EMAIL registered_post_type_{$post_type} registered_taxonomy_{$taxonomy} register_block_template.unregister_block_template register_form register_post register_sidebar register_{$post_type}_post_type_args register_{$taxonomy}_taxonomy_args registration_errors render_block_core_navigation_link_allowed_post_status render_block_{$this->name} rest_after_insert_attachment safecss_filter_attr sanitize_file_name script_handles script_loader_tag ServerSideRender site_search_columns site_status_autoloaded_options_action_to_perform site_status_autoloaded_options_limit_description site_status_autoloaded_options_size_limit skipBlockSupportAttributes style_handles templateLock template_redirect theme.json the_content the_custom_header_markup the_excerpt_embed the_excerpt_rss the_password_form the_post_navigation Twenty Eleven Twenty Fifteen Twenty Fourteen Twenty Nineteen Twenty Seventeen Twenty Sixteen Twenty Ten Twenty Thirteen Twenty Twelve Twenty Twenty Twenty Twenty-Fiver Twenty Twenty-Four Twenty Twenty-One Twenty Twenty-Three Twenty Twenty-Two TwitterAPI Update URI update_option upgrader_source_selection user_profile_update_errors user_search_columns view_script_handles WordPress4.1 WordPress4.2 WordPress4.3 WordPress4.4 WordPress4.5 WordPress4.6 WordPress4.7 WordPress4.8 WordPress4.9 WordPress5.0 WordPress5.1 WordPress5.2 WordPress5.3 WordPress5.4 WordPress5.5 WordPress5.6 WordPress5.7 WordPress5.8 WordPress5.9 WordPress6.0 WordPress6.1 WordPress6.2 WordPress6.3 WordPress6.4 WordPress6.5 WordPress6.6 WordPress6.7 WordPress6.8 wpcf7_form_tag WP Fastest Cache WP Multibyte Patch wp_add_inline_script wp_admin_canonical_url wp_after_insert_post wp_autoload_values_to_autoload wp_body_open wp_cache_set wp_content_img_tag wp_create_nonce wp_date WP_DEBUG_LOG wp_default_autoload_value wp_dequeue_script_module wp_dequeue_style wp_deregister_script_module wp_deregister_style WP_DEVELOPMENT_MODE wp_editor_set_quality wp_enqueue_script wp_enqueue_scripts wp_enqueue_script_module wp_enqueue_style wp_filesystem wp_footer wp_get_attachment_image wp_get_custom_css wp_get_document_title wp_head WP_HTML_Tag_Processor wp_is_block_template_theme wp_is_block_theme wp_is_rest_endpoint wp_mail wp_max_autoloaded_option_siz wp_omit_loading_attr_threshold wp_plugin_dependencies_slug wp_print_styles wp_register_script wp_register_script_module wp_replace_insecure_home_url wp_resource_hints wp_script_modules wp_set_options_autoload wp_set_option_autoload wp_targeted_link_rel wp_theme_files_cache_ttl wp_title wp_verify_nonce writingMode XAMPP _wp_render_title_tag {$adjacent}_post_link {$type}_template サイトエディタ フルサイト編集(FSE)
はざくみ イラストブログ

Proudly powered by WordPress | Theme: nendebcom by nendeb. | Privacy Policy