Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

198
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda