i'm trying to use ajax with a simple example, i took the example from : http://ocaoimh.ie/2008/11/01/make-your-wordpress-plugin-talk-ajax/ example N°5 (which doesn't work actually as it is)

So i'm trying just to get the result from the php page, located in /wp-content/ajax-php.php, but nothing, except the alert, seems to react. Could you have a look and tell me if you see something wrong? :

            Plugin Name: ajax with images
            Plugin URI: http://ocaoimh.ie/
            Description: A simple hello world plugin, taken from <a href="http://jquery.bassistance.de/jquery-getting-started.html#rate">here</a>
            Version: 0.1
            Author: Donncha O Caoimh
            Author URI: http://ocaoimh.ie/

            function helloworld5_scripts() {
                    global $current_user;
                    if( !$current_user->ID )
                    if( is_single() ) {
            add_action( 'wp_print_scripts', 'helloworld5_scripts');

            function helloworld5_head() {
                    global $current_user;
                    if( !$current_user->ID )
                    if( !is_single() )
            <script  type='text/javascript'>
            var currentpost = '';
            jQuery(document).ready(function() {
                    // generate markup
                    var ratingMarkup = ["Please rate: "];
                    for(var i=1; i <= 5; i++) {
                            ratingMarkup[ratingMarkup.length] = "<a href='#'>" + i + "du-texte-pour-tester</a> ";
                    // add markup to container and applier click handlers to anchors
                    jQuery("#rating").append( ratingMarkup.join('') ).find("a").click(function(e) {
                            // send requests
                            jQuery.post("/wp-content/ajax-php.php", {rating: jQuery(this).html()}, function(data) {
                                    jQuery("#rating").append(data+' : hello ');
                            } );
                            return false;
            // -->
            add_action( 'wp_head', 'helloworld5_head' );

            function add_thumb_to_post( $content ) {
                    global $post;
                    global $current_user;
                    if( !$current_user->ID )
                            return $content;
                    if( !is_single() )
                            return $content;
                    return $content . "<p><div id='rating'></div></p><script type='text/javascript'>\n<!--\ncurrentpost='{$post->ID}';\n//-->\n</script>";
            add_action( 'the_content', 'add_thumb_to_post' );


1 Answer 1


Your code is technically correct, though you should be doing ajax calls via the WordPress provided means. Also, your path to ajax-php.php will fail if WP isn't installed in root.

What does ajax-php.php contain and what are you expecting it to return? If you're trying to use WordPress functions inside of that file, you need to bootstrap WordPress by including wp-load.php. But again, you probably shouldn't be doing it that way in the first place, see the link above.

  • 2
    Also Ajax in Plugins in Codex is a must read.
    – Rarst
    Jun 1, 2011 at 21:30
  • Thanks, i don't know how to use a basic code in the front-page, actually i understand this : cdn.ocaoimh.ie/wp-content/uploads/2008/11/helloworld4.txt which is to use Ajax in the admin area, but how should i do the same for a front-page? Simply add 'nopriv' ? the goal would be to click on "next" or "previous" and have the different post thumbnails in Ajax. Can you help me understand it?
    – Paul_p
    Jun 1, 2011 at 22:52

Your Answer

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

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