intense7
intense7

Reputation: 13

How to pass php variable to wordpress AJAX handler

Struggling to pass through some php variables into my ajax handler function in functions.php

Example provided below doesn't work, probably has something to do with the hooks but I can't find any info on how to do this:

/*Unsubscribe*/
$test_variable = "derp";

function user_unsubscribe($test_variable){
  echo json_encode($test_variable);
  wp_die();
};

add_action('wp_ajax_user_unsubscribe', 'user_unsubscribe');
add_action('wp_ajax_nopriv_user_unsubscribe', 'user_unsubscribe');

Upvotes: 0

Views: 1176

Answers (3)

Jaydeep Jagani
Jaydeep Jagani

Reputation: 74

You can pass that PHP variable in ajax data. Please check below files in which I had send the "test_variable" Value to Ajax Function from jQuery.

Jquery File Code

jQuery(document).ready(function($) {    
    $('#btn').on('click',function(){
      $.ajax({ 
           data: {action: 'get_listing_names','test': global.test_variable},
           type: 'post',
           url: global.ajax,
           success: function(data) {
            console.log(data);
          }
      });
    });
});

Functions.php file Code.

<?php
/**
 * Enqueue scripts and styles.
 *
 * @since 1.0.0
 */
function ja_global_enqueues() {

    wp_enqueue_script(
        'global',
        get_template_directory_uri() . '/js/global.js',
        array( 'jquery' ),
        '1.0.0',
        true
    );
    wp_localize_script(
        'global',
        'global',
        array(
            'ajax' => admin_url( 'admin-ajax.php' ),
            'test_variable' => 'Test Value',
        )
    );
}
add_action( 'wp_enqueue_scripts', 'ja_global_enqueues' );


add_action('wp_ajax_nopriv_get_listing_names', 'ajax_listings');
add_action('wp_ajax_get_listing_names', 'ajax_listings');


function ajax_listings() {

    $test_variable = $_POST['test_variable'];


    wp_send_json_success( $test_variable );        

}

Upvotes: 1

intense7
intense7

Reputation: 13

Solved it with this solution: can I pass arguments to my function through add_action?

Working code solution is:

/*Unsubscribe*/
$test_variable = "derp";

function user_unsubscribe($test_variable){
  echo json_encode($test_variable);
  wp_die();
};

add_action('wp_ajax_user_unsubscribe', function() use ($test_variable){
  user_unsubscribe($test_variable);
});

add_action('wp_ajax_nopriv_user_unsubscribe', function() use ($test_variable){
  user_unsubscribe($test_variable);
});

Upvotes: 1

janw
janw

Reputation: 6662

The prefered way to pass variables to ajax is to add them to the request and read them from $_GET or $_POST official documentation

If you need other variables you'll either have to use globals or call a extra function.

favorite

Struggling to pass through some php variables into my ajax handler function in functions.php

Example provided below doesn't work, probably has something to do with the hooks but I can't find any info on how to do this:

function user_unsubscribe(){
  $test_variable = get_test_variable();

  echo json_encode($test_variable);
  wp_die();
};

add_action('wp_ajax_user_unsubscribe', 'user_unsubscribe');
add_action('wp_ajax_nopriv_user_unsubscribe', 'user_unsubscribe');

function get_test_variable() {
    // here get/fetch your variable;

    /*Unsubscribe*/
    $test_variable = "derp";

    return $test_variable;
}

Upvotes: 0

Related Questions