Archived topic
Elements on password protected pages
7 replies · Started by Sean on December 2, 2021
A client wants me to add a live chat button onto a password protected page (standard Wordpress visibility set to Password Protected) - I can use a hook element to add the script to the page easily enough.
But, it's loaded before the visitor has entered the password. Is there any way of disabling the element until the page password has been entered?
I've got round it by adding a HTML block and putting the script in that - but that leaves the potential for an editor to accidently delete it!
Thanks!
Hi there,
you can use this PHP to set an Element to display:
add_filter( 'generate_element_display', function( $display, $element_id ) {
global $post;
if ( 100 === $element_id && empty($post->post_password) ) {
$display = false;
}
return $display;
}, 10, 2 );
Replace the 100 with the ID of the Element.
That snippet will then disable the element if the password has not been entered.
Thanks.
That disables the element - even when the password is entered correctly!
Made a tweak to the code above.
Thanks. But unfortunately that doesn't seem to work either - it's visible with or without the password.
Did you update the Element ID ?
Yes, even copy/pasted it to make sure!
Try this method:
add_filter( 'generate_element_display', function( $display, $element_id ) {
if ( 100 === $element_id && post_password_required() ) {
$display = false;
}
return $display;
}, 10, 2 );