1

I'm currently using Sage 8.5.1 theme and have some password protected pages. The client would like to have the word "Protected" removed but not the title. I've used this code snippet

add_filter( 'protected_title_format', 'remove_protected_text' );
function remove_protected_text() {
    return __('%s');
}

But it removes the title also. I've looked and everywhere seems to use basically the same snippet of code. I've posed this question on Roots Discourse and they said it's not theme specific and to post my question here. Thanks

2 Answers 2

1

You don't want to translate the title, so there's no need to use __() here and you won't translate '%s', because that's a placeholder.

add_filter( 'protected_title_format', 'remove_protected_text' );
function remove_protected_text() {
    return '%s';
}

should work.

3
  • 1
    I would also add in the private_title_format filter hook too for good measure.
    – Howdy_McGee
    Commented Oct 30, 2017 at 20:32
  • This did not work. Entire title removed.
    – kemmieg
    Commented Oct 31, 2017 at 20:15
  • In that case, something else is happening in your system. I've tested it in a local installation and it worked just fine. It also makes sense looking at the code. Is the title still removed if you replace %s with anything else?
    – janh
    Commented Oct 31, 2017 at 20:25
0

use a filter function: Copy and paste whichever you prefer into your theme functions.php

<?php
function the_title_trim($title){
$pattern[0] = '/Protected:/';
$replacement[0] = ''; // Enter some text to put in place of Protected:
return preg_replace($pattern, $replacement, $title);
}
add_filter('the_title', 'the_title_trim');
?>
3
  • This seems like a more complicated version than the previously posted. answer.
    – Howdy_McGee
    Commented Oct 30, 2017 at 20:32
  • This one throws a php error.
    – kemmieg
    Commented Oct 31, 2017 at 20:25
  • If you are putting in php tag it will shows an error before this code just close the previous php code link this: ?> add this tag before the code mentioned above.
    – Swati
    Commented Nov 1, 2017 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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