Skip to content

Instantly share code, notes, and snippets.

@AmmarCodes
Last active March 20, 2024 21:04
Show Gist options
  • Save AmmarCodes/a85211e3a5b8dd16029b261c7e46c7f1 to your computer and use it in GitHub Desktop.
Save AmmarCodes/a85211e3a5b8dd16029b261c7e46c7f1 to your computer and use it in GitHub Desktop.
WordPress: link to specific elementor tab on a specific page

It's a quick ad-hoc solution to allow you to link to your page to a specific tab from elementor tabs widget.

Here's the catch, you need to replace the id of the specific tab you want to use to not contain the title- part.

Use https://your-website/#elementor-tab-1515

instead of: https://your-website/#elementor-tab-title-1515

Make sure you add the JavaScript code in the footer, so it loads after the elementor tabs widget, otherwise the elementor tabs widget code will override what we're trying to acheive.

@zodieman
Copy link

zodieman commented Feb 6, 2024

Hi there, this is my code, for me, it works well https://github.com/Artem66/elementor-tab-anchor/blob/master/main.js

example link will be something like this - ?dummy=elementor-tab-title-1341#elementor-tab-title-1341 be careful with variable suf if your tab id is less than four digits

While it works, for me the result is that it doesn't render tabs that are above the one I'm targeting. Refreshing the page renders everything correctly but I lose the tab focus I'm trying to target. Any thoughts?

@Artem66
Copy link

Artem66 commented Feb 6, 2024

@zodieman You can try to remove setTimeout and run the script after the DOM is fully loaded.

jQuery(document).ready(function($) {
    var suf = '';

    var setupTabs = function() {
        var current = window.location.href;
        suf = current.slice(-4, -1);
        var current = current.split(`#elementor-tab-title-${suf}`);
        if (current.length > 1) {
            $('.elementor-tab-title').removeClass('elementor-active');
            $('.elementor-tab-title[data-tab="'+current[1]+'"]').addClass('elementor-active');
            $('.elementor-tab-content').hide();
            $('.elementor-tab-content[data-tab="'+current[1]+'"]').show();
        }
    };

    $(window).on('load', function() {
        setupTabs();
    });

    $('.elementor-tab-title[data-tab]').click(function(){
        var current_location = window.location.href;
        current_location = current_location.split(`#${suf}`);
        window.location = current_location[0]+'#elementor-tab-title'+$(this).attr('data-tab');
    });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment