'Trigger Js function onclick

I am using wordpress and have a function like this, how to redirect to the corresponding url only happens onclick, for now the "en" language page is automatically redirecting after the page load, but the "zh-hant" redirecting after click, anyone can help me to check the code? Thanks.

add_action( 'wp_head', 'redirect_after_booking' );
function redirect_after_booking() {
    if ( in_category('teachers') ) {
        if(ICL_LANGUAGE_CODE=='en'){
        ?>
            <script>
                window.beforeConfirmedBooking = function() {
                    window.location.href = "https://aaa.com";
                };
            </script>
        <?php
        }
        if(ICL_LANGUAGE_CODE=='zh-hant'){
        ?>
            <script>
                window.beforeConfirmedBooking = function() {
                    window.location.href = "https://aaa.com/zh-hant";
                };
            </script>
        <?php
        }
    }
}


Solution 1:[1]

One method would be to do the below using a switch statement.

This allows for easy growth and in the event you end up with a LOT of languages easy to just repeat and falls back to the site URL in the event there isn't a match.

add_action( 'wp_head', 'redirect_after_booking' );
function redirect_after_booking() {
    if ( in_category('teachers') ) {
        
        switch (CL_LANGUAGE_CODE) {
            case 'zh-hant':
                wp_redirect( trailingslashit( get_site_url() ) . CL_LANGUAGE_CODE );
                exit;
                break;
            default:
                wp_redirect( get_site_url() );
                exit;
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bazdin