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

Ajax #90

Open
zaferakbiyik opened this issue Mar 4, 2023 · 0 comments
Open

Ajax #90

zaferakbiyik opened this issue Mar 4, 2023 · 0 comments

Comments

@zaferakbiyik
Copy link

Hi, how can use with ajax, do you have a snippit with currently page
for example ajax_dfi(); i think maybe you already coded in plugin.

my way;

function deneme() {
    if (class_exists('Dynamic_Featured_Image')) :
        global $dynamic_featured_image;

        // Get the current page ID from the AJAX request data
        $current_page_id = isset($_POST['current_page_id']) ? intval($_POST['current_page_id']) : null;

        if ($current_page_id) {
            $featured_images = $dynamic_featured_image->get_featured_images($current_page_id);

            if ($featured_images) :
                $result = array();
                foreach ($featured_images as $image) :
                    $result[] = $image['full'];
                endforeach;
                echo json_encode($result);
            endif;
        }
    endif;
    wp_die();
}


add_action( 'wp_ajax_deneme', 'deneme' );
add_action( 'wp_ajax_nopriv_deneme', 'deneme' );

index.php

<script>
jQuery(document).ready(function($) {
    // Get the current page ID
    var current_page_id = <?php echo get_the_ID(); ?>;
    var url = '<?php echo admin_url('admin-ajax.php'); ?>';

    $.ajax({
    url: url,
        type: 'POST',
        data: {
            action: 'deneme',
            current_page_id: current_page_id
        },
        dataType: 'json',
        success: function(data) {
            console.log(data);
        },
        error: function() {
            console.log('Error retrieving featured images.');
        }
    });
});


</script>
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