How to call ajax in wordpress

后端 未结 7 528
青春惊慌失措
青春惊慌失措 2020-11-28 10:49

My ajax call output is always showing 0 as output don\'t know why

In functions.php I have this code

function get_data() {
    $abc = \'         


        
7条回答
  •  误落风尘
    2020-11-28 11:08

    Step 1: Add ajax 'wp_enqueue_script' file in function file where you have to add other 'wp_enqueue_script' or 'wp_enqueue_style' files

    wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/my-ajax- script.js', array('jquery') );
        wp_localize_script( 'ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
    

    Step 2:Now you need to to create function, where you want to get response,using ajax e.g below

     add_action('wp_footer','add_ajaxex_in_footer');
       function add_ajaxex_in_footer()
        { ?>
    
    

    Step 3: Now you have to create function where you have to write query,

    add_action('wp_ajax_my_special_ajax_call_enroll_cours', 'enroll_cours');
    add_action('wp_ajax_nopriv_my_special_ajax_call_enroll_cours', 'enroll_cours');
    
     function enroll_cours()
      { 
          echo "Here you van write Query or anything"; 
       }
    exit;
    }
    

    => If you want fire ajax request after onClick button,just pass the button ID

    
    

提交回复
热议问题