4

Is it possible to add hooks on function.php for disable password protected page for back-end users (logged users)?

I use beaver builder and when i modify page or post I need always to put the password of the page before to access to the builder.

Thanks.

4

Disable the post password protection for (some) users

You can try the post_password_required filter (4.7+) to override it for logged in users:

add_filter( 'post_password_required', function( $returned, $post )
{
    // Override it for logged in users:
    if( $returned && is_user_logged_in() )
        $returned = false;

    return $returned;
}, 10, 2 );

or disable it for users with a given capability ( e.g. manage_options) :

add_filter( 'post_password_required', function( $returned, $post )
{
    // Override it for users with the 'manage_options' capability
    if( $returned && current_user_can( 'manage_options' ) )
        $returned = false;

    return $returned;
}, 10, 2 );

We could target a given post type with:

add_filter( 'post_password_required', function( $returned, $post )
{
    // Target protected posts only
    if( ! $returned )
        return $returned;

    // Target logged in users only
    if( ! is_user_logged_in() )
        return $returned;

    // Target 'page' post type only
   if( 'page' !== get_post_type( $post ) )
        return $returned;

   // Override 
   return false;

}, 10, 2 );

Remove 'Protected' text from protected post titles

To remove the prepended Protected from the title, we can adjust the protected title format, for logged in users, with the protected_title_format filter:

add_filter( 'protected_title_format', function( $format, $post )
{
    if( is_user_logged_in() )
        $format = '%s';

    return $format;
}, 10, 2 );
  • It's works great thanks! is it possible to disable password protected page for a specific user role? – sampaii May 25 '17 at 12:07
  • yes, I updated the answer @sampaii – birgire May 25 '17 at 15:38
  • Thanks very much @birgire. I have an other question at this link if you have an idea : wordpress.stackexchange.com/questions/268228/… . Thanks again for your help – sampaii May 26 '17 at 15:36

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.