Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
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
        }
    }
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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. */
about 4 years ago · Juan Pablo Isaza Report

0

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;
        }
    }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!