Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda