WordPress.org

Ready to get started?Download WordPress

Forums

Need Help on plugin Dev (5 posts)

  1. awls99
    Member
    Posted 3 years ago #

    Hi there,
    I'm trying to write a simple plugin for wordpress, I've followed instructions here: http://codex.wordpress.org/Creating_Tables_with_Plugins because I needed two new tables for my plugin.
    Here's the sample code:

    function wat_install ()
    {
    	$table1 = $wpdb->prefix.'wat_ads';
    	$table2 = $wpdb->prefix.'wat_clicks';
    
    	if($wpdb->get_var("show tables like '$table1'") != $table1) {
    		$sql1="CREATE TABLE <code>$table1</code> (
    			  <code>id</code> INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
    			  <code>html</code> TEXT DEFAULT ' ',
    			  PRIMARY KEY (<code>id</code>)
    			)
    			ENGINE = MyISAM;
    		";
    
    	}
    	if($wpdb->get_var("show tables like '$table2'") != $table2) {
    		$sql2="CREATE TABLE <code>$table2</code> (
    			  <code>id</code> INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
    			  <code>html</code> TEXT DEFAULT ' ',
    			  PRIMARY KEY (<code>id</code>)
    			)
    			ENGINE = MyISAM;
    		";
    
    	}
    	$wpdb->query($sql1);
    	$wpdb->query($sql2);
    }
    register_activation_hook(__FILE__,'wat_install');

    When I try to activate the plugin I get:

    Plugin could not be activated because it triggered a fatal error.
    Fatal error: Cannot redeclare wat_install() (previously declared in C:\*****\wp-content\plugins\wp-ad-tracking\wp-ad-tracking.php:14) in C:\*****\wp-content\plugins\wp-ad-tracking\wp-ad-tracking.php on line 39

    Even though I get that error the plugin still shows up as activated on the plugin list, if I comment out "register_activation_hook(__FILE__,'wat_install');" no error will be produced, but I won't get my fancy tables.
    Note: Line 14 is the first line inside the function that has some code while line 39 is the line here the end function bracket (}) is

    I've tried several things and a lot of googling, but I'm lost, can anyone give me a hand?

  2. stephane02
    Member
    Posted 3 years ago #

    Well, fix the error first, then ftp connect to wordpress plugin folder, remove your plugin files, try uploading the plugin again and test it.

  3. awls99
    Member
    Posted 3 years ago #

    I'm trying to figure out what's the error :(
    (and it's on local machine)

  4. awls99
    Member
    Posted 3 years ago #

    I need to get this plugin working soon, so I made a small workaround, not preatty, but works like a charm:

    on the install function added (right before the end): add_option('wat_installed', true, '', 'no');
    and used that option to make sure it runs the first time:

    //register_activation_hook(__FILE__,'wat_install');
    if(!get_option('wat_installed'))wat_install();
    add_action('admin_menu', 'wat_admin_options');
  5. awls99
    Member
    Posted 3 years ago #

    bump
    Could really use some help on this
    I got this plugin working for my own use but I'd like to share it and the way it is now is just not elegant

Topic Closed

This topic has been closed to new replies.

About this Topic