0

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');
0

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

  • Thanks, @BA_Webimax. Unfortunately, that isn't working — the default WP text for a password-protected page remains for both pages. – jimw Aug 18 '14 at 18:13
  • Ugh, sorry. This was wrong if ( !is_page('page-slug-one') && !is_page('page-slug-two') ) , check above for edit. Had "||" instead. – BA_Webimax Aug 18 '14 at 18:17
  • That worked! How does it feel to be awesome?! – jimw Aug 18 '14 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.