WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i would like to call a php page from an Ajax method : the alert works fine, the code as well has been tested outside of wordpress, but here it seems that the php page is never called.

I found some articles, among them : How to manage ajax calls and JSON in wordpress but i get more confused. Can you please tell me what should i do here? (with "diapo.php" it doesn't work, and like that neither)

here is the call with jquery :

                var alt_p = $(this).attr('alt');

                    type: "POST",
                    url: "<?php bloginfo( 'template_directory' ) ?>/diapo.php",
                    data: {p:alt_p},
                    success: function(data) {
                return false;

and the php page for now :

        $p = $_POST['p'];
        query_posts( 'cat=3&p=$p' );
        while (have_posts()) : the_post();
            $result = the_post_thumbnail('normal');

        echo '$result';


Thanks for your help

share|improve this question
Unless you are using PHP to output your JS then your JS won't do jack as it doesn't have the full URL. Hard code the URL in and see if that works. – Cristian May 31 '11 at 21:03

To use the WordPress ajax url you can pass the var using wp_localize_script:

wp_enqueue_script( 'functions', get_bloginfo( 'stylesheet_directory' ) . '/js/functions.js', array( 'jquery' ), false);
    wp_localize_script( 'functions', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );

In your functions.js

                type: "POST",
                url:  MyAjax.ajaxurl,
                data: {p:alt_p},
                success: function(data) {
share|improve this answer
Thanks, isn't normally the third parameter the "data" to send to the php page? here it seems to me that we send the php to js? so here, if i'd like to write 'echo "my_string"' for example to be returned, how can i implement it? create a plugin, and use the hook add_action('admin-ajax', ...) ? – Paul_p Jun 3 '11 at 17:08

Calling PHP file directly is more generic way to implement Ajax and should not be used in WordPress.

See Ajax in Plugins in Codex and try to slowly work through examples there. Yes, it's not the easiest stuff if you aren't very experienced in WP, but that is how Ajax should be done in it.

share|improve this answer
Hi, thanks, can you help with this a little bit? i already wrote the path to the js file, that works with other jquery stuff, so can i let all the ajax code inside it? or do i write it in my specific "plugin" for ajax? Also, i don't really understand the codex example : i don't want to do anything with the "admin" area, since the ajax would be to show some images on the front page,... so i don't really understand the code, with " if (is_admin) else { } ", could you help me with a very basic example? – Paul_p Jun 1 '11 at 0:01
@Paul_p yes, you need to write some WP-specific code to implement Ajax in it. Codex covers front-end as well as back-end. Unfortunately Codex is about as easy of example as it gets. Try just copy-pasting and making Codex examples works first for you. – Rarst Jun 1 '11 at 7:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.