• I’ve created my first plugin for WP and I was hoping to release, so I decided to add an install function which would set up the database to save people some hassle, but it doesn’t seem to work.

    I get no errors, but the table just isn’t created.

    Here’s the code I’m using:

    global $wpdb;
    	$tableName = $wpdb->prefix . 'tweets';
    
    	$twitterUserName = "jonlambell";
    	$tweetLimit = 5;
    
    	function tweet_install()
    	{
    		global $wpdb;
    		global $tableName;
    
    		if($wpdb->get_var("SHOW TABLES LIKE '$tableName'") != $tableName)
    		{
    			$sql = "CREATE TABLE " . $tableName . " (
    					  id bigint(255) NOT NULL,
    					  name varchar(255) NOT NULL,
    					  img varchar(255) NOT NULL,
    					  msg varchar(255) NOT NULL,
    					  time varchar(255) NOT NULL,
    					  PRIMARY KEY  (id)
    					);";
    
    			require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    			dbDelta($sql);
    		}
    	}
    	register_activation_hook(__FILE__,'tweet_install');

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter JonLambell

    (@jonlambell)

    I’ve tried putting this at the top and at the bottom of my page and it still refuses to add the table.
    I’ve also tried using ‘backticks’ around the field names to no effect.

    Thread Starter JonLambell

    (@jonlambell)

    No one know anything about this then? Surely I’ve either done something slightly wrong here which I can’t spot or I’m missing something.

    Thread Starter JonLambell

    (@jonlambell)

    Tried changing to this

    global $wpdb;
    	$tableName = $wpdb->prefix . 'tweets';
    
    	$twitterUserName = "jonlambell";
    	$tweetLimit = 5;
    
    	function tweet_install()
    	{
    		global $wpdb;
    		global $tableName;
    
    		$sql = "DROP TABLE IF EXISTS " . $tableName . ";
    				CREATE TABLE " . $tableName . " (
    				  id bigint(255) NOT NULL,
    				  name varchar(255) NOT NULL,
    				  img varchar(255) NOT NULL,
    				  msg varchar(255) NOT NULL,
    				  time varchar(255) NOT NULL,
    				  PRIMARY KEY  (id)
    				);";
    		$wpdb->query($sql);
    		$wpdb->show_errors();
    	}
    	register_activation_hook(__FILE__,'tweet_install');

    Still it doesn’t add the table to the db. I’ve tried all sorts now and it’s doing my head in. It just refuses to add the table!

    Thread Starter JonLambell

    (@jonlambell)

    I can’t believe that no one knows how to solve this problem.
    With the amount of people who frequent this place.

    I’ve found a couple of threads with similar problems, none of the solutions work but must are also unanswered threads. What’s with the lack of support with WordPress?

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Problems with register_activation_hook’ is closed to new replies.