skip to Main Content

I’ll keep it short.

I have a WordPress page and need to redirect users to another URL after 4 seconds. This URL is a query parameter.

Here is an example.

They land on my page with this URL:

https://koniecstresu.sk/dakujem5/?wj_lead_unique_link_live_room=https%3A%2F%2Fevent.webinarjam.com%2Fgo%2Flive%2F15%2Fmqx6vt5coriq3rforg

The wj_lead_unique_link_live_room is the parameter.

And in this instance, they need to be redirected to this URL after 4 seconds: https://event.webinarjam.com/go/live/15/mqx6vt5coriq3rforg

I need to do this in HTML and JavaScript, which I can embed to the WordPress page in Elementor… tried to do it many different ways, but I could not figure out how to first get the URL parameter.

Thank y’all!

2

Answers


  1. You can check if the parameter is set using URLSearchParams, and if the parameter exists, then add a timeout that redirects after 4 seconds to the given target.

    const params = new URLSearchParams(window.location.href)
    
    if( params.has('wj_lead_unique_link_live_room') ) {
        const redirectUrl = decodeURIComponent(params.get('wj_lead_unique_link_live_room'))
        setTimeout(() => window.location.href = redirectUrl, 4000)
    }
    
    Login or Signup to reply.
  2. You can use URLSearchParams for search params in the URL. check the code below. code will go to the active theme functions.php file.

    function redirect_to_given_url_based_on_params(){ ?>
        <script type="text/javascript">
            (function(){
                var params = new URLSearchParams( window.location.href );
                if( params.has( 'wj_lead_unique_link_live_room' ) ) {
                    var redirect_url = decodeURIComponent( params.get( 'wj_lead_unique_link_live_room' ) );
                    setTimeout( function() {
                        window.location.href = redirec_url;
                    }, 4000 );
                }
            });
        </script>
    <?php }
    add_action( 'wp_footer', 'redirect_to_given_url_based_on_params', 10, 1 );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search