skip to Main Content

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
        }
    }
}

2

Answers


  1. You should be doing all of it in one function itself

    
    add_action( 'wp_head', 'redirect_after_booking' );
    function redirect_after_booking() {
        if ( in_category('teachers') ) {
            $url_endpoint = '';
            if(ICL_LANGUAGE_CODE=='en'){
                
            } else if (ICL_LANGUAGE_CODE=='zh-hans') {
                $url_endpoint = '/zh-hans';
            }else if (ICL_LANGUAGE_CODE=='zh-hant') {
                $url_endpoint = '/zh-hant';
            }
            ?>
            <script>
                window.beforeConfirmedBooking = function() {
                    window.location.href = "https://aaa.com<?php echo $url_endpoint; ?>";
                };
                const btn = document.querySelector('.el-button .el-button--primary .redirect-link');
                btn.addEventListener('click', beforeConfirmedBooking);
            </script>
            <?php
        }
    }
    
    

    You can also do it just using php and no js at all

    add_action( 'wp_head', 'redirect_after_booking' );
    function redirect_after_booking() {
        if ( in_category('teachers') ) {
            $url_endpoint = '';
            if(ICL_LANGUAGE_CODE=='en'){
                
            } else if (ICL_LANGUAGE_CODE=='zh-hans') {
                $url_endpoint = '/zh-hans';
            }else if (ICL_LANGUAGE_CODE=='zh-hant') {
                $url_endpoint = '/zh-hant';
            }
        }
    }
    
    // The following will redirect you to where ever you want
    header('Location: https://aaa.com' . $url_endpoint);
    
    /* Make sure that code below does not get executed when we redirect. */
    
    Login or Signup to reply.
  2. 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;
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search