Support » Developing with WordPress » 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 nothing

    Help 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' );
    • This topic was modified 4 years, 5 months ago by TWD.
Viewing 2 replies - 1 through 2 (of 2 total)
  • There’s two options.

    First, you can just do a string replace:

    $new_url = str_replace ('/lessons', '/ja/lessons', $old_url);

    Secondly, if you want to do some testing before that, you could check if that is actually part of the URL.

    if (strpos ($old_url, '/ja/lessons') !== false) {
        $new_url = str_replace ('/lessons', '/ja/lessons', $old_url);
    }
    Thread Starter TWD

    (@twd)

    Actually the $search and $replace variables above are the other way around, but basically THANKS. You nailed it.

    Final working code:

    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);
    
        // $currenturl_relative returns a string 
        // such as '/devel/lms/ja/lessons/my-lesson/'
        // or '/devel/lms/ja/quizzes/my-quiz/'
    
        // test to see if the URL is looking for a Lesson or Quiz translation
        // and if true, then direct it to the non-translated version URL
    
        if (strpos ($currenturl_relative, '/ja/lessons/') !== false) {
            $new_url = str_replace ('/ja/lessons/', '/lessons/', $currenturl_relative);
            $urlto = $protocol . $_SERVER['HTTP_HOST'] . $new_url;
        } elseif (strpos ($currenturl_relative, '/ja/quizzes/') !== false) {
            $new_url = str_replace ('/ja/quizzes/', '/quizzes/', $currenturl_relative);
            $urlto = $protocol . $_SERVER['HTTP_HOST'] . $new_url;
        } else {
            $urlto = $currenturl;
        }
            
        
        if ($currenturl != $urlto)
            exit( wp_redirect( $urlto ) );
    
    }
    add_action( 'template_redirect', 'redirect_page' );

    Leaving this as open in case anyone has suggestions for improvement.

    • This reply was modified 4 years, 5 months ago by TWD.
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.