The MU forums have moved to

$wpdb->prefix doesn't work during activation? (5 posts)

  1. delayedinsanity
    Posted 14 years ago #

    I'm just finishing up a small plugin, and was testing my activation and deactivation hooks when I noticed a problem right off the line.

    The plugin uses a few database tables, which are defined as constants, such as;

    define( 'MYPLUGIN_TABLE_WHATEVER', $wpdb->prefix . 'table_whatever' );

    Which works fine up until the constant is used inside the activation hook, at which point it drops the $wpdb->prefix, and I wind up with tables called 'table_whatever' only and not 'wp_#_table_whatever'.

    Easy fixsure, simply hardcode $wpdb->prefix and the name of the table into the activation hook, but I'm just curious as to why this is the only place it drops the prefix off of a constant? Seems a little odd.

  2. tmoorewp
    Posted 14 years ago #

    I think this is more to do with the PHP define() function than WordPress. The define is ignore your string join operation and using only what it views as a string, which is 'table_whatever'.

  3. delayedinsanity
    Posted 14 years ago #

    PHP's define() will accept string concatenation, as well as nested methods if you need; the problem is that it works just fine everywhere else in the code.

    The only place where $wpdb->prefix isn't appended properly is inside the activation callback. Everywhere else I get wp_#_tablename. It's like it somehow strips it, or calls the function as though it existed outside of the file somewhere in limbo.


    It gets odder than that. Apparently if you don't add a deactivation hook, Wordpress calls the activation hook on deactivation? It also runs twice on activation....

    I wrote the following quick'n'dirty to demonstrate;

    <?php /*
    Plugin Name: WPDB Object Test
    Description: If you only knew.
    Version: 0.1
    define( 'WPDB_TEST_TABLE', $wpdb->prefix . 'table_name' );
    $outside_data = WPDB_TEST_TABLE;
    function wpdb_test_activate( $outside_data ) {
    	global $wpdb;
    	$file = dirname( __FILE__ ) . '/test.log';
    	$data = 'Outside: ' . $outside_data . "\n";
    	$data .= 'Constant: ' .WPDB_TEST_TABLE . "\n";
    	$data .= 'Inside: ' . $wpdb->prefix . "table_name\n\n";
    	$fp = @fopen( $file, 'a' );
    	if ( $fp )
    		fwrite( $fp, $data );
    	fclose( $fp );
    register_activation_hook( __FILE__, call_user_func( 'wpdb_test_activate', $outside_data ) );

    It writes to the file twice on activation, the first time around stripping the prefix from the constant, the second time around it works (wtf?). If you then deactivate the plugin, it writes to the file again, which means the activation hook is running on deactivation.

    I say again... w-t-f? :)

  4. delayedinsanity
    Posted 14 years ago #

    Nevermind, I got ripped a new one for asking on wp-hackers about this, and the original problem boiled down to nothing more than scope. When called during activation the script is outside of the global scope, and $wpdb wasn't the database object as suspected until after activation.

    Amateur mistake. :)

  5. tmoorewp
    Posted 14 years ago #

    Yeah, I saw your thread on the hackers list. Then, of course, I saw the scope issue when someone else pointed it out. It happens...

About this Topic

  • Started 14 years ago by delayedinsanity
  • Latest reply from tmoorewp