Archive

Posts Tagged ‘wordpress’

Obscure Wordpress Bug

December 29th, 2008

While working on my new WordPress plugin (announcement post coming soon), I stumbled upon an interesting point in the way WordPress plugins work (well, Jorge stumbled upon it, I debugged it). Plugins are loaded as files in the global namespace, not within the confines of a function. This means that variables you declare can have an impact on the rest of WordPress.

A specific example: my plugin had the following lines of code:

foreach($custax_style_pages AS $page)
    add_action('admin_head-'.$page, 'custax_styles');

Harmless, right? Wrong! Unfortunately, the way WP parses queries includes this code:


for ($i=0; $ipublic_query_vars); $i += 1) {
    $wpvar = $this->public_query_vars[$i];
    if (isset($this->extra_query_vars[$wpvar]))
        $this->query_vars[$wpvar] = $this->extra_query_vars[$wpvar];
    elseif (isset($GLOBALS[$wpvar]))
        $this->query_vars[$wpvar] = $GLOBALS[$wpvar];
    //...
}

The end result is that, since $page gets registered in $GLOBALS, and “page” is common enough to be a query variable, everything goes to hell fairly quickly.

The moral of the story? Prefix your variables - all of them - unless you’re positive your code is encased in a function.

Wordpress Plugin Installation Hackery

July 15th, 2008

Update: Full script posted below.

I’m just wrapping up a relatively large project centering around a Wordpress plugin.  I’ve gotten a chance to explore the API in depth, and have discovered a lot of nice things and a lot of not-so-nice things.  One of the not-so-nice things was the way plugin activation is handled…it is assumed that, assuming no fatal errors occur, that a plugin was activated properly every time.  There’s no feedback mechanism, no way to pass back a message saying “woa, something’s wrong here”.  So I wrote my own.

Now, this plugin will be installed on one site, and I will be doing the installation, so realistically there’s not too much of a concern.  But I was slightly worried that the plugin made a lot of tables, and that in the future the plugin may be installed on a system that has a table with the same name.  This may be incredibly unlikely, but it’s good to plan.  With that goal in mind I wrote a neat piece of code (see below for the whole script to put things in context, though this piece is the most interesting):
Read more…