Hide the Private prefix on one specific page

Trying to conditionally hide the “private” prefix in front of the page title on one specific page named ‘members’ only.

I used this code in functions.php to hide the prefix on all pages.

function title_format($content) {
return '%s';
}

add_filter('private_title_format', 'title_format');
add_filter('protected_title_format', 'title_format');

Tried to hook into this with the if is_page ( ‘members’ ) in various ways, but so far the only result i managed to get is fatal errors.

Solutions Collecting From Web of "Hide the Private prefix on one specific page"

This worked for me. One of the keys is that you have to make sure to pass the content through unchanged if it doesn’t meet the condition.

function title_format($content) {
    if (is_page('members')) :
        return '%s';
    else :
        return $content;
    endif;
}

add_filter('private_title_format', 'title_format');
add_filter('protected_title_format', 'title_format');

This will only apply to the page with a slug of ‘members’, though, and it sounds like you’re wanting to display a list of page titles on the Members page, and remove the prefix there. Is that the only place? Once the Member clicks the link, do you still want to omit the Private: and Protected: prefix on the title for the specific page?

If so, one approach would be to move all of the Protected: pages as children of the parent page. Use the code above, but modify the condition to determine if the pages are children of the members page.

Another approach would be to filter the title on the page template when you loop it out to the page. A function that I repurposed from csstricks:

function the_title_trim($title) {

    $title = attribute_escape($title);

    $findthese = array(
        '#Protected:#',
        '#Private:#'
    );

    $replacewith = array(
        '', // What to replace "Protected:" with
        '' // What to replace "Private:" with
    );

    $title = preg_replace($findthese, $replacewith, $title);
    return $title;
}

And in your page template you would display the title like this:

echo the_title_trim(get_the_title());

That would only apply to that particular page template on the Members page assuming that you’re creating a custom template for that page.

I hope this gives you a few ideas for how to approach it.