WordPress.org

Ready to get started?Download WordPress

Forums

Plugin development, install (3 posts)

  1. ckruse
    Member
    Posted 5 years ago #

    I am attempting to create my own plugin, and am running into a fatal error when I attempt to activate it. I cannot figure out why. As of now, the only thing the plugin does is create a new DB table at activation. Here is the code

    $rbVersion = "1.0";
    
    //install DB tables
    function rb_install() {
    	global $wpdb;
    
    	//first check to see if the DB table already exists
    	$tableName = $wpdb->prefix . "rusty_beard";
    
    	if($wpdb->getvar("SHOW TABLES LIKE '" . $tableName . "'") != $tableName) {
    		//create DB tables
    		$install = "CREATE TABLE " . $tableName . "(
    				field_id  KEY INT NOT NULL AUTO_INCREMENT,
    				field_name VARCHAR(50),
    				field_type VARCHAR(50),
    				field_default VARCHAR(240),
    				field_required INT,
    				field_label VARCHAR(240)
    				field_order INT)";
    
    		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    
    		dbDelta($install);
    
    		//add version to DB
    		add_option("rb_version", $rbVersion);
    
    	}
    }
    
    //install the plugin
    register_activation_hook(__FILE__, 'rb_install');

    This is all located in the main plugin file, the only plugin file I have.

    When I attempt to activate it, WP gives the following error:

    Plugin could not be activated because it triggered a fatal error.
    
    Fatal error: Cannot redeclare rb_install() (previously declared in /home/dir/public_html/dir/wp-content/plugins/RustyBeard/rusty_beard.php:21) in /home/dir/public_html/dir/wp-content/plugins/RustyBeard/rusty_beard.php on line 45
  2. Gale Andrews
    Member
    Posted 4 years ago #

    I am having the same problem. I see you have other threads about register_activation_hook. Did you get it resolved?

  3. allgood
    Member
    Posted 4 years ago #

    You need to wrap the above code with the following if statement...

    if(!function_exists("rb_install")){
        function rb_install() {
    	global $wpdb;
    
    	//first check to see if the DB table already exists
    	$tableName = $wpdb->prefix . "rusty_beard";
    
    	if($wpdb->getvar("SHOW TABLES LIKE '" . $tableName . "'") != $tableName) {
    		//create DB tables
    		$install = "CREATE TABLE " . $tableName . "(
    				field_id  KEY INT NOT NULL AUTO_INCREMENT,
    				field_name VARCHAR(50),
    				field_type VARCHAR(50),
    				field_default VARCHAR(240),
    				field_required INT,
    				field_label VARCHAR(240)
    				field_order INT)";
    
    		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    
    		dbDelta($install);
    
    		//add version to DB
    		add_option("rb_version", $rbVersion);
    
    	}
        }
    
        //install the plugin
        register_activation_hook(__FILE__, 'rb_install');
    }

Topic Closed

This topic has been closed to new replies.

About this Topic