Reputation: 1
I have a page with a dropdownmenue that is generated by [dynamic_gallery] which is fine, and I see the content I want to see listed, also fine. But if I click on any of the items, a plain text shortcode is being generated, and not the gallery (plugin: FlaGallery) that I need.
here is the full code from my themes ´functions.php:
// Function to create dynamic dropdown for galleries based on date
function dynamic_gallery_dropdown() {
global $wpdb;
ob_start();
// Get all FlaGallery albums from the database
$table_name = $wpdb->prefix . 'flag_album';
$albums = $wpdb->get_results("SELECT id, name FROM $table_name");
// Create an associative array to map album names (DD-MM) to their IDs
$gallery_map = [];
foreach ($albums as $album) {
$gallery_map[$album->name] = $album->id;
}
// Get today's date
$today = new DateTime();
// Start dropdown HTML
echo '<select id="galleryDropdown" onchange="loadGallery(this.value)">';
echo '<option value="">Wähle ein Datum...</option>';
// Generate next 4 days
for ($i = 0; $i < 4; $i++) {
$date = clone $today;
$date->modify("+$i days");
// Format for display (e.g., "Dienstag 28.01.2025")
setlocale(LC_TIME, 'de_DE.UTF-8');
$displayDate = strftime("%A %d.%m.%Y", $date->getTimestamp());
// Format for FlaGallery lookup (e.g., "28-01")
$galleryName = $date->format("d-m");
// Get the corresponding album ID
if (isset($gallery_map[$galleryName])) {
$albumID = $gallery_map[$galleryName];
echo "<option value='$albumID'>$displayDate</option>";
}
}
echo '</select>';
echo '<div id="galleryContainer"></div>';
// JavaScript for loading the gallery
wp_localize_script('jquery', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
echo "<script>
function loadGallery(albumID) {
if (albumID) {
jQuery.ajax({
type: 'POST',
url: ajax_object.ajaxurl,
data: {
'action': 'load_gallery',
'albumID': albumID
},
success: function(data) {
// Ensure the gallery content is inserted properly into the container
document.getElementById('galleryContainer').innerHTML = data;
}
});
}
}
</script>";
return ob_get_clean();
}
// Register the dynamic gallery shortcode
add_shortcode('dynamic_gallery', 'dynamic_gallery_dropdown');
// AJAX callback for loading gallery
function load_gallery_callback() {
if (isset($_POST['albumID'])) {
$albumID = intval($_POST['albumID']);
// Create the gallery shortcode with the album ID
$shortcode = '[flagallery album=' . $albumID . ']';
// Use do_shortcode() to process the gallery shortcode and return the result
echo do_shortcode('[flagallery album=' . $albumID . ']');
}
wp_die(); // End the request
}
add_action('wp_ajax_load_gallery', 'load_gallery_callback');
add_action('wp_ajax_nopriv_load_gallery', 'load_gallery_callback');
// Enqueue jQuery and localize AJAX for frontend use
function enqueue_custom_scripts() {
wp_enqueue_script('jquery');
wp_localize_script('jquery', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
}
add_action('wp_enqueue_scripts', 'enqueue_custom_scripts');
tried ajax, JavaScript, with the same result, I really struggle on this one.
Upvotes: 0
Views: 18