Reputation: 145
I am using a third-party plugin to allow the creation of a post from the front-end of the site. Basically I want the functionality that users can create a post, and I can in the admin backend approve the post or not. However tt has an AJAX uploader that allows you to upload a featured image, but this only works when you're logged in. I see that the ajax call go through wp-admin/admin-ajax.php and to the line:
do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
I can't see how this restricts the upload or how I enable the upload through this AJAX call.
Any help appreciated. Thanks
Upvotes: 4
Views: 3083
Reputation: 5937
Ajax works a bit differently with wordpress. Instead of sending information by ajax to a url for a function to pick up, you send it to "/wp-admin/admin-ajax.php" and it fires the wp_ajax hook or in the case of frontend, wp_ajax_nopriv hook.
When this file receives information, it will match the action: variable in your ajax function with the actions added to the wp_ajax_nopriv_ hooks in the case of frontend or wp_ajax in the case of backend.
To be clear this is only the server side code. You will need jQuery to trigger an action (e.g. button is pressed) and then to send the information to the action and retrieve the output. e.g
jQuery('#image').click(function() { // fires when some element with id #image is click on
jQuery.ajax({
type: "POST",
url: "/wp-admin/admin-ajax.php",
data: {
action: 'sample_ajax_function',
reg: regnum, // any other data you want to send?
maketaxonomy: '<?php echo $maketaxonomy;?>'
},
success: function (output) { // if data is returned from your function you can pick it up using output now.
console.log(output); //log it to your console so you can see what is returned.
},
});
});
and then in your functions file.
add_action('wp_ajax_nopriv_sample_ajax_function', 'my_ajax_function');
function my_ajax_function() {
$reg= $_POST['reg'];
echo $reg; //output something.
exit; // no more output
}
if your looking for a method you can use to upload images have a look here Html multiple file input - javascript access and delete files .
Upvotes: 3