I want to redirect all the old posts and pages on a WordPress site to the home page as I'm shutting the site down but want to show people a thank you for your support page no matter what page they go on.
I have tried several .htaccess pieces of code but with no success, so any working code samples out there?
2 Answers
If you have direct access to the files, you'd be able to write yourself a solution:
add_action( 'template_redirect', 'redirect_to_homepage' ); function redirect_to_homepage() { $homepage_id = get_option('page_on_front'); if ( ! is_page( $homepage_id ) ) { wp_redirect( home_url( 'index.php?page_id=' . $homepage_id ) ); } } I used this post.
Best of luck!
Below code will redirect all page/post to home page.
add_action('template_redirect', 'wh_redirect_to_home'); function wh_redirect_to_home() { if (!is_home() || is_page() || is_single()) { wp_redirect(esc_url(home_url('/'))); exit(); } } Code goes in functions.php file of your active child theme (or theme). Or also in any plugin php files.
Code is tested and works.
Hope this helps!
1