How to replace a Case Switch with more robust code snippet
-
The following Case-Switch function does exactly what I want,
which is to redirect the page from:/ja/lessons/planning-a-meeting-int/
to
/lessons/planning-a-meeting-int/
But rather than type out a new “Case-Switch” statement for every
url, of course it would be better to have code that:
a) tests if the the url contains “ja/lessons”
b) if true, then implement the script
c) else do nothingHelp for a PHP newbie?
function redirect_page() { if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') { $protocol = 'https://'; } else { $protocol = 'http://'; } $currenturl = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $currenturl_relative = wp_make_link_relative($currenturl); switch ($currenturl_relative) { case '/devel/lms/ja/lessons/planning-a-meeting-int/': $my_array = explode('/',$currenturl_relative); $my_array_fixed = array_splice($my_array,4); $my_string = implode('/',$my_array_fixed); $my_string = '/'.$my_string; $urlto = home_url($my_string); break; default: return; } if ($currenturl != $urlto) exit( wp_redirect( $urlto ) ); } add_action( 'template_redirect', 'redirect_page' );
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘How to replace a Case Switch with more robust code snippet’ is closed to new replies.