Untested but gosh darn it might just work…
javascript file:
jQuery( document ).ready(function() {
jQuery.ajax({
url: // !! hardcode the URL of WP admin-ajax.php here, since you don't want to use wp_localize_script() to pass it in...
type: 'POST',
data:{
action: 'some_action', // this is the function (via wp_ajax_ hook) that will be triggered
},
success: function( data ){
var returned_data = JSON.parse(data);
}
}); // jQuery.ajax
});
php file:
wp_enqueue_script( 'my-ajax-js', 'url of my .js file', array('jquery'), "1.0.0", true);
function get_some_variables() {
$return_data = array("one","two","three","four");
echo ( json_encode($return_data) );
wp_die();
}
add_action( 'wp_ajax_some_action', 'get_some_variables' );
(EDIT) I should add..this is a LOT of overhead for a one-way trip to the browser for some php variables. I’m curious as to why you don’t want to just use wp_localize_script()
?
Another reckless cowboy way to get php variables into the DOM is to echo the JSON encoded string to a hidden <div>
, then use js to go pull the content out of that div. For small amounts of data that you don’t care if is exposed in plain-text…that works, too.