[Support request] Elements on password protected pages

Home Forums Support [Support request] Elements on password protected pages

Home Forums Support Elements on password protected pages

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #2035688
    Sean

    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!

    #2035700
    David
    Staff
    Customer Support

    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.

    #2035942
    Sean

    Thanks.

    That disables the element – even when the password is entered correctly!

    #2035945
    David
    Staff
    Customer Support

    Made a tweak to the code above.

    #2035968
    Sean

    Thanks. But unfortunately that doesn’t seem to work either – it’s visible with or without the password.

    #2035973
    David
    Staff
    Customer Support

    Did you update the Element ID ?

    #2036063
    Sean

    Yes, even copy/pasted it to make sure!

    #2036122
    David
    Staff
    Customer Support

    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 );
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.