admin管理员组文章数量:1304100
Per these instructions I've cooked up a custom password protected page for some of the pages on my site:
<?php if ( post_password_required() ) {
add_filter( 'the_password_form', 'custom_password_form' );
function custom_password_form() {
global $post;
$label = 'pwbox-'.( empty( $post->ID ) ? rand() : $post->ID );
$o = '<form class="grey-form protected-post-form" action="' . get_option('siteurl') . '/wp-login.php?action=postpass" method="post">
' . __( "<P>
PRIVATE. PLEASE USE THE YOU WERE GIVEN BLAH BLAH" ) . '
<label class="pass-label" for="' . $label . '">' . __( "" ) . ' </label><input onChange="javascript:this.value=this.value.toLowerCase();" name="post_password" id="' . $label . '" type="password" style="background: #ffffff; border:1px solid #999; color:#333333; padding:10px;" size="20" /><input type="submit" name="Submit" class="button" value="' . esc_attr__( "Submit" ) . '" />
</form>
<div style="height:700px"/>
';
return $o;
}
echo get_the_password_form();
This is fine except if you test it with a wrong password it just reshows the form. No feedback to the user that he's put in a wrong password.
How can I feed back to my user that he's put in a wrong password?
Per these instructions I've cooked up a custom password protected page for some of the pages on my site:
<?php if ( post_password_required() ) {
add_filter( 'the_password_form', 'custom_password_form' );
function custom_password_form() {
global $post;
$label = 'pwbox-'.( empty( $post->ID ) ? rand() : $post->ID );
$o = '<form class="grey-form protected-post-form" action="' . get_option('siteurl') . '/wp-login.php?action=postpass" method="post">
' . __( "<P>
PRIVATE. PLEASE USE THE YOU WERE GIVEN BLAH BLAH" ) . '
<label class="pass-label" for="' . $label . '">' . __( "" ) . ' </label><input onChange="javascript:this.value=this.value.toLowerCase();" name="post_password" id="' . $label . '" type="password" style="background: #ffffff; border:1px solid #999; color:#333333; padding:10px;" size="20" /><input type="submit" name="Submit" class="button" value="' . esc_attr__( "Submit" ) . '" />
</form>
<div style="height:700px"/>
';
return $o;
}
echo get_the_password_form();
This is fine except if you test it with a wrong password it just reshows the form. No feedback to the user that he's put in a wrong password.
How can I feed back to my user that he's put in a wrong password?
Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Feb 15, 2015 at 10:42 hawbslhawbsl 5101 gold badge11 silver badges31 bronze badges1 Answer
Reset to default 0This worked for me adding this code in functions.php:
add_filter( 'the_password_form', 'wpse_71284_custom_post_password_msg' );
/**
Add a message to the password form.
@wp-hook the_password_form
@param string $form
@return string */ function wpse_71284_custom_post_password_msg( $form ) { // No cookie, the user has not sent anything until now. if ( ! isset ( $COOKIE[ 'wp-postpass' . COOKIEHASH ] ) ) return $form;
// Translate and escape. $msg = esc_html__( 'La contraseña es incorrecta o ya ha sido utilizada.', 'your_text_domain' );
// We have a cookie, but it doesn’t match the password. $msg = "$msg
";return $msg . $form; }
add_action('init', 'myStartSession', 1); add_action('wp_logout', 'myEndSession'); add_action('wp_login', 'myEndSession'); function myStartSession() { if(!session_id()) { session_start(); } } function myEndSession() { session_destroy (); }
if ( post_password_required() ) { $session_id = 'wp-postpass_' . get_the_ID(); //onload $current_cookie = wp_unslash($COOKIE[ 'wp-postpass' . COOKIEHASH ]); //get old cookie $old_cookie = isset( $_SESSION[ $session_id ] ) ? $_SESSION[ $session_id ] : ''; //set new session $_SESSION[ $session_id ] = $current_cookie; if ( $current_cookie != $old_cookie && !empty( $old_cookie ) ){ error_notification('Error! Authentication failed!'); } } ?>
本文标签: phpStyling my own password protected pagehow to deal with wrong password
版权声明:本文标题:php - Styling my own password protected page, how to deal with wrong password? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741780740a2397293.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论