Question

How do I provide two different personalized messages for two different password-protected pages? I'm able to personalize the default message using the code below, but I'm not sure how to add a second unique message for visitors to a second password-protected page. I assume I would add some conditional code in my functions.php file but my attempts have failed. Any ideas?

// to functions.php: change password message for protected page/s

function change_pw_text($content) {
$content = str_replace(
'This post is password protected. To view it please enter your password below:',
'Hint: Tell our system to show you, and it will.',
$content);
return $content;
}
add_filter('the_content','change_pw_text');
Was it helpful?

Solution

You should probably add in more checks, but this should get you started.

function change_pw_text($content) {

    // Just to save processing other pages.
    if ( !is_page('page-slug-one') && !is_page('page-slug-two') )
        return $content;

    $find = 'This post is password protected. To view it please enter your password below:';

    if ( is_page('page-slug-one') )
        $replace = 'Hint: Tell our system to show you, and it will.';

    if ( is_page('page-slug-two') )
        $replace = 'Sorry: This page requires you to be logged in.';

    $content = str_replace( $find, $replace, $content );

    return $content;
}

add_filter('the_content','change_pw_text');

For a nice resource on the possible Conditional Tags in WordPress see... http://codex.wordpress.org/Conditional_Tags

OTHER TIPS

This feature was recently added to the Change Password Protected Message.


The following was taken from the plugin author's support page:

How to change the "Password Protected" message on WordPress

WordPress allows you to block access to any post/page unless the reader has a password. One example of this might be a school which allows access to certain students in a class, or a writer which releases content to patrons.

It is possible to add password protection to any post/page by using the "visibility" settings on the right side of the editing screen. Simply select "Password protected", then enter the password which people can use to access it.

enter image description here

When a post/page is password protected, it will show the something like the following message:

"This content is password protected. Please enter a password to view."

enter image description here

How to change the password protected message

If you would like to change the text, you can install the Change Password Protected Message plugin. This allows you to change the message for all password protected posts/pages, or change the message for a specific post/page alone.

Change the message for all posts/pages

  1. Make sure the Change Password Protected Message plugin is installed and activated.

  2. Go to the Settings > Reading page

enter image description here

  1. You can now set the message via the option shown below:

enter image description here

Change the message for a specific post/page

To override a the text displayed on a specific post/page, we can use a custom field. Follow the steps below:

  1. Make sure the Change Password Protected Message plugin is installed and activated.

  2. Go to edit the post/page.

The next steps will depend on if you are using the new WordPress Block/Gutenberg editor or Classic Editor. Select an option below:

The next steps will depend on if you are using the new WordPress Block/Gutenberg editor or Classic Editor. Select an option below:

WordPress 5+ with the new Editor

  1. In the editing screen, click the 3 dots at the top right of the screen.

enter image description here

  1. Then select the "Preferences" option from the bottom.

enter image description here

  1. A panel should appear. Scroll to the bottom and enable "Custom Fields".

enter image description here

  1. After clicking the button to enable, you should see a new "Custom Fields" section near the bottom of the page. We will now add a new custom field called "override_password_text" which will contain the custom message.

Set the Name to "override_password_text". Then set the Value to any text/html you'd like to display. Then click the "Add Custom Field" button.

enter image description here

Set the Name to "override_password_text". Then set the Value to any text/html you'd like to display. Then click the "Add Custom Field" button.

enter image description here

After saving/publishing the page, the text will now be displayed with your custom message.

WordPress Classic Editor

  1. In the editing screen, open the "Screen Options" tab at the top right of the screen:

enter image description here

  1. Make sure "Custom Fields" is enabled:

enter image description here

  1. Scroll to the "Custom Fields" box, near the bottom of the page. We will now add a new custom field called "override_password_text" which will contain the custom message.

Set the Name to "override_password_text". Then set the Value to any text/html you'd like to display. Then click the "Add Custom Field" button.

enter image description here

After saving/publishing the page, the text will now be displayed with your custom message.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top