GD Star Rating
Using with AJAX on a custom post type (3 posts)

  1. Ross Wintle
    Posted 4 years ago #


    I'm trying to use GD Star Rating in a theme that uses AJAX to display objects from a custom post type.

    It's all set up and the AJAX mostly works fine, but I get a PHP error when I call the wp_gdsr_render_article() function from my loop when I'm using the AJAX.

    The error I get is:

    Fatal error: Call to a member function render_article() on a non-object in [...]\wp-content\plugins\gd-star-rating\code\fn\legacy.php on line 182

    I use exactly the same loop code within the AJAX as I do in a non-AJAX page display, and the stars work fine on the non-AJAX page.

    My guess is that something in the plugin is not being initialised when I call it through AJAX.

    I've tried including wp-load.php to see if that gets the plugin initialised correctly but this doesn't seem to help either.

    Any ideas how to get this working?




  2. Ross Wintle
    Posted 4 years ago #

    A quick follow up.

    So the problem is that to load my "posts" using AJAX I make a call to wp-admin/admin-ajax.php and pass my action and parameters. I then use an action hook to produce my dynamic lists:

    add_action('wp_ajax_load_resources', 'im_load_resources' );
    add_action('wp_ajax_nopriv_load_resources', 'im_load_resources' );

    I've been playing with this and looking at the plugin code. It looks like this is happening because the frontend.php isn't being loaded. The problem is with the plugin loading. In gd-star-rating.php there is a test that forks one way for admin requests and another way for non-admin requests:

    if (!defined("WP_ADMIN") || (defined("WP_ADMIN") && !WP_ADMIN))

    There must be other tests like this also as just changing this test was not sufficient to fix the problem. What I did was convince WordPress not to set WP_ADMIN for my specific call that I was making through admin-ajax.php. This helped, but I then also had to spit out a wp_head() with my AJAX response in order to allow the voting to work.

    Obviously, I seem to be alone in making this request, but it would be really handy for me if I could use the plugin to at least render read-only ratings when making a call through /wp-admin/admin-ajax.php.


  3. rzepak
    Posted 4 years ago #

    Hi magicroundabout

    I'm facing the same problem right now.. I've got ajax call to file with loop, from where I want to load post and display ratings.

    I've got error: Fatal error: Call to a member function render_article() on a non-object in \gd-star-rating\code\fn\legacy.php on line 170

    Did you manage to find a way to solve your problem?


Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic