Redirect all WordPress posts and pages to homepage

FoamyMedia picture FoamyMedia · Apr 27, 2017 · Viewed 7.7k times · Source

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?

Answer

Thoby picture Thoby · Apr 27, 2017

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!