Estoy usando wordpress y tengo una función como esta, cómo redirigir a la URL correspondiente solo ocurre al hacer clic, por ahora, la página de idioma "en" se redirige automáticamente después de cargar la página, pero el redireccionamiento "zh-hant" después de hacer clic, cualquiera ¿Puede ayudarme a verificar el código? Gracias.
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 } } }Deberías hacerlo todo en una sola función.
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 } }También puede hacerlo simplemente usando php y sin js en absoluto
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. */Un método sería hacer lo siguiente usando una declaración de cambio.
Esto permite un fácil crecimiento y, en caso de que termine con MUCHOS idiomas, es fácil repetirlos y recurre a la URL del sitio en caso de que no haya una coincidencia.
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; } } }