(function() { // Wait for the element to be present function bestio_runWhenElementExists(selector, callback) { const element = document.querySelector(selector); if (element) { callback(); } else { // Retry after a short delay if the element isn't found setTimeout(() => bestio_runWhenElementExists(selector, callback), 100); } } // Ensure wp.customize is available before running the script function bestio_runWhenCustomizeIsReady(callback) { if (typeof wp !== 'undefined' && typeof wp.customize !== 'undefined') { callback(wp.customize); } else { setTimeout(() => bestio_runWhenCustomizeIsReady(callback), 100); } } // Specify the selector for the element you're waiting for const elementSelector = '#your-element-id-or-class'; // Replace with your actual element selector bestio_runWhenElementExists(elementSelector, function() { bestio_runWhenCustomizeIsReady(function(api) { // Your original code api.sectionConstructor['bestio-upsell'] = api.Section.extend({ attachEvents: function() {}, isContextuallyActive: function() { return true; } }); const bestio_section_lists = ['banner', 'service']; bestio_section_lists.forEach(bestio_homepage_scroll); function bestio_homepage_scroll(item) { item = item.replace(/-/g, '_'); api.section('bestio_' + item + '_section', function(section) { section.expanded.bind(function(isExpanding) { api.previewer.send(item, { expanded: isExpanding }); }); }); } }); }); })();