Adding custom content to password protected page

I am wanting to add some content to the password protected page. Just some text and a gravity form so people can request access if they don;'t have a password. How can I go about this the best? Is there a way i can create a template of add a function to modify in my child theme folder?

Thank you

Just an update. I did figure out how to add this in my functions file. So my only question is if there is a way i can set different content on different password protected pages?

This is the code i used:

// PASSWORD PROTECT
// =============================================================================


function my_password_form() {
    global $post;
    $label = 'pwbox-'.( empty( $post->ID ) ? rand() : $post->ID );
    $o = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" method="post">
' . __( "<h1 class='mtn h3'>Health Care Practitioners Portal</h3><p class='font-150 strong mtn mbs'>Please enter your access password below.</p><p class='font-120 mtn mbs'>This content is intended for healthcare practitioners who are interested in education and resources regarding medical cannabis.</p><p class='mtn mbn'>If you do not currently have a password, please email <a href='mailto:info@hybridpharm.com'>info@hybridpharm.com</a></p>" ) . '
    <label for="' . $label . '">' . __( "Password:" ) . ' </label><input name="post_password" id="' . $label . '" type="password" size="20" maxlength="20" /><input type="submit" name="Submit" value="' . esc_attr__( "Submit" ) . '" />
    </form>
    ';
    return $o;
}
add_filter( 'the_password_form', 'my_password_form' );

Hello Eyedia,

Thanks for writing in!

By default, the password protected pages will display a password field. Do you want to display a custom form instead? You will have to display two forms. The first one would be the default password for people who knew the password. And then the second one will be your custom form for people to ask for the password. You might want to update your code and use this:

// An X custom password protected post/page
// =============================================================================
function x_custom_password_protected() {
    global $post;
    $label = 'pwbox-'.( empty( $post->ID ) ? rand() : $post->ID );
    $o = '<div class="x-protect"><form class="mbn" action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" method="post">
    ' . __( "To view this protected post, enter the password below:" ) . '
    <div><label for="' . $label . '">' . __( "Password:" ) . ' </label><input name="post_password" id="' . $label . '" type="password" size="20" maxlength="20" /></div><div><input type="submit" name="Submit" value="' . esc_attr__( "Submit" ) . '" /></div>
    </form></div>
    ';

    $o .= '<div class="x-protect mtm">' . do_shortcode('[gravity-shortcode]') . '</div>';
    return $o;
}
add_filter( 'the_password_form', 'x_custom_password_protected' );
// =============================================================================

Just make sure to replace the [gravity-shortcode] with the correct gravity form shortcode so that it will display correctly.

Please let us know if this works out for you.

Sorry, i think you misunderstood.

What I mean is I have added code in my functions file so as to add a custome message to the password protect page. The issue is this message is now global on ALL password protected pages.

So I am wondering how I can have for example:

password protected PAGE A executes one function showing custom content
and
password protected PAGE B executes another function showing custom content
and
every other password protected pages show the default (only the password field)

Does that make sense? I tried a couple things i found in forums elsewhere if if statements grabbing the page ID but i couldn’t get any to work :confused:

Hello Eyedia,

What you need seems to have an if-else conditions so that you can display the custom content you want. You may need something like this:

// An X custom password protected post/page
// =============================================================================
function x_custom_password_protected() {
    global $post;
    $label = 'pwbox-'.( empty( $post->ID ) ? rand() : $post->ID );
    
    if ( is_page("A") ) {
        $o = '<div class="x-protect mtm"><p>Your Custom Content for Page A</p></div>';
    } else if ( is_page("B") ) {
        $o = '<div class="x-protect mtm"><p>Your Custom Content for Page B</p></div>';
    } else {
        $o = '<div class="x-protect"><form class="mbn" action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" method="post">
        ' . __( "To view this protected post, enter the password below:" ) . '
        <div><label for="' . $label . '">' . __( "Password:" ) . ' </label><input name="post_password" id="' . $label . '" type="password" size="20" maxlength="20" /></div><div><input type="submit" name="Submit" value="' . esc_attr__( "Submit" ) . '" /></div>
        </form></div>
        ';
    }
    
    return $o;
}
add_filter( 'the_password_form', 'x_custom_password_protected' );
// =============================================================================

Hope this helps. Kindly let us know.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.