{"id":2416,"date":"2012-06-19T16:15:56","date_gmt":"2012-06-19T15:15:56","guid":{"rendered":"http:\/\/www.simonbattersby.com\/blog\/?p=2416"},"modified":"2012-06-19T16:15:56","modified_gmt":"2012-06-19T15:15:56","slug":"wordpress-wp_ajax-returning-0-error","status":"publish","type":"post","link":"https:\/\/www.simonbattersby.com\/blog\/2012\/06\/wordpress-wp_ajax-returning-0-error\/","title":{"rendered":"WordPress wp_ajax returning 0 error"},"content":{"rendered":"<p>This morning I spent a little time constructing an ajax call from a front-end page (not an admin page) using wp_ajax. As is my wont, I tested this in Firefox and all was well. I used the <a href=\"http:\/\/codex.wordpress.org\/Plugin_API\/Action_Reference\/wp_ajax_%28action%29\">WP codex<\/a>.<\/p>\r\n<p>Then I tried it in Opera and it failed, no response whatsoever, just a 0 in the ajax response. Seemed odd that Opera should have a problem, tried it in Chrome, same thing. Lots of Googling later, saw loads of similar questions being asked, no answers&#8230;<\/p>\r\n<p>I then looked at the code in wp_ajax, and all became clear&#8230;<\/p>\r\n<p>I had entries in my <code>functions.php<\/code> as follows:<\/p>\r\n<pre> add_action('wp_ajax_myfunction', 'myfunction');\r\n \r\n function myfunction() {\r\n    <span class=\"code_comment\">\/\/stuff\r\n<\/span> }<\/pre>\r\n<p>So if I pass an action of &#8216;myfunction&#8217; to wp_ajax it should call the hook at the top of the above code, which in turn calls myfunction(), right? <strong>Only if you are logged on to WordPress at the time!<\/strong> If you aren&#8217;t logged in, wp_ajax calls the hook <code>wp_ajax_<strong>nopriv<\/strong>_myfunction<\/code>. And that&#8217;s why it worked for me in Firefox, because I was also logged on through Firefox. Nowhere is this mentioned in the codex, that I can see.<\/p>\r\n<p>Having identified this it&#8217;s then simple to add another hook to your <code>functions.php<\/code>:<\/p>\r\n<pre> add_action('wp_ajax_nopriv_myfunction', 'myfunction');<\/pre>\r\n<p>Hope this saves someone an hour or so&#8230;<\/p>","protected":false},"excerpt":{"rendered":"<p>This morning I spent a little time constructing an ajax call from a front-end page (not an admin page) using wp_ajax. As is my wont, I tested this in Firefox and all was well. I used the WP codex. Then I tried it in Opera and it failed, no response whatsoever, just a 0 in [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[10],"class_list":["post-2416","post","type-post","status-publish","format-standard","hentry","category-web-design-and-build","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/posts\/2416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/comments?post=2416"}],"version-history":[{"count":0,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/posts\/2416\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/media?parent=2416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/categories?post=2416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/tags?post=2416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}