1

I've a class which has a function that check and creates table in database. In order to do so i need to use WordPress $wpdb object.

I need the function to run only on first plugin activation, so i use the function:

register_activation_hook  ( __FILE__, array( 'MemorialCandles', 'dbInstall'   ) );

The problem is that i get always this error:

Fatal error: Using $this when not in object context in /home/xxx/xxx/wordpress/wp-content/plugins/MemorialCandles/memorial-candles.class.php on line 77

the class code:

<?php

// Global Variables:
global $wpdb;
register_activation_hook  ( __FILE__, array( 'MemorialCandles', 'dbInstall'   ) );

/**
 * Class: MemorialCandles
 * 
 * Provides skeleton to the plugin and handles queries and action.
 * 
 * @author Dor Zuberi <dor@zubri.me>
 * @copyright 2011 Dor Zuberi
 * @license http://www.php.net/license/3_01.txt
 */
class MemorialCandles
{
    // Variables    
    /**
     * @var string stores plugin direction - RTL or LTR.
     */
    private $pluginDirection;

    /**
     * @var string stores the plugin database table name.
     */
    private $tableName;

    // Constructor
    /**
     * Initiates the plugin, stores and configure the basic setup procedures.
     * 
     * @return void
     */
    function __construct()
    {
        global $wpdb;

        $this->tableName = $wpdb->prefix . 'memorialcandles';
    }

    // Getters

    // Setters

    // Methods
    /**
     * Handles the database table creation.
     * 
     * @return void
     */
    function dbInstall()
    {
        global $wpdb;

        if( $wpdb->get_var( "SHOW TABLES LIKE `{$this->tableName}`" ) != $this->tableName )
        {
            $sql = "CREATE TABLE `{$this->tableName}` (
                        id        int(8) NOT NULL AUTO_INCREMENT,
                        fullName  text   NOT NULL,
                        message   text   NOT NULL,
                        postDate  text   NOT NULL,
                        galleryID int(8) NOT NULL,

                        UNIQUE KEY id(id)
                    );";

            require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
            dbDelta( $sql );
        }
    }

    /**
     * Handles the database table drop procedure.
     * 
     * @return void
     */
    function dbUninstall()
    {
        global $wpdb;

        $sql = "DROP TABLE IF EXISTS `{$this->tableName}`;";

        $wpdb->query( $sql );
    }    
}

?>

Thanks in advance! :D

1

Try:

register_activation_hook  ( __FILE__, array( new MemorialCandles(), 'dbInstall'   ) );

Or define dbInstall as ´static´ and use that to set the table name instead the constructor, which I think is the best approach.

3
  • the first method works :) but now i have a different problem, when i activate the plugin, it creates the table but i get the following error: The plugin generated 336 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.
    – Dor Zuberi
    May 11 '11 at 4:37
  • Do you have WP_DEBUG activated?
    – MZAweb
    May 11 '11 at 14:44
  • yes, it is activated :)
    – Dor Zuberi
    May 12 '11 at 10:31
1

Ok new answer , and this time a tested working solution. first create an instance of you class and only then call your register_activation_hook so something like:

$MemorialCandles = NEW MemorialCandles();

register_activation_hook  ( __FILE__, array( 'MemorialCandles', 'dbInstall'   ) );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.