Domanda

Ho una classe che ha una funzione che verifica e crea tabella nel database. Per fare in modo ho bisogno di usare WordPress $ wpdb oggetto.

Ho bisogno la funzione per funzionare solo su prima attivazione plug-in, così io uso la funzione:

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

Il problema è che ottengo sempre questo errore:

Fatal error: Usando $ this quando non in contesto oggetto in /home/xxx/xxx/wordpress/wp-content/plugins/MemorialCandles/memorial-candles.class.php on line 77

il codice della classe:

<?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 );
    }    
}

?>

Grazie in anticipo! : D

È stato utile?

Soluzione

Prova:

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

Oppure stabilire dbInstall come'static' e l'uso che per impostare il nome della tabella, invece il costruttore, che credo sia l'approccio migliore.

Altri suggerimenti

Ok nuova risposta, e questa volta una soluzione di lavoro collaudato. prima creare un'istanza di voi classe e solo allora chiamare il register_activation_hook così qualcosa come:

$MemorialCandles = NEW MemorialCandles();

register_activation_hook  ( __FILE__, array( 'MemorialCandles', 'dbInstall'   ) );
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a wordpress.stackexchange
scroll top