Windows Live Writer Error: blogger.getUsersBlogs method received from the weblog server was invalid

This was really irritating me today. I set up a new WordPress installation (more on that another day), tweaked it up a little bit, and then couldn’t get it working with Windows Live Writer, which is my blogging tool at the moment. (It really is one of the better Microsoft products).

So I go through the add new blog wizard, and then I get this obnoxious error:

So irritating! Naturally I googled, and came across a solution. All I had to do was add these lines to my .htaccess file:

<Files xmlrpc.php>
SecFilterInheritance Off
</Files>

But that didn’t solve the problem. It actually made it worse…. now I was getting an apache error even getting to the blog page through a browser.

After doing some Http proxy snooping using Fiddler, I figured out that the error was this:

Warning: Cannot modify header information – headers already sent by (output started at /home/testdomain.com/html/wp-content/plugins/recent-posts.php:2)

Ah! Now we are getting somewhere. When I opened up the offending plugin file, I found blank lines at the top and bottom before the <? , which is what caused the whole problem… the xmlrpc.php in WordPress couldn’t change the headers because the blank lines had already been sent down to the client. This then caused the XML to be invalid, which gave me that error.

Minor WordPress Annoyances (Double Dashing)

I just recently realized that WordPress automatically converts a double dash — into an em-dash, which looks like a double dash without a space between them. So irritating, especially when you are showing source code or command line examples in your post.

The solution is to open the SITE_ROOT/wp-includes/functions-formatting.php file and comment out these two lines:

$curl = str_replace(‘ — ‘, ‘ — ‘, $curl);
$curl = str_replace(‘–’, ‘–’, $curl);

The ironic thing is that you wouldn’t even see the double dash in those lines if I hadn’t already commented them both out.

But now, I can double dash away.

Fixing WordPress WP-Cache Errors When Moving your Blog

I recently migrated this site from Dreamhost over to MediaTemple, which was one of the simplest migrations I’ve done… upload the database file, copy over the contents of my web directory, update wp-config.php…  and everything works!

Well, nearly. I still had this error message when I browsed to my site:

Warning: main(/johnnysthoughts.com/wp-content/advanced-cache.php) [function.main]: failed to open stream: No such file or directory in /johnnysthoughts.com/wp-settings.php on line 69

I’m using the WP-Cache module to make sure that the site runs as fast as possible. Because of the way the module works, you can’t just copy the site over. The problem lies in the symbolic link from SITE_ROOT/wp-content/advanced-cache.php to the actual plugin file in the plugins directory.

So, open up your SSH connection and navigate to your site root, and run these commands.

cd wp-content
rm advanced-cache.php
rm cache/*
chmod 775 cache
ln -s plugins/wp-cache/wp-cache-phase1.php advanced-cache.php

This will delete the bad symlink file, clear the cache, set the permissions on the folder and then create a new symlink to the wp-cache-phase1.php file.

PHP: Disabling Adsense on All Posts Within the Last Week

I’ve always disliked having Adsense on my blog, but like many bloggers, I really need to recoup the costs of hosting my site. And since I personally dislike slow loading websites, I’ve tried to keep my website running as fast as possible with a quality hosting provider like MediaTemple… which isn’t exactly the cheapest hosting out there.

I’ve decided that a nice compromise to reward the faithful readers of my blog is to disable the ads on blog posts newer than one week. This means that if you are reading my blog via a feed link, or even from a current link posted on a social site like dzone, you won’t have to see any annoying ads on the page. At all.

So how to go about this? Turns out it’s pretty simple… It took me a minute of googling to remember how to subtract days from a date object in PHP.

This gives you a date object from exactly one week ago:

$lastweek = time() – (7 * 24 * 60 * 60);

Then, all I needed was to open up the single.php file in my theme directory, and change my adsense deluxe code to look like this:

<?
$lastweek = time() – (7 * 24 * 60 * 60);
if(get_the_time(‘U’) < $lastweek){
    adsense_deluxe_ads(‘largerect’);
}
?>

If you aren’t using Adsense Deluxe, you can always just substitute your own ad code in there. Also, if you want to set a longer or shorter date range, just substitute the 7 for whatever amount of days you want.

Another note, if you are using WP-Cache to cache your content, is that the ads won’t start showing up after a week unless you whack your cache every so often. Not a huge deal, but something to keep in mind.

And just like that, no more ugly ads on “current” blog posts.

Workaround: SimpleXML Problem with Single Element Values

Quinn and I are working on a project that involves parsing a large number of XML transactions in PHP. We chose to use SimpleXML because it’s lightweight and the XML we are parsing isn’t really complicated enough to warrant using the full Dom.

We’ve run into a lot of strange issues with SimpleXML, most notablly that it always seems to return an array even if you query for a single element. We’ve also uncovered some inconsistent errors that seem to occur when trying to get the element value.

Here’s the sample XML that we are using. (ok, it’s not the real thing, I changed all the names)

<?xml version=”1.0″?>
<article username=”johnny”>
   <thepost>
      <bgcolor>#6699cc</bgcolor>
      <commentcount>50</commentcount>
      <text>The article text goes here</text>
   </thepost>
</article>

First, we read the contents of the XML from a file named test.xml into a variable like so:

$obj = simplexml_load_string(file_get_contents(“test.xml”));

If we were to do a print_r() on the $obj variable, we’d see this output:

SimpleXMLElement Object ( [@attributes] => Array ( [username] => johnny ) [thepost] => SimpleXMLElement Object ( [bgcolor] => #6699cc [commentcount] => 50 [text] => The article text goes here ) )

So then we did a print_r() on the $obj->commentcount child item, and here’s the weirdness that ensued:

SimpleXMLElement Object ( [0] => 50 )

When we tried to access the item using $obj->commentcount[0] it would return nothing every time. It worked just fine for the bgcolor element, but wouldn’t work for the commentcount. We started thinking that it must be something to do with the integer in the field instead.

Finally Quinn stumbled on a method that worked:

echo(1 * $obj->commentcount[0]);

We then figured out that you could also use this syntax, which would also work:

$cc = $obj->commentcount[0];

echo($cc);

What seems to be going on is that the object can’t be echoed out to a string directly without “casting” it first.

So, the moral of this story is that you may want to pull SimpleXML child elements out into seperate variables if you are having issues returning the values. What a pain.