Updated: August 27th, 2012

Today's snippet is tremendously helpful if you are using an XML-RPC WordPress interface to read and publish your articles and are running into 500 Server Error issues due to running out of memory, manifesting themselves in something like this error message: "Invalid Server Response – The response to the metaWeblog.newMediaObject method received from the weblog server was invalid".

For example, my regular PHP memory allocation is 32MB or so, but if I load up Windows Live Writer, my favorite publishing tool, and ask it to load 1000 of the latest blog posts, I will undoubtedly get a server error back.

One solution would be to increase the memory allocated to PHP to something higher, like 256MB, which is how I used to get around the issue. I say "get around" because it's not a good solution – if someone finds a page that uses a lot of memory on your site, they could easily kill not only your web server but the whole machine due to swap death. Keeping a lower memory limit allows you to run your web server, such as Apache, with more children, thus serving more requests without getting overloaded.

So, I've looked into the WordPress core and came up with what I think is a proper fix – dynamic memory limit tweaking when dealing with XML-RPC only. Here is the code – add it to your functions.php and you should be golden:

* Dynamically increase allowed memory limit for XML-RPC only.
* @param array $methods
* @return array
function my_xmlrpc_methods($methods) {
  ini_set('memory_limit', '256M');
  return $methods;
add_action('xmlrpc_methods', 'my_xmlrpc_methods');

The code is pretty self-explanatory: it hooks into a WordPress hook that fires only for XML-RPC requests and adjusts the memory limit on the fly. No more out of memory errors and I'm able to load 1000 posts in Windows Live Writer without problems. If you want to load more, and PHP still crashes, try to raise this limit, but be careful not to send the server into swap death spiral.

● ● ●
Artem Russakovskii is a San Francisco programmer and blogger. Follow Artem on Twitter (@ArtemR) or subscribe to the RSS feed.

In the meantime, if you found this article useful, feel free to buy me a cup of coffee below.

  • marcel

    Hi Artem,
    You have to have a pretty flexible host for this to work. Most 'cheap' hosts won't allow you to temporarily increase the memory limit.

  • Scott Leonard

    At first, I was speechless.
    An incredible solution.

    And it is, however some will have difficulty implementing the solution, as mentioned by Marcel.

    Today was my first visit to Beerpla.net and read some of the articles. Love the site, as well as your creative approach to various areas of technology. 5 Stars!

  • Jim Dyer

    This looks great, butI can't find a functions.php file. Any idea where it should go, and should I make a functions.php file and put it in the root of my wordpress installation?

    Or does this mean I have one of the cheap hosts Marcel was talking about???


    • Jim, there should already be a functions.php file in your theme folder. If it's not there, either add it yourself (don't forget to put in standard php tags inside) or contact the theme author and ask him to add it.

      • Jim Dyer

        Thanks, Artem, I found it OK, but it's not working for me. I'm still getting the same error.

        I suppose there could be a server limit???

        • Absolutely – hosts, especially shared, may place restrictions that you are unable to override. I recommend going to a VPS or dedicated hosting in that case.

  • David G

    Thanks for this post Artem. This is sure on the right track. The add_action method wasn't working for me so I added a phprc file (PHP5.3) with custom directive for memory_limit – tried a setting of 256M and I could get 500 posts to be retrieved but failure at 1000. Trying 128M and 500 failed. Pain.
    I read else where there is a bug report into the Live Writer dev team to perhaps make their post retrieval a bit more efficient.
    Hopefully they will since it really shouldn't take that much mem to grab the posts.

    • Yeah, that'll be the day when they optimize it – I blame PHP for a lot of the memory bloat though.

  • Phillwv

    Yowser, if I might say.

    I'm on a share host (el cheapo) and after adding to functions.php WLW grabbed 2000 posts .. and, yes, listed them!

    So don't despair, folk. Took nearly a minute, btw. Don't know whether to be grateful to the webhost (Scala) or the WLW team – or both!

  • Pingback: I admired this and i am going to forget()

  • Pingback: a lot like Doraemon()