WordPress.org

Ready to get started?Download WordPress

Forums

Redeclaring functions - error on my own plugin (8 posts)

  1. haampie
    Member
    Posted 4 years ago #

    Hello there,

    After reading a lot about WordPress plugins I thought about creating my own plugin. It's a very simple plugin, I actually copied and pasted a lot from the WordPress documentation.

    When I activate the plugin I get an error:

    Fatal error: Cannot redeclare poll_installation() (previously declared in ~\wp-content\plugins\wp_poll_engine\poll_engine.php:31) in ~\wp-content\plugins\wp_poll_engine\poll_engine.php on line 74

    I activated and deactivated the plugin several times while testing.

    Here is the code:

    <?php
    global $wp_poll_db_version;
    $wp_poll_db_version = '0.1';
    
    function poll_installation(){
    	global $wp_poll_db_version;
    	global $wpdb;
    
    	$table_name = $wpdb->prefix . 'poll_engine';
    	if($wpdb->get_var("SHOW TABLES LIKE '" . $table_name . "'") != $table_name){
    		$sql = "CREATE TABLE " . $table_name . " (
    			id mediumint(9) NOT NULL AUTO_INCREMENT,
    			time bigint(11) DEFAULT '0' NOT NULL,
    			name tinytext NOT NULL,
    			text text NOT NULL,
    			url VARCHAR(55) NOT NULL,
    			UNIQUE KEY id (id)
    		);";
    		require_once(ABSPATH . 'wp-admin/include/upgrade.php');
    		dbDelta($sql);
    
    		$welcome_name = "Admin";
    		$welcome_text = "Congratulations, you just completed the installation!";
    
    		$insert = "INSERT INTO " . $table_name .
    			" (time, name, text) " .
    			"VALUES ('" . time() . "','" . $wpdb->escape($welcome_name) . "','" . $wpdb->escape($welcome_text) . "')";
    		$results = $wpdb->query( $insert );
    		add_option('wp_poll_db_version', $wp_poll_db_version);
    	}
    
    	$installed_ver = get_option( "wp_poll_db_version" );
    	if( $installed_ver != $wp_poll_db_version ) {
    		$sql = "CREATE TABLE " . $table_name . " (
    		id mediumint(9) NOT NULL AUTO_INCREMENT,
    		time bigint(11) DEFAULT '0' NOT NULL,
    		name tinytext NOT NULL,
    		text text NOT NULL,
    		url VARCHAR(55) NOT NULL,
    		UNIQUE KEY id (id)
    		);";
    
    		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    		dbDelta($sql);
    
    		update_option( "wp_poll_db_version", $wp_poll_db_version );
    	}
    }
    
    function wp_poll_menu(){
    	add_menu_page('WP Poll Engine', 'Poll Settings', 8, 'poll_settings', 'wp_poll_page');
    	add_submenu_page('poll_settings', 'Subpage for Poll Settings', 'Subpage', 8, 'poll_settings_sub', 'wp_poll_subpage');
    }
    
    function wp_poll_page() {
    	echo '<h2>Title</h2>';
    }
    
    function wp_poll_subpage(){
    	echo '<h2>Subpage</h2>';
    }
    
    register_activation_hook(__FILE__, 'poll_installation');
    add_action('admin_menu', 'wp_poll_menu');
    ?>

    Is there anyone who can solve the problem?

  2. willkemp
    Member
    Posted 4 years ago #

    It would be good if you said what you did to fix it - so that other people coming across this thread in future can find out.

  3. haampie
    Member
    Posted 4 years ago #

    Whoops, I marked the topic as resolved when I posted it ;) So, it is not resolved!

    If I remove the line with the register_activation_hook function there is no error... But I want to use that function anyway.

  4. Matty Cohen
    Member
    Posted 4 years ago #

    Hi haampie,
    If you rename your poll_installation() function, that should resolve your function declaration issue.

    I'd say, either rename it to something unique ( eg: haampie_poll_installation() ) or instantiate it from an object.

    I hope this helps. :)

    Cheers,
    Matt.

  5. haampie
    Member
    Posted 4 years ago #

    Hey Matt,

    Maybe the register_activation_hook function is bugged, because changing the name of my installation function makes no sense. I'm absolutely sure there is no function called my_own_poll_installation_function() ;)

    As I said before, when I remove the line containing the register_activation_hook, there is no error.

  6. willkemp
    Member
    Posted 4 years ago #

    I think you're misunderstanding what's actually happening here.

    When a plugin is activated, its code is first run in a "sandbox" environment to make sure it doesn't generate any errors. If it doesn't, then it's re-run in the normal environment.

    I haven't investigated this very deeply, but there seems to be something not quite right with this process, because some types of error seem to produce a situation where the code gets run twice and you get this "redeclare" error. What's actually happening is the declaration is clashing with itself - not with a declaration somewhere else in WordPress. But the cause of the problem actually lies elsewhere.

    You may be able to work out what the problem is from the httpd error log.

  7. haampie
    Member
    Posted 4 years ago #

    Thanks for your comment willkemp, but unfortunately there are no relevant errors in the logs.

    If I remove the add_option function and the update check, the error is gone. So I think something is wrong with add_option('wp_poll_db_version', $wp_poll_db_version);

    I'll post the whole script again:

    <?php
    global $wp_poll_db_version;
    $wp_poll_db_version = '0.1';
    
    function my_own_poll_installation(){
    	global $wp_poll_db_version;
    	global $wpdb;
    
    	$table_name = $wpdb->prefix . 'poll_engine';
    	if($wpdb->get_var("SHOW TABLES LIKE '" . $table_name . "'") != $table_name){
    		$sql = "CREATE TABLE " . $table_name . " (
    			id mediumint(9) NOT NULL AUTO_INCREMENT,
    			time bigint(11) DEFAULT '0' NOT NULL,
    			name tinytext NOT NULL,
    			text text NOT NULL,
    			url VARCHAR(55) NOT NULL,
    			UNIQUE KEY id (id)
    		);";
    		require_once(ABSPATH . 'wp-admin/include/upgrade.php');
    		dbDelta($sql);
    
    		$welcome_name = "Admin";
    		$welcome_text = "Congratulations, you just completed the installation!";
    
    		$insert = "INSERT INTO " . $table_name .
    			" (time, name, text) " .
    			"VALUES ('" . time() . "','" . $wpdb->escape($welcome_name) . "','" . $wpdb->escape($welcome_text) . "')";
    		$results = $wpdb->query( $insert );
    //add_option('wp_poll_db_version', $wp_poll_db_version);
    	}
    
    /*$installed_ver = get_option( "wp_poll_db_version" );
    if( $installed_ver != $wp_poll_db_version ) {
    	$sql = "CREATE TABLE " . $table_name . " (
    	id mediumint(9) NOT NULL AUTO_INCREMENT,
    	time bigint(11) DEFAULT '0' NOT NULL,
    	name tinytext NOT NULL,
    	text text NOT NULL,
    	url VARCHAR(55) NOT NULL,
    	UNIQUE KEY id (id)
    	);";
    	require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    	dbDelta($sql);
    
    	update_option( "wp_poll_db_version", $wp_poll_db_version );
    }*/
    
    }
    
    function wp_poll_menu(){
    	add_menu_page('WP Poll Engine', 'Poll Settings', 8, 'poll_settings', 'wp_poll_page');
    	add_submenu_page('poll_settings', 'Subpage for Poll Settings', 'Subpage', 8, 'poll_settings_sub', 'wp_poll_subpage');
    }
    
    function wp_poll_page() {
    	echo '<h2>Title</h2>';
    }
    
    function wp_poll_subpage(){
    	echo '<h2>Subpage</h2>';
    }
    
    register_activation_hook(__FILE__, 'my_own_poll_installation');
    add_action('admin_menu', 'wp_poll_menu');
    ?>
  8. Matty Cohen
    Member
    Posted 4 years ago #

    Hi haampie,
    Have you tried replacing add_option with update_option? While add_option just adds the option (which could return an error if the option already exists), update_option checks for the existance of an option, adds it if it doesn't exist or updates it if it does.

    I hope this helps. :)

    Cheers,
    Matt.

Topic Closed

This topic has been closed to new replies.

About this Topic