How to Remove ‘Private’ or ‘Protected’ Before Titles in WordPress

Home » Snippets » How to Remove ‘Private’ or ‘Protected’ Before Titles in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you perplexed by the words ‘Private’ or ‘Protected’ that appear before your WordPress post titles? These prefixes automatically show up whenever you protect your material or set it to private, but they can be quite annoying and unattractive to display. You might be concerned about the potential impact on user experience or the aesthetics of your site. If you’d like to remove these prefixes, you’re in luck. This article will guide you through the steps to eliminate ‘Private’ or ‘Protected’ from your WordPress titles effortlessly.

					function wpturbo_remove_private_prefix($title) {
    $title = str_replace('Private: ', '', $title);
    $title = str_replace('Protected: ', '', $title);
    return $title;
}
add_filter('the_title', 'wpturbo_remove_private_prefix');
				

The provided PHP code is designed to manipulate the title of private or protected WordPress pages or posts. More specifically, it removes the ‘Private:’ or ‘Protected:’ prefixes automatically added by WordPress to the titles of such pages or posts.

function wpturbo_remove_private_prefix($title) {
    $title = str_replace('Private: ', '', $title);
    $title = str_replace('Protected: ', '', $title);
    return $title;
}
add_filter('the_title', 'wpturbo_remove_private_prefix');

Let’s break it down:

The code begins with the declaration of a new function named wpturbo_remove_private_prefix(). The function accepts a single argument: $title. This argument represents the title of the page or the post that WordPress is currently processing.

function wpturbo_remove_private_prefix($title) {
    ...
}

Inside this custom function, we then implement two lines of code, each utilizing the PHP str_replace() builtin function, which is used to replace some characters with some other characters in a string.

$title = str_replace('Private: ', '', $title);
$title = str_replace('Protected: ', '', $title);

The first line replaces ‘Private: ‘ (including the space after the colon) with an empty string ”, effectively removing it if it is found in the $title. The second line does exactly the same for ‘Protected: ‘. Therefore, these two lines work together to strip either prefix from the $title if they exist.

After processing is complete, the new (potentially modified) $title is returned with:

return $title;

Finally, the custom function is hooked into the WordPress filter the_title using the add_filter() function.

add_filter('the_title', 'wpturbo_remove_private_prefix');

This means that WordPress will automatically call wpturbo_remove_private_prefix() for each title it displays, applying our prefix-removal logic. If the title being processed is of a private or protected post or page, the function removes the ‘Private:’ or ‘Protected:’ prefix, resulting in a cleaner title.

Register an account to save your snippets or go Pro to get more features.