Created
December 21, 2021 08:50
-
-
Save dannyfoo/d5f1e91aed20f54fafb853b0f647bd11 to your computer and use it in GitHub Desktop.
Customizations for default WordPress password protected pages / posts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Add a custom class to the body for password posts / pages only | |
function add_password_protected_body_class( $classes ) { | |
if ( post_password_required() ) | |
$classes[] = 'wp-password-page'; | |
return $classes; | |
} | |
add_filter( 'body_class', 'add_password_protected_body_class' ); | |
// Reformat the password message and form fields | |
function my_custom_password_form() { | |
global $post; | |
// Custom logic for the message | |
$password_form_message = | |
__( '<p>This content is password protected. Enter password below to view it:</p>' ); | |
// Put together the custom form using the dynamic message | |
$label = 'pwbox-'.( empty( $post->ID ) ? rand() : $post->ID ); | |
$form = '<div class="container"><form class="post-password-form" action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" method="post">' . $password_form_message . '<label id="password-label" for="' . $label . '">' . __( "Password:" ) . ' </label><input name="post_password" id="' . $label . '" class="pw-window" type="password" placeholder="Password" size="20" /><input type="submit" class="btn" name="Submit" value="' . esc_attr__( "Submit" ) . '" /></form></div>'; | |
return $form; | |
} | |
add_filter( 'the_password_form', 'my_custom_password_form' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment