php - Call External Object in Class Function During Callback -
i've class has function check , creates table in database. in order need use wordpress $wpdb object.
i need function run on first plugin activation, use function:
register_activation_hook ( __file__, array( 'memorialcandles', 'dbinstall' ) );
the problem 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 plugin , handles queries , 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 plugin database table name. */ private $tablename; // constructor /** * initiates plugin, stores , configure basic setup procedures. * * @return void */ function __construct() { global $wpdb; $this->tablename = $wpdb->prefix . 'memorialcandles'; } // getters // setters // methods /** * handles database table creation. * * @return void */ function dbinstall() { global $wpdb; if( $wpdb->get_var( "show tables `{$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 database table drop procedure. * * @return void */ function dbuninstall() { global $wpdb; $sql = "drop table if exists `{$this->tablename}`;"; $wpdb->query( $sql ); } } ?>
thanks in advance! :d
to use instance method in callback, callback needs instance. you'll either need create instance call register_activation_hook
:
register_activation_hook(__file__, array(new memorialcandles(), 'dbinstall'));
or make dbinstall
class method.
class memorialcandles { // variables /** * @var string stores plugin database table name. */ private static $tablename, $tablesuffix = 'memorialcandles'; ... // methods /** * handles database table creation. * * @return void */ static function dbinstall() { global $wpdb; $tablename = self::$tablename = $wpdb->prefix . self::$tablesuffix; if( $wpdb->get_var( "show tables `{$tablename}`" ) != $tablename ) { $sql = "create table `{$tablename}` ( id int(8) unsigned not null auto_increment, fullname text not null, message text not null, postdate text not null, galleryid int(8) unsigned not null, unique key id(id) );"; require_once( abspath . 'wp-admin/includes/upgrade.php' ); dbdelta( $sql ); } } ... }
Comments
Post a Comment