I'm trying to use a function in wordpress for both direct and ajax implementation but it doesn't seem to work because it has arguments. When I remove the arguments from the function, the ajax call works fine but with them I get a "500 (internal server error).
PHP:
add_action('wp_ajax_nopriv_example_function', 'example_function');
add_action('wp_ajax_example_function', 'example_function');
function example_function($foo) {
if (is_null($foo)) {
$foo = $_POST["foo"];
}
echo $foo;
}
JS:
$(document).on("change", "select", function(){
$foo = 'test';
$.ajax({
ajax_object.ajax_url,
type: 'post',
data: { action: 'example_function', foo: $foo },
success: function(html) {
$(div).append(html);
}
});
});