From bbcfca198e28ea021446dbfae84c75752ad37680 Mon Sep 17 00:00:00 2001 From: David Cavins Date: Mon, 3 Jan 2022 14:15:48 -0600 Subject: [PATCH] Add customizable message above password form. * Add an input so that site admins can easily add a custom message above the password entry form. * Output the custom message on the `password_protected_before_login_form` hook. --- admin/admin.php | 27 +++++++++++++++++++++++++++ password-protected.php | 17 +++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/admin/admin.php b/admin/admin.php index cbb971a..e2df4e5 100644 --- a/admin/admin.php +++ b/admin/admin.php @@ -163,6 +163,14 @@ public function password_protected_settings() { 'password_protected' ); + add_settings_field( + 'password_protected_message', + __( 'Message to display above the password form', 'password-protected' ), + array( $this, 'password_protected_message_field' ), + $this->options_group, + 'password_protected' + ); + register_setting( $this->options_group, 'password_protected_status', 'intval' ); register_setting( $this->options_group, 'password_protected_feeds', 'intval' ); register_setting( $this->options_group, 'password_protected_rest', 'intval' ); @@ -172,6 +180,7 @@ public function password_protected_settings() { register_setting( $this->options_group, 'password_protected_allowed_ip_addresses', array( $this, 'sanitize_ip_addresses' ) ); register_setting( $this->options_group, 'password_protected_remember_me', 'boolval' ); register_setting( $this->options_group, 'password_protected_remember_me_lifetime', 'intval' ); + register_setting( $this->options_group, 'password_protected_message', 'wp_filter_post_kses' ); } @@ -311,6 +320,24 @@ public function password_protected_remember_me_lifetime_field() { } + /** + * Output the form element to collect the message input. + * + * @since 3.0 + * + * @return string HTML to display. + */ + public function password_protected_message_field() { + + $settings = apply_filters( 'password_protected_message_editor_settings', array( + 'textarea_rows' => 5, + ) ); + $saved_option = wp_unslash( get_option( 'password_protected_message' ) ); + + wp_editor( $saved_option, 'password_protected_message', $settings ); + + } + /** * Pre-update 'password_protected_password' Option * diff --git a/password-protected.php b/password-protected.php index 659c489..0ce0966 100644 --- a/password-protected.php +++ b/password-protected.php @@ -70,6 +70,7 @@ public function __construct() { add_filter( 'rest_authentication_errors', array( $this, 'only_allow_logged_in_rest_access' ) ); add_action( 'init', array( $this, 'compat' ) ); add_action( 'password_protected_login_messages', array( $this, 'login_messages' ) ); + add_action( 'password_protected_before_login_form', array( $this, 'display_custom_message' ) ); add_action( 'login_enqueue_scripts', array( $this, 'load_theme_stylesheet' ), 5 ); // Available from WordPress 4.3+ @@ -742,6 +743,22 @@ public function login_messages() { } + /** + * Add a message above the "password protected" form, if set. + * + * @since 3.0 + * + * @return string HTML to display. + */ + function display_custom_message() { + + $message = get_option( 'password_protected_message' ); + if ( $message ) { + echo apply_filters( 'the_content', wp_unslash( $message ) ); + } + + } + /** * Load Theme Stylesheet *