Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy load a Divi shortcode from library using AJAX #713

Open
japl71 opened this issue Oct 15, 2024 · 0 comments
Open

Lazy load a Divi shortcode from library using AJAX #713

japl71 opened this issue Oct 15, 2024 · 0 comments

Comments

@japl71
Copy link

japl71 commented Oct 15, 2024

Hi. I am trying to lazy load a Divi shortcode from library using AJAX, but the only output I get in the page is
[et_pb_section global_module="16594"][/et_pb_section]

Steps To Reproduce

  1. In child theme functions.php I am using this code: function load_lazy_shortcode_content() {
    if (isset($_GET['global_module'])) {
    $global_module_id = intval($_GET['global_module']);
    $shortcode = '[et_pb_section global_module="' . $global_module_id . '"][/et_pb_section]';
    echo do_shortcode($shortcode);
    }
    wp_die();
    }
    add_action('wp_ajax_load_lazy_shortcode_content', 'load_lazy_shortcode_content');
    add_action('wp_ajax_nopriv_load_lazy_shortcode_content', 'load_lazy_shortcode_content');

  2. In builder, this javascript to handle AJAX calling

<script async> document.addEventListener("DOMContentLoaded", function () { const lazyListContainer = document.getElementById("lazyListContainer"); let divLoaded = false; function loadDiv() { const rect = lazyListContainer.getBoundingClientRect(); const windowHeight = window.innerHeight || document.documentElement.clientHeight; if (rect.top < windowHeight && rect.bottom > 0 && !divLoaded) { divLoaded = true; // Marca que el div ya ha sido cargado const xhr = new XMLHttpRequest(); xhr.open("GET", "/wp-admin/admin-ajax.php?action=load_lazy_shortcode_content&global_module=16594", true); xhr.onload = function () { if (xhr.status === 200) { const lazyDiv = document.createElement("div"); lazyDiv.classList.add("lista-lazy-loaded"); lazyDiv.innerHTML = xhr.responseText; // Inserta el contenido HTML del shortcode procesado lazyListContainer.appendChild(lazyDiv); } }; xhr.send(); window.removeEventListener("scroll", loadDiv); } } window.addEventListener("scroll", loadDiv); loadDiv(); }); </script>
  1. All i get in the frontend is this: [et_pb_section global_module="16594"][/et_pb_section]

image

What am I doing wrong? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant