Code

In WordPress, hide page name for protected pages

WordPress has a handy feature where you can password protect pages on your site. When a user lands on that page they are shown only the page name (preceded by “Protected”) and a form to enter a password.

On a recent project, I also wanted to hide the page name since that was also sensitive, along with the content of the page.

The fix is pretty easy, and uses the post_password_required() function.

Simply drop the following code into the functions.php file of your theme:

add_filter('protected_title_format', 'protected_title_function');

function protected_title_function($title) {

  if ( post_password_required() ) {
    $return_val = __('Protected', 'themedomain');
  } else {
    $return_val = '%s';
  }
	
  return $return_val;
}

Don’t forget to also change the permalink since it is generally based on the text of the title.

Leave a Reply

Your email address will not be published. Required fields are marked *