Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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');
?>
share|improve this question

2 Answers 2

up vote 0 down vote accepted

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' );
share|improve this answer

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.

share|improve this answer
    
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 '10 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 '10 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 '10 at 21:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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