Stack OverFlow
Stack OverFlow

Reputation: 61

functions.php not sending ajax request

i working on a wordpress custom post type slider by using shortcode. i am using ajax code its working fine when i get data from any other page but it creates problem when i get same data from shortcode of the custom post type which is written in functions.php

there is my functions.php code
add_shortcode( 'speaker-shortcode', 'speakers_shortcode');

function speakers_shortcode( $atts ) { ?>
    <section class="sp_slider slider">
        <?php
        $args = array( 'post_type' => 'speakers', 'posts_per_page' => 10 );
        $the_query = new WP_Query( $args ); 


        if ( $the_query->have_posts() ) :
            while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                <div class="slide_div">
                    <div id="flip_wrap">
                        <div class="flip_outer">
                            <div class="vid_flip">
                                <div class="ab_flpr_inner">
                                    <div class="front_flip">
                                        <div class="sp_thumb">
                                            <?php the_post_thumbnail('full'); ?>
                                        </div>
                                    </div>
                                    <div class="back_flip">
                                        <h2>Watch Video</h2>
                                    </div>
                                </div>
                            </div>
                        </div>  
                    </div>
                    <div class="slide_cntnt">
                        <h2><?php the_title(); ?></h2>
                            <a class="pop_btn" href="<?php the_permalink(); ?>" onClick="popup('<?php echo $post->ID; ?>')">Click Me</a>
                        <div class="entry-content">
                            <?php //the_content(); ?> 
                        </div>
                    </div>
                </div>          
            <?php endwhile;
        endif; ?>
    </section> 

<?php
}

here is my ajax script and popup

    <div class="popup" id="page_content">
    <div class="popup_container black_bg">
        <a href="javascript:;" onClick="javascript:jQuery('#page_content').hide();" id="close">&times;</a>
        <div id="page_content_sec">
        </div>
    </div>
</div>  

there interface page which i am using for calling ajax. code here

<?php 
require_once("../../../wp-config.php"); 


if(isset($_GET['pageid'])) {
    $pageid = $_GET['pageid'];
} else {
    $pageid = "";
}

//echo $pageid;
$content_post = get_post($pageid);
    $content = $content_post->post_content; ?>
        <div class="pop_hd">
                <h2 style="color: #fff; text-align: center;"> Hafiz<?php echo get_the_title($pageid); ?></span></h2>

                <?php
                    $key_name = get_post_custom_values($key = 'Video_Link');
                    echo $key_name[0];
                ?>
                <?php get_post_custom_values('Video_Link', $post_id); ?>                    
        </div>

    <?php

die;
?>

Upvotes: 0

Views: 88

Answers (1)

maulik
maulik

Reputation: 1052

Your full code is un-properer for use of ajax in wordpress.
to use ajax in worpress functions.php & js script is needed, no need to create any new file to run ajax response & no need to include config file over there.
Check below basic code to work with ajax.

add_action('wp_ajax_FUNCTION-NAME', 'FUNCTION-NAME'); // Logged-in users
add_action('wp_ajax_nopriv_FUNCTION-NAME', 'FUNCTION-NAME'); // Guest users

function FUNCTION-NAME(){
    // your ajax response code.
}
  • add action & function will be in your functions.php file & order depended.

    And your script will call ajax like below.
<script>
    jQuery.ajax({
      type: "POST",
      url: ("<?php echo admin_url( 'admin-ajax.php' ); ?>"),
      data: ({
        action: FUNCTION-NAME,
        data : data
      }),
      success: function (response) {
        jQuery(".class-name").html(response);
        // "show/hide" event on response received.
      }
    });
</script>
  • in your code you have passed value in ajax code by $_GET to generate response which is not worked anyway. you have to use $_POST go get values to generate ajax response.

Upvotes: 1

Related Questions