This is probably a palm slap question, but I'm confused.
I created what I thought was a simple WP template page. A form to update a form to a db via ajax callback. At the bottom I placed the add_action() statements for the callback function to fire when the form submits via WP-AJAX.
I quickly realised that the callback function (the functions in the add_action() statements) were not firing. The response code from the 'done' function in my jQuery was 0... which means WP-AJAX couldn't hook into the callback function.
add_action('wp_ajax_updateuserprofileAction', 'updateuserprofileAction');
add_action('wp_ajax_nopriv_updateuserprofileAction', 'updateuserprofileAction');
function updateuserprofileAction($args) {
$nonce = $_POST['send'];
if ( !wp_verify_nonce($_POST['send'],'jch-ajax-update-userprofile') ) {
echo $nonce;
die ( $nonce ); }
// Read the form values
$success = false;
$senderFirstName = isset( $_POST['senderFirstName'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['senderFirstName'] ) : "";
$senderLastName = isset( $_POST['senderLastName'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['senderLastName'] ) : "";
$senderEmail = isset( $_POST['senderEmail'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['senderEmail'] ) : "";
// If all values exist, send the email
if ( $senderFirstName && $senderLastName && $senderEmail) {
global $current_user;
get_currentuserinfo();
update_user_meta( $current_user->ID, 'phone', $_POST['senderPhone']);
if($success == 1)
{
die('success');
}
}
else {
die( 'ERROR');
}
}
Out of frustration, I moved the callback function and the add_action statements to a plug-in which also uses WP-AJAX and immediately... it works.
So my question is: why? Why can't you put add_action() statements into a template page? Or to put it another way: what are the 'rules' for where you can place add_action()?
Ideally, I'd like to keep the add_action() and the callback functions in the same file as the template page. Is there a way to do this?