Home  >  Article  >  Backend Development  >  Why is my WordPress AJAX call returning \'0\' despite having an echo statement in the PHP function?

Why is my WordPress AJAX call returning \'0\' despite having an echo statement in the PHP function?

Barbara Streisand
Barbara StreisandOriginal
2024-10-30 19:38:03456browse

Why is my WordPress AJAX call returning '0' despite having an echo statement in the PHP function?

How to Invoke Ajax in WordPress

Problem

An AJAX call consistently yields '0' as its output, leaving the developer puzzled. The functions.php file contains the following code:

<code class="php">function get_data() {
    $abc = '1';
    $result = $wpdb->get_results("SELECT * FROM " . $wpdb->options . " WHERE option_name LIKE '_transient_%'");
    echo $result; // Outputting this value but still displays 0
    wp_die();
}

add_action('wp_ajax_nopriv_get_data', 'get_data');
add_action('wp_ajax_get_data', 'get_data');</code>

Meanwhile, the AJAX call is made via JavaScript:

<code class="javascript">$('body').on("click", ".re-reset-btn", function(e) {

    var panel = $('#re-compare-bar');

    $.ajax({
        type : "GET",
        dataType : "json",
        url : "/wp-admin/admin-ajax.php",
        data : {action: "get_data"},
        success: function(response) {

            alert("Your vote could not be added");
            alert(response);
        }
    });

    $("#re-compare-bar-tabs div").remove();
    $('.re-compare-icon-toggle .re-compare-notice').text(0);

});</code>

Despite the attempt to output the variable $abc, the result remains '0'.

Solution

In WordPress, the global variable ajaxurl is provided by default for AJAX calls. However, this variable is not defined in the frontend. To utilize AJAX calls in the frontend, you need to define ajaxurl manually.

A recommended approach is to employ wp_localize_script. Consider that your AJAX calls reside in "my-ajax-script.js", add wp_localize_script as follows:

<code class="php">function my_enqueue() {
      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' ) ) );
 }
 add_action( 'wp_enqueue_scripts', 'my_enqueue' );</code>

Once your JS file is localized, you can access the my_ajax_object object in your JS file and modify your AJAX call accordingly:

<code class="javascript">jQuery.ajax({
    type: "post",
    dataType: "json",
    url: my_ajax_object.ajax_url,
    data: formData,
    success: function(msg){
        console.log(msg);
    }
});</code>

The above is the detailed content of Why is my WordPress AJAX call returning \'0\' despite having an echo statement in the PHP function?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn