Created
January 14, 2025 18:32
-
-
Save cargabsj175/5b85ff99510508b569b8160c897b1838 to your computer and use it in GitHub Desktop.
incrustar un shorcode en una etiqueta div indicada
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1. Generar el contenido del shortcode en el servidor y añadirlo en un contenedor oculto | |
| function insertar_shortcode_en_contenido($content) { | |
| // Solo ejecutar en posts individuales del tipo 'property' | |
| if (!is_singular('property')) { | |
| return $content; | |
| } | |
| // Generar el contenido del shortcode | |
| $mi_shortcode = do_shortcode('[dynamic_button]'); | |
| // Incluir el contenido del shortcode en un atributo de datos | |
| $content .= '<div id="shortcode-container" style="display:none;" data-shortcode-content="' . esc_attr($mi_shortcode) . '"></div>'; | |
| return $content; | |
| } | |
| add_filter('the_content', 'insertar_shortcode_en_contenido'); | |
| // 2. Agregar JavaScript para insertar el contenido del shortcode en los lugares adecuados | |
| function agregar_script_shortcode() { | |
| if (is_singular('property')) { | |
| ?> | |
| <script type="text/javascript"> | |
| document.addEventListener('DOMContentLoaded', function() { | |
| // Seleccionar el contenedor oculto con el contenido del shortcode | |
| var shortcodeContainer = document.getElementById('shortcode-container'); | |
| // Si el contenedor existe, insertar el contenido en el lugar deseado | |
| if (shortcodeContainer) { | |
| var shortcodeContent = shortcodeContainer.getAttribute('data-shortcode-content'); | |
| // Seleccionar todos los elementos que tienen las clases especificadas | |
| var divObjetivos = document.querySelectorAll('.item-tools'); // Añade tantas clases como necesites | |
| // Recorrer cada elemento y insertar el contenido del shortcode | |
| divObjetivos.forEach(function(divObjetivo) { | |
| divObjetivo.insertAdjacentHTML('beforebegin', shortcodeContent); // 'beforebegin' para insertar antes | |
| // divObjetivo.insertAdjacentHTML('afterend', shortcodeContent); // 'afterend' para insertar después | |
| }); | |
| } | |
| }); | |
| </script> | |
| <?php | |
| } | |
| } | |
| add_action('wp_footer', 'agregar_script_shortcode'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment