1

The code below is contained in a plugin file. It just seeks to execute an update query against the wp database. However, its generating a fatal error.

Fatal error: Cannot redeclare ce3_cleanup()

Do I need to load a config file to get acess to $wpdb->query?

function ce3_cleanup()
{
    $wpdb->query("update wp_postmeta set meta_key=replace(meta_key,'cb2_customHeader','_cb2_customHeader') where meta_key like 'cb2_customHeader'");
    $wpdb->query("update wp_postmeta set meta_key=replace(meta_key,'cb2_customTitle','_cb2_customTitle') where meta_key like 'cb2_customTitle'");
}
register_activation_hook(__FILE__, 'ce3_cleanup');
?>

2 Answers 2

0

The recommended way to write that function would be:

function ce3_cleanup() {
    global $wpdb;

    $wpdb->query( "update $wpdb->postmeta set meta_key=replace(meta_key,'cb2_customHeader','_cb2_customHeader') where meta_key like 'cb2_customHeader'" );
    $wpdb->query( "update $wpdb->postmeta set meta_key=replace(meta_key,'cb2_customTitle','_cb2_customTitle') where meta_key like 'cb2_customTitle'" );
}
register_activation_hook( __FILE__, 'ce3_cleanup' );
2

No, Cannot redeclare... means that you already have a function named ce3_cleanup() somewhere else on your site. Perhaps in another plug-in or in your theme.

3
  • Hmm, I added global $wpdb inside the function and it finally worked! Not sure about the redeclare business. I don't have that function anywhere else..
    – Scott B
    Sep 28, 2010 at 21:21
  • Well, I say it worked. At least it didn't throw an error, but the previous entries that were without an underscore are not yet showing up with underscores... go figure. I'll have to examine that table.
    – Scott B
    Sep 28, 2010 at 21:22
  • Whenever you use $wpdb in a function, you need to make sure it's in the same scope ... just using $wpdb references a local scope ... if you want to use the actual $wpdb object, you need that global keyword. But the actual error message you received did not have anything to do with $wpdb but with the function invoking it ... not the most useful error message in the world.
    – EAMann
    Sep 28, 2010 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.