Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create different custom messages for Wordpress password protected pages?

I have a Wordpress website with 2 different pages that are protected using the native Wordpress password protect functionality.

I am looking for a way (if one exists) to customize the text above the password field differently for different password-protected pages.

I found a plugin that replaces the default text with custom HTML code, but then this message displays for ALL password protected pages.

What I'm trying to achieve is for password-protected page A to show "Message A" above the password field, and password-protected page B to show "Message B" above the password field, whereas currently all password-protected pages display the same message.

Does anyone know if there is a way to achieve this result? Thanks in advance!

like image 719
lba2134 Avatar asked Oct 31 '25 04:10

lba2134


1 Answers

It is possible.

Yo have to add this code to your functions.php :

add_filter( 'the_password_form', 'custom_password_protected_form' );

function custom_password_protected_form($output) {
    global $post;

    switch ($post->post_name) {
        case 'page-a':
            $replacement_text = 'Message A';
            break;
        case 'page-b':
            $replacement_text = 'Message B';
        case 'default':
            $replacement_text = '';
    }

    if (!empty($replacement_text)) $output = str_replace(__( 'This content is password protected. To view it please enter your password below:' ), $replacement_text, $output);
    return $output;
}

Tested on WP 5.2.2

like image 135
David Rensonnet Avatar answered Nov 02 '25 21:11

David Rensonnet