How to add a switch to admin area which enables/disables crisis-mode: Redirect all traffic to certain page if switch is enabled

Question

Case:
There is natural disaster or terrorist attack etc. Admin logins to site and enables "crisis mode". All traffic gets redirected to crisis page.

I found this code from another answer which deals the redirect part. So how to create a switch to admin area which executes the code.

add_action( 'template_redirect', function() {
    if ( is_page( 813 ) ) {
        return;
    }

    wp_redirect( esc_url_raw( home_url( 'index.php?page_id=183' ) ) );
    exit;
} );

Answer to the question:
I fiqured out a solution with ACF option field.

  1. Create option page
  2. Add 1/0 Switch to option page
  3. Add code to child themes functions.php
add_action( 'template_redirect', function() {
  if ( is_page( 853 ) ) {
    return;
  }
  if(get_field('crisis', 'option')) {
    wp_redirect( esc_url_raw( home_url( 'index.php?page_id=853' ) ) );
    exit;
  }
} );
  1. Done.

Do you have a better approuch to this?

0
Antti Makela 2 months 0 Answers 12 views 0

Leave an answer