Wordpress plugin callback function return 0
i'm looking for an answer for my problem since days ...
Here is my problem. I'm making a wordpress plugin using jquery and ajax. For the moment i'm just trying to make a web example working :)
The callback function, returns everytime 0
Here is my whole code, for my wordpress admin page:
<?php
add_action('wp_ajax_join_mailinglist', 'join_mailinglist');
add_action('wp_ajax_nopriv_join_mailinglist', 'join_mailinglist');
function join_mailinglist() {
$email = $_POST['email'];
if(!empty($email)) {
$yourEmail = 'xxx@xxx.com';
$subject = 'Add me to your mailing list';
$success = mail($yourEmail, $subject, $email);
if(!empty($success)) {
echo 'Your email is subscribed to our mailing list.';
} else {
echo 'There was a problem. Please try again.';
}
}
die();
}
?>
<div class="wrap">
<div id="icon-options-general" class="icon32"><br /></div>
<form method="post" id="mailinglist" action="">
<div id="message"></div>
<input type="text" name="mailinglistemail" id="mailinglistemail" />
<input type="submit" name="submit" id="mailinglistsubmit" value="Join"开发者_StackOverflow中文版 /><img src=" <?php admin_url(); ?>/wp-admin/images/wpspin_light.gif" alt="" class="ajaxsave" style="display: none;" />
</form>
<script type="text/javascript" >
jQuery(document).ready(function(){
jQuery("#mailinglist").submit(function() {
if(jQuery("#mailinglistemail").val()=="") {
jQuery("#mailinglist #message").text("Please enter your email address.");
return false;
} else {
var email = jQuery('#mailinglistemail').val();
if(email.indexOf("@") == -1 || email.indexOf(".") == -1) {
jQuery("#mailinglist #message").text("Please enter a valid email address.");
return false;
} else {
var data = {
action: 'join_mailinglist',
email: email
};
//alert('ici');
jQuery("#mailinglistsubmit").hide();
//alert('la');
jQuery(".ajaxsave").show();
//alert('now');
jQuery.post("<?php echo admin_url('admin-ajax.php'); ?>", data,
function(response){
jQuery(".ajaxsave").hide();
jQuery("#mailinglistsubmit").show();
jQuery("#mailinglist #message").html(response);
alert('The server responded: ' + response);
});
return false;
}
}
});
});
</script>
</div>
Thanks for your help. Yann
Not sure if this is your problem, but I was getting a response of just "0" and it took me the longest time to figure it out. Aside from making sure you put exit
or die
at the end of your php function, I found out that you have to put your add_action('wp_ajax_...
bit at the top level of your plugin. I had it nested inside my admin_menu
hook, which didn't work.
maybe you have to add '_callback'
add_action('wp_ajax_join_mailinglist', 'join_mailinglist');
like this
add_action('wp_ajax_join_mailinglist', 'join_mailinglist_callback');
I got stuck into this for a day or two, then i've found that i forgot to include a json_encode in the callback function.
In .js, located on plugin's directory, i wrote this:
jQuery.ajax({
type: 'POST',
dataType: 'json',
data:{
'action': 'do_ajax',
'func': 'yell_out',
'var': 'hi there'
},
beforeSend: function(x) {
if(x && x.overrideMimeType) {
x.overrideMimeType("application/json;charset=UTF-8");
}
},
url: ajax_url,
success:function(data){
alert('success: ' + data);
},
error: function(errorThrown){
alert('oh no: ' + errorThrown);
});
}
For the php part, this:
add_action('wp_ajax_do_ajax', 'do_ajax_callback');
function do_ajax_callback(){
switch($_REQUEST['fn']){
case 'yell_out':
$output = yell_out($_REQUEST['id']);
break;
default:
$output = 'No function specified, check your jQuery.ajax() call';
break;
}
$output = json_encode($output);
if(is_array($output)){ print_r($output); }
else{ echo $output; }
die();
}
function yell_out($string){
return $string;
}
精彩评论