“Plugin could not be activated because it triggered a fatal error.”
-
16-10-2019 - |
Question
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');
?>
Solution
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' );
OTHER TIPS
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange