Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress not recognizing function during plugin activation

I get this error when I try to activate my plugin:

call_user_func_array() expects parameter 1 to be a valid callback, function 'fancy_lists_create_table' not found or invalid function name in /Applications/XAMPP/xamppfiles/htdocs/intranet/wp-includes/plugin.php on line 525

This was output from a plugin that debugs the "xxx characters of unexpected output" error that is commonly seen. I don't understand why the function isn't valid. Code below:

namespace fancy_lists; 
register_activation_hook( __FILE__, 'fancy_lists_create_table' );

function fancy_lists_create_table(){
    global $wpdb;

    $table_name = $wpdb->prefix.'fancy_lists';

    if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {

        $charset_collate = $wpdb->get_charset_collate();

        $sql = "CREATE TABLE $table_name (
          id mediumint(9) NOT NULL AUTO_INCREMENT,
          created datetime DEFAULT NOW() NOT NULL,
          created_by text NOT NULL,
          list_name text NOT NULL,
          column_config text NOT NULL,
          permissions text NOT NULL,
          notifications text NOT NULL,
          UNIQUE KEY id (id)
        ) $charset_collate;";

        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        dbDelta( $sql );
    }

}
like image 755
asebold Avatar asked Dec 02 '25 09:12

asebold


1 Answers

register_activation_hook() isn't aware of the namespace. You must specify it:

register_activation_hook( __FILE__, __NAMESPACE__ . '\fancy_lists_create_table' );
like image 164
dan9vu Avatar answered Dec 04 '25 00:12

dan9vu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!