PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 5, 2022

[FIXED] Trigger Js function onclick

 February 05, 2022     javascript, php, wordpress     No comments   

Issue

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

Solution

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. */


Answered By - Arun Bohra
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing