Support » Plugin: Yet Another Related Posts Plugin (YARPP) » YARPP Fatal Error in ajax context

  • Resolved med_freeman


    yarpp: v4.0.6


    first, thanks for your great work !

    i’m loading the body of specific pages via ajax instead of regular full page request, and that worked perfectly until i enabled yarpp.

    yarpp works in standard context, but i now have a php fatal error in my xhr response (ajax) :

    Fatal error: Class name must be a valid object or a string in /home/www/260a3a65240797a4ffcfe1fda27207fd/web/test/wordpress/wp-content/plugins/yet-another-related-posts-plugin/class-core.php on line 29

    To diagnose, i added this code before the offending line (l.29) :

    // load cache object
     require_once(YARPP_DIR . '/class-cache.php');
     require_once(YARPP_DIR . '/cache-' . YARPP_CACHE_TYPE . '.php');
     $this->storage_class = $yarpp_storage_class;
     echo $this->storage_class;
     if($this->storage_class == '') {
        echo 'Cache class reference is now empty';
     $this->cache = new $this->storage_class( $this );

    In standard context i only have an echo of ‘YARPP_Cache_Tables’ in my pages.

    In ajax context i have again ‘YARPP_Cache_Tables’, but then ‘Cache class reference is now empty’.

    So it seems $this->storage_class loses its value the second after it is initialized with the correct value ‘YARPP_Cache_Tables’.

    I can’t seem to find why…

    Just to be a little more precise, ajax context in wordpress goes via admin, so a call to is_admin() in an ajax request would return true.


Viewing 3 replies - 1 through 3 (of 3 total)
  • med_freeman


    Follow-up :

    I added the following line before the offending code :

      $this->storage_class = 'YARPP_Cache_Tables';
     $this->cache = new $this->storage_class( $this );

    That allows the plugin to work with my ajax request, since the storage class variable is this time correctly defined.

    In this context, related posts are displayed TWO times at the end of the post.

    I’m guessing YARPP is instantiated two times.

    I have a fix for this in yarpp.php (l.40) :

    // new in 3.3.3: init yarpp on init
    add_action( 'init', 'yarpp_init' );
    function yarpp_init() {
    	global $yarpp;
    	if(!isset($yarpp)) {
    		$yarpp = new YARPP;

    Just enclosed $yarpp = new YARPP between if(!isset($yarpp)) { and } to prevent multiple instantiation.

    Since the problem probably comes from me (not really sure though), i won’t ask for this fix, but since it doesn’t do any harm, you can add it if you don’t mind.

    Thanks a lot



    my bad, i was initializing wordpress two times…..

    Could you share your code in which way you get YARPP block in your content? Php handler and JS.

    Thank you in advance!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘YARPP Fatal Error in ajax context’ is closed to new replies.