0

I'm writing a plugin for my wordpress site and am having trouble understanding the jquery ajax requests. In a nutshell, I am trying to get some user meta data loaded when the page loads.

So in my javascript file I have a line:

$.getJSON("http://mysite.co.uk/wp-content/plugins/myplugin/ajax/ajax.php?action=test", function(json) { // do stuff });

So the above DOES get called, but it's the PHP code I'm having trouble with. In the file ajax.php, I am making a database call, but NONE of the usual class information is available. I've tried including the admin ajax page, various other pages from my own plugin (which does work).... not sure what else to do!

The database call method works OK in my plugin code.. but I guess that the ajax.php file is outside the WP framework so that's why it's not working... but I don't know how to get it IN the framework!?

I just need to use the $wpdb->get_results($sql); command to get my SQL. The error returned from firebug is that I am making a call to an undefined function.

Thanks for any help...

7
  • What did alert(json) give you? Commented Jun 14, 2010 at 22:03
  • actually, absolutely nothing. I've edited my PHP script to simply echo "hello"... that is returned as I can see the response in firebug (in firefox). Commented Jun 14, 2010 at 22:08
  • Did you check your error logs to see if PHP reported any errors? Commented Jun 14, 2010 at 22:09
  • well I know that it's a problem with PHP as my code returns the error. I simply need to know how to query the wordpress database outside of the framework... not sure what I am doing wrong! Commented Jun 14, 2010 at 22:15
  • @Matt - Is the JSON coming back valid? Check it here: jsonlint.com Commented Jun 14, 2010 at 22:16

1 Answer 1

1

Read this page, basically you only need to define a callback for an action and use the already defined Wordpress machinery.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks - I have read that page before, but never ACTUALLY REALLY read it.... Just spent some time and now I understand. I've hooked in to the admin as it suggested which is possibly working... just trying to establish a good json connection first, then will test the $wpdb class ... which I am sure will be ok to use. Many thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.