1

I need to expand on this discussion Redirect entire website to a single page to include two pages rather than one.

On this site, I need two pages to be live and all others to redirect to the home page.

http://prairiebrand.com/

This is the code I'm trying and I get a perpetual redirect.

add_action( 'template_redirect', 'wpse_76802_goodbye_redirect' );

    function wpse_76802_goodbye_redirect() {
        if (( ! is_page( 16372 ) ) || ( ! is_page( 16384 ) )) {
            wp_redirect( esc_url_raw( home_url( 'index.php?page_id=16372' ), 301 ) );
            exit;
        }
    }
1

is_page() supports array too, so you could do something like this.

add_action( 'template_redirect', 'wpse_76802_goodbye_redirect' );

function wpse_76802_goodbye_redirect() {
    if ( !is_page( array( 16372, 16384 ))) {
        wp_redirect( esc_url_raw( home_url( 'index.php?page_id=16372' ), 301 ) );
        exit;
    }
}
6
  • This code throws this error:
    – Brett
    Aug 15 '18 at 20:01
  • Parse error: syntax error, unexpected '{' in /nfs/c09/h01/mnt/130406/domains/prairiebrand.com/html/wp-content/themes/prairie-brand-3.1/functions.php on line 323
    – Brett
    Aug 15 '18 at 20:01
  • Everything looks correct though.
    – Brett
    Aug 15 '18 at 20:02
  • Check your funcions.php on line 323, to check the unexpected '{', when I am not sure I simply remove the code, save to check everything is ok and then simply add it at the bottom. Aug 15 '18 at 20:04
  • I ")" was actually missing in this line: if ( !is_page( array( 16372, 16384 ) ) {. I will correct it for future visitors Thanks for help!
    – Brett
    Aug 15 '18 at 20:58
1

You have invalid condition. Change || to && (otherwise if always will be TRUE):

if ( !is_page( 16372 ) && !is_page( 16384 ) )

This mean: if current page ID is not 16372 AND is not 16384 then redirect.

Full code:

add_action( 'template_redirect', 'wpse_76802_goodbye_redirect' );

function wpse_76802_goodbye_redirect() {
    if ( !is_page( 16372 ) && !is_page( 16384 ) ) {
        wp_redirect( esc_url_raw( home_url( 'index.php?page_id=16372' ), 301) );
        exit;
    }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.