• Hi,

    I was wondering if anyone could help me.

    I am trying to write a plugin and as part of the plugin I need to query the database using jQuery/Ajax from the front side of the website but im not sure how I write this in my plugin.

    I have read the following page Ajax in Plugins but im not sure how to do it.

    If some one could help me out that would be much appreciated.

    Many Thanks
    Pete

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi Pete, the codex page you linked also links to this: https://www.wphardcore.com/2010/5-tips-for-using-ajax-in-wordpress/

    Although it is counter-intuitive, you do need to be sending your requests from the javascript to the admin-ajax.php file. The hooks you define will be called from there, and from there your functions will be called.

    Example Hooks:

    // this hook is fired if the current viewer is not logged in
    add_action( 'wp_ajax_nopriv_myajax-submit', 'myajax_submit' );
    add_action( 'wp_ajax_myajax-submit', 'myajax_submit' );

    The hook/action name is determined by the action field in the ajax request. So if your action is called “myajax-submit” you’d use the two actions above. The second argument is the name of the function to call in your plugin.

    These are about as easy as the examples can be, i personally like the examples shown here.
    https://ocaoimh.ie/make-your-wordpress-plugin-talk-ajax/

    The above is also one of the links at the bottom of the Ajax codex page.

    If you have problems implementing your ajax function, post your code into a pastebin and link it back here for us to look at..

    guar I spent around 10hours on the https://www.wphardcore.com/2010/5-tips-for-using-ajax-in-wordpress/
    but with no luck.
    If you are not an experienced wordpress that article is of no use, because it addresses experienced users.
    The article does not mention explicitly all the files that are impacted nor does it have a full source code example.

    I left a polite question to the author to add an full example, but my request was not even admited on that blog page…

    I’ll try Mark’s advice and post back here.

    Thanks

    Did anyone get anywhere with this?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Database query with Ajax’ is closed to new replies.