WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Plugin Slows Down Page Load of Blog, Please Help (7 posts)

  1. golddave
    Member
    Posted 7 years ago #

    I created a plugin that parses an XML file on another site, pulls out a small bit of information and displays it on a blog where the plugin function is called. The plugin works but for some reason slows the page load of the blog tremendously. I'm not a very advanced plugin coder (or PHP coder for that matter) and was wondering if anybody would care to look over my code and help me figure out what the problem is and how I may be able to remedy it.

    The plugin parses the baseball standings found at http://www.erikberg.com/mlb/standings.xml and pulls out the magic number for the team of your choice (default is my Mets). If your chosen team is not in first place (and hence has no magic number) the plugin does nothing.

    The code may be found at the following URL:
    http://www.golddave.com/help/MagicNumber2.txt

    Any help would be greatly appreaciated.

    Thanks in advance,
    Dave

  2. leflo
    Member
    Posted 7 years ago #

    The problem usually is, that your site needs to load the XML data from http://www.erikberg.com/mlb/standings.xml and that causes a big delay.

    What you need is caching. If you have cron jobs on your server use one for downloading the XML file every so or so minutes and just parse and deliver the output (or even parse in the cronjob too and just pipe it through).

    Another attempt would be to use AJAX to load the data later (as seen in the plugin Scrobbler).

    HIH

  3. davidchait
    Member
    Posted 7 years ago #

    Yep, caching. CG-FeedRead does it, so does the built-in Magpie RSS code. Not hard to do, just write the results of your retrieval to a file, when you go to run the function again, check if the file exists.. if it doesn't, or if it does but more than X hours have passed, go retrieve the XML and parse again. if it does exist, and isn't outdated, just open the file and spit out the contents again (which might be blank, or might be html...). actually, if you output as html, you can check the timestamp and if it's okay just do an include(file) sort of thing...

    -d

  4. golddave
    Member
    Posted 7 years ago #

    Thank you to everyone for your help. I have fixed the problem. The remote XML was part of it so rather than access it from the remote site I now copy it down to my server. I have a function that checks how old the XML is and downloads accordingly so that I'm not downloading every time the plugin is called.

    Thanks again.

  5. davidchait
    Member
    Posted 7 years ago #

    Still, no need to parse the XML every time the plugin runs (that's not a simple bit of code...)... Cache the result to a .html file, and when you re-download the XML, run the parse function and re-cache the .html, then just include the html into your page/sidebar/whatever.

    -d

  6. golddave
    Member
    Posted 7 years ago #

    Very good idea. I just have to figure out hor to cache it to the .html file and I'm in business. Any ideas? (I'd settle for pointing me towards a tutorial if you'd rather not give me all the gory details yourself.)

    Thanks.

  7. davidchait
    Member
    Posted 7 years ago #

    assuming the file exists or you have create access to the directory, something like:

    $html = "<b>this is some html</b>nIsn't this cool?";
    $hfile = fopen($pathtoresults."myresults.htm", "w");
    if ($hfile)
    {
    fwrite($hfile, $html);
    fclose($hfile);
    }

    That's off the top of my head, just a basic example. And obviously, you need to point to a path on the server. you can use your wp-root, assuming it's simple HTML that's fine. Or, you can use wp-root/wp-content/ as another safe alternative.

    -d

Topic Closed

This topic has been closed to new replies.

About this Topic