Fixing Apostrophe Handling in WP MTG-Helper

I recently got into blogging about Magic: The Gathering. It’s been something I’ve wanted to do for a long time. While setting up my blog I came across the excellent WP MtG-Helper WordPress plugin which provides nice looking card images using simple BBCode-like syntax. It’s easy to install and use. Many thanks to Sebastian Sebald for the work he has put into it.

Once I started using it I noticed that cards that contained apostrophes in the name proved to be a weak point. I checked out the plugin FAQ and the plugin homepage to see if there was information on it. Sure enough, it’s a known issue. My interest was piqued. By day I work as an applications programmer specializing in web applications and services and this problem was right up my alley.

The problem stems from WordPress escaping all apostrophes to prevent them from negatively impacting the WordPress scripts that process a post and display it. It’s a good thing. Looking at the URL generated by the WP MtG-Helper for a card using an apostrophe, say Krark’s Thumb, I could see that the escaped apostrophe was causing the problem, as stated by Sebastian in his known issue item. The HTML character code for the ‘ is ’. I could see that this was then URL encoded to become:

%26%238217%3B

Delving further into the issue led me to the code making up mtg_helper_cardfinder.php within the plugin; namely the function get_source_from_name. This method takes the name of the card and parses it into the format that Wizards of the Coast uses for their public images. Here’s the code from version 1.1.1:

//for secure cardname parsing
function get_source_from_name($name){
	$name = htmlspecialchars(urldecode($name));
	$name = str_replace(array(",","-")," ",$name);
	$name = ereg_replace('[ ]+', ' ', $name);
	$name = str_replace(" ","_",$name);

	return "http://www.wizards.com/global/images/magic/general/"
		. $name
		.".jpg";
}

With a slight alteration I added a simple string replacement after the urldecode call and before the htmlspecialchars call. The idea was to take advantage of the fact that WP MtG-Helper found the card fine if you just didn’t include the ‘. So, the idea is to leave the card name displaying properly, and simply remove the ‘ from the WP MtG-Helper card image URL. Here’s the code:

//for secure cardname parsing
function get_source_from_name($name){
        $name = str_replace("’","",urldecode($name));
        $name = htmlspecialchars($name);
        $name = str_replace(array(",","-")," ",$name);
        $name = ereg_replace('[ ]+', ' ', $name);
        $name = str_replace(" ","_",$name);

        return "http://www.wizards.com/global/images/magic/general/"
                . $name
                .".jpg";
}

So, to anyone who is using the WP MtG-Helper WordPress plugin who wants cards like Akroma’s Vengeance and Nevinyrral’s Disk to display but also want to use the proper name of the card, this is the fix you’ll want to apply.

Only follow these next steps at your own risk. If you don’t feel comfortable applying the changes yourself then either get someone who is comfortable doing it to help you, or wait until the plugin is updated in the future. To apply the fix yourself you need to edit the mtg_helper_card.php file in the WP MtG-Helper plugin. Replace the line

$name = htmlspecialchars(urldecode($name));

with the following lines:

$name = str_replace("’","",urldecode($name));
$name = htmlspecialchars($name);

Alternatively, you can also collapse that to one line if you like:

$name = htmlspecialchars(str_replace("’","",urldecode($name)));

Save the file once you’ve finished the edit. You should be able to then use proper card names in your posts. For example, cards like Dromar’s Charm should work after the update. Enjoy.

Father, husband, programmer, gamer. Magic: The Gathering enthusiast who enjoys playing and discussing the game with anyone who is willing.

Tagged with: , ,
Posted in General
12 comments on “Fixing Apostrophe Handling in WP MTG-Helper
  1. Thea Steele says:

    This is awesome, thanks! I’m just learning PHP so this was an awesome convergence of interests for me :)

  2. Glad to hear it! I contacted the developer of the plugin as well and he indicated he’d work on integrating the fix.

  3. Sebastian says:

    Hi,

    could you send me a copy of the adjusted code, please? :) Would save me some time. I’ll uploaded it to the plugins on wordpress.org and credit you.

    Greetings,

    Sebastian

  4. Sent you the file. Thanks again!

  5. Zaael says:

    If I hover over Dromar’s Charm I’m not seeing a card, anyone else got the same problem?

  6. Thanks for pointing it out. I’m at work, so I can’t and haven’t tested the card images today due to firewall restrictions. I just upgraded the plugin earlier today to the newly released v1.1.2, which is supposed to include the fix. Maybe something’s up. I’ll check it out and will post back when I can.

  7. Zaael, It looks to be working fine with version 1.1.2. Were you by chance behind a firewall that blocks http://www.wizards.com?

  8. Zaael says:

    Hmm strange. I don’t think so. It worked for me before the upgrade to 1.1.2. I’m not seeing the cards if I hoover over the names. I downgraded to version 1.1.1 for my site and it’s working again. Weird, maybe I need to learn how to hoover over card names. :-)

  9. I believe you, just having a different experience myself. I am running WordPress 2.9.2 and upgraded the plugin via the Dashboard. When you hover over Dromar’s Charm in this blog post, does the card display for you?

  10. Zaael says:

    No it doesn’t for me. And this is the fourth pc I try it on. Does it display for you?

  11. Hmm, worked for one of my friends, but this current PC doesn’t. I’m seeing what you’re seeing now that I’ve had a chance to sit down and test it. I’ll try to contact Sebastian as he manages it. Thanks for your patience with me, Zaael.

  12. Zaael says:

    No problem! I’ve had contact with Sebastian about it. He said he just changed one line of code. Maybe if someone else comes with the problem he will look a little further into it. :-)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>