Already Used Variable Names in WordPress

Last week I handed over a somewhat completed plugin over to Kyjen, and I was pretty proud of it. I knew it wasn’t 100% done; however, it was a good first step. Earlier today, I got an email from them with a screenshot of a very weird bug:

All of the messages in my plugin were showing up in the admin/user-new.php within the WordPress Admin… Pretty weird, right?

After a lot of investigating, and trying to figure out where the hell my custom messages were being called, I came across this part within the admin/user-new.php:

Turns out, in the custom messages in my plugin, I had declared the array that held all of my messages as $messages, thus overriding the WordPress core.

Now I know to be more careful in my plugin variable naming. Previously, I had been careful with just my function names, but I guess it can’t hurt to be as explicit as possible.

  • Anonymous

    A good idea when creating wordpress plugins is to prefix your variables with an abbreviation of your plugin name. So for the “MyPlugin” plugin you could use $mp_var or $mpVar depending on your style. It makes debugging in the future far easier.

    • Agreed. All of my plugin’s functions had the prefix, just not the variables. Lesson learned 🙂

    • Ballz

      bitch  fuk u