WordPress.org

Forums

Yet Another Related Posts Plugin (YARPP)
[resolved] YARPP Fatal Error in ajax context (4 posts)

  1. med_freeman
    Member
    Posted 1 year ago #

    yarpp: v4.0.6

    Hi,

    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;
    
     //ADDED TEST CODE
     echo $this->storage_class;
     if($this->storage_class == '') {
        echo 'Cache class reference is now empty';
     }
    
    l.29
     $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.

    Best

    http://wordpress.org/plugins/yet-another-related-posts-plugin/

  2. med_freeman
    Member
    Posted 1 year ago #

    Follow-up :

    I added the following line before the offending code :

    // ADDED LINE FOR AJAX COMPAT
      $this->storage_class = 'YARPP_Cache_Tables';
    l.29
     $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

  3. med_freeman
    Member
    Posted 1 year ago #

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

  4. Slams
    Member
    Posted 10 months ago #

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

    Thank you in advance!

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Yet Another Related Posts Plugin (YARPP)
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic