Link to home
Start Free TrialLog in
Avatar of gplana
gplanaFlag for Spain

asked on

Problem with Wordpress and AJAX

Hi.

I have a Wordpress site and I want to refresh some data when user click on a link, but without reloading the full page, so I'm trying to use AJAX in Wordpress. I have added this code to functions.php file:

do_action('wp_ajax_nopriv_poll_results','show_poll_results');
do_action('wp_ajax_poll_results','show_poll_results');

function show_poll_results() {
   echo 'Hey!';
}

Open in new window


And also I have this snipped of code on my page:

	<div id="sidebar-poll-results" class="widget mainbox">
<?php show_poll_results(); ?>
	</div><!-- end #side-leaders -->

Open in new window


and finally I have this jQuery code on the same page:
	        jQuery('#poll_view_results', this).click(function() {
			show_results();
			return false;
	        });

Open in new window


and finally I have this code that mades the ajax call:

	function show_results() {
		$.post("/wp-admin/admin-ajax.php", {'action':'poll_results'}, function(response) {
			$('#sidebar-poll-results').html(response);
		});
	}

Open in new window


When page is loaded, contents appear well (it appears the "Hey!" message, so php show_poll_results functions is working, but, after clicking on the link #poll_view_results, response variable of the ajax callback functions is getting a "-1" instead of "Hey!". Does anybody knows why this could happen ?

Thanks
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

What do you get if you browse to
/wp-admin/admin-ajax.php?action=poll_results?
Avatar of gplana

ASKER

Hi julianH. Thanks for your answer.

I'm getting "-1" (without quotes), so just 2 characters, looking at source html code, so I'm not getting html but this 2 characters.

So it looks like show_poll_results function is not beign executed. Maybe the hook addition I made on functions.php is not ok ?
It would seem that way - your show_poll_results is being executed at page render time so the function is working - but your call back to the function is not - the URL rules out a GET / POST issue and also indicates that the action is not being wired correctly.
Avatar of gplana

ASKER

Thanks. I'm not sure if I fully understand. Do you mean the problem is on do_action() or on ajax call ?

and what can be the solution ?

Thanks.
The problem is not the AJAX call.

Here is what we know

1. When you load the page - hey is displayed - so the show_poll_results function is successfully called when the page is first generated.

2. When you access the link directly (the link used by the AJAX call) you get a -1 (or -2) result

3. You get the same result when you call from AJAX - which is obvious.

From what you have posted there is nothing wrong with your linking the click to the AJAX call or the AJAX call itself - the problem is therefore in how the event in Word Press for the action is being wired to the show_poll_results - you need to look there to solve the problem

The key thing is to get this URL to work when typed into the browser address bar
/wp-admin/admin-ajax.php?action=poll_results?

Once that works - then the AJAX call should work .
Avatar of gplana

ASKER

Ok, thanks. But I read do_action is the right way to link my function to a wordpress event, and wp_ajax_nopriv_xxx and wp_ajax_xxx are the events to use for ajax call with action param = xxxxx; so I don't understand what's wrong here.

You give me the clue of the problem, but can you or anyone else explain me what is wrong with these wordpress hooks ?

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gplana

ASKER

Thank you very much. You solved my problem. I should use add_action instead of do_action. This was the problem. Now I 'm getting some other errors, but the function is beign executed.