Skip to content

Instantly share code, notes, and snippets.

@cargabsj175
Created January 14, 2025 18:32
Show Gist options
  • Select an option

  • Save cargabsj175/5b85ff99510508b569b8160c897b1838 to your computer and use it in GitHub Desktop.

Select an option

Save cargabsj175/5b85ff99510508b569b8160c897b1838 to your computer and use it in GitHub Desktop.
incrustar un shorcode en una etiqueta div indicada
// 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