'How to redirect a child page to its parent page.? - WordPress
In WordPress, I need to redirect a child page to its parent page. When the parent page has a password protection set, the function code I have below also protects its child pages but I need that when a user goes to a child page it automatically redirects to its parent page so that it enters 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;
}
}
}
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|