0

I am trying to add a custom HTML block to the page with the below code. I created a Drop-down List and a search button for redirecting pages. But it seems like that window.location.href doesn't work in WordPress.

Is there any way for adding the code to redirect the page?

<script>     
searchButton.addEventListener('click', () => {
            const selectedOption = filterSelect.value;
            if (selectedOption === 'newsports') {
                window.location.href = 'https://www.google.com/';
            } else if (selectedOption === 'yahoo') {
                window.location.href = 'https://www.yahoo.com/';
            }
</script>
1
  • Leanne, not exactly sure as we can't see how this code is added, but filterSelect.value are you certain that's not empty? Can you add, immediately after the click listener, console.log( filerSelect.value ); and the check your console to see if there's actually anything there? Commented Mar 26 at 2:19

2 Answers 2

1

Your code only works on two conditions. Does not work any time neither of those two conditions are active.

But the proper way to redirect in WP is with the wp_redirect() function:

 wp_redirect('https://example.com/new-page', 301);

If you want to redirect inside your script, use this:

 window.location.replace("https://example.com/new-page");
3
  • She wants the redirect to happen based on user input, where they select from a dropdown and then they get redirected. With wp_redirect() that's executed via PHP on the server, not client side. Commented Mar 26 at 2:21
  • True, but wouldn't the proper script command still be window.location.replace() ? Commented Mar 26 at 21:09
  • I believe they're interchangeable with the sole exception that replace doesn't add new additional records to the browsing history, but since this is redirecting someone based on their interaction, I don't think we're attempting to bypass a URL. So really either should be fine. Commented Mar 26 at 21:16
0

In your code missing searchButton and filterSelect variables and also JS code executes after the DOM has fully loaded try the below the script.

<script>
    document.addEventListener('DOMContentLoaded', function() {
        // Define the search button and filter select
        const searchButton = document.getElementById('searchButton');
        const filterSelect = document.getElementById('filterSelect');

        // Add click event listener to the search button
        searchButton.addEventListener('click', function() {
            const selectedOption = filterSelect.value;
            if (selectedOption === 'newsports') {
                window.location.href = 'https://www.google.com/';
            } else if (selectedOption === 'yahoo') {
                window.location.href = 'https://www.yahoo.com/';
            }
        });
    });
</script>

please change as per your requirement HTML elements with IDs searchButton and filterSelect corresponding to your search button and dropdown list respectively.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.