In WordPress, I need to redirect a child page to its parent page. When the parent page has password protection set, the function code I have below also protects its child pages from having a different password on each page. But I need that when a user goes to a child page, it automatically redirects to its parent page so that they enter the password in the form.

I currently have this function added in functions.php that displays the same form from the parent page on the child pages.

// This function prints the password form if the parent page is password protected. It is called whenever 'the_content' is invoked.
function ft_password_protect_children_page_contents( $org_content ){
    if ( is_page() ){
        global $post;
        $ancestors = $post->ancestors;
        foreach ( $ancestors as $ancestor ) {
            if ( post_password_required( $ancestor ) ) {
                $real_post = $post;
                $post = get_post( $ancestor );
                echo get_the_password_form();
                $post = $real_post;
    return $org_content;
add_filter( 'the_content', 'ft_password_protect_children_page_contents' );

Could you also help me with another functionality: When the user enters the password in the form that is on the parent page, automatically redirect to the first child page. This only when the correct password is entered. Please, Help me!!!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.