Reputation: 1523
I have some custom code and I am not sure how to catch that Post request.
<?php $nonce = wp_create_nonce('nrc_update_certifications_nonce');?>
<?php $link = admin_url('admin-ajax.php?action=nrc_update_certifications&nonce=' . $nonce); ?>
<file-upload
extensions="jpg,jpeg,png,pdf"
:accept="accept"
:multiple="true"
post-action="<?php echo $link;?>"
:data="{
types: accept,
certifications_ids: certifications_ids,
}"
v-model="certifications"
name="certifications[]"
@input-filter="inputFilter"
ref="upload">
<span class="button">Select files</span>
</file-upload>
In my child theme I have a file update-certifications.php. This file is imported in functions.php
function nrc_update_certifications() {
// I don't get here!
if ( !wp_verify_nonce( $_REQUEST['nonce'], "nrc_update_certifications_nonce")) {
exit("No naughty business please");
}
exit ("Works!");
}
add_action('wp_ajax_update_certifications', 'nrc_update_certifications');
Upvotes: 0
Views: 235
Reputation: 1716
The correct way to do that is the following:
function nrc_update_certifications() {
// I don't get here!
if ( !wp_verify_nonce( $_REQUEST['nonce'], "nrc_update_certifications_nonce")) {
exit("No naughty business please");
}
exit ("Works!");
}
add_action('wp_ajax_nrc_update_certifications', 'nrc_update_certifications');
What changes is that when you do add_action the action starts with "wp_ajax_" and it MUST be followed by what you specify in the action parameter of your call. Since you build the link like this:
<?php $link = admin_url('admin-ajax.php?action=nrc_update_certifications&nonce=' . $nonce); ?>
Then the missing part after wp_ajax_ must be "nrc_update_certifications". What you are free to change is the function specified as the second parameter of add_action
Sidenote: if you want that ajax to be available for non-logged user aswell, then you are missing another call to add action which should be:
add_action( 'wp_ajax_nopriv_nrc_update_certifications', 'nrc_update_certifications' );
See the "nopriv" part of the action. If you want to know more i suggest you to take a look at this: https://codex.wordpress.org/AJAX_in_Plugins
Upvotes: 1