-
-
Save pablo-sg-pacheco/b6ebef644d93f8480b1955b4bed5b052 to your computer and use it in GitHub Desktop.
<?php | |
/** | |
* Password protects a custom post type programmatically. | |
* | |
* @param $post | |
*/ | |
add_action( 'the_post', function( $post ){ | |
if ( $post->post_type != 'post' ) { | |
return; | |
} | |
$post->post_password = 'your_password'; | |
} ); | |
?> |
Hum, that's odd. I just tried on my end and it works.
Maybe it's a problem with your theme. Try to switch it to Storefront theme for example and check again
Hi ,
I have simple twenty twelve theme, Did you try with custom post type? I have custom post type.
I'm using WooCommerce and I've tried to use the custom post type 'product' and it works just fine.
When I go view the product, the first thing it shows is the password field and I can see no content from the product at all
Hi,
I am trying different ways lets see, anyway thanks a lot
Thank you so much!!
;)
Write the password directly in the code?
How about letting the user set/change password as pages/posts?
Hi @arthurclouds ,
How about letting the user set/change password as pages/posts?
In that case, I believe there wouldn't be necessary to add any code, as this is the default WordPress mechanism when you set a page/post visibility as "Password protected".
Thanks pablo, works great and super useful - we were looking for keeping the same password for all posts to simplify for users and this makes it very easy and programmable.
Hi,
i have implemented the code in plugin when i published the post and then go to view it shows the CPT and below the message "This content is password protected. To view it please enter your password below". The CPT should be hide until enter password.