【发布时间】:2011-05-10 21:03:50
【问题描述】:
我有一个类,它具有检查和在数据库中创建表的功能。 为此,我需要使用 WordPress $wpdb 对象。
我需要该功能仅在第一次插件激活时运行,所以我使用该功能:
register_activation_hook ( __FILE__, array( 'MemorialCandles', 'dbInstall' ) );
问题是我总是得到这个错误:
致命错误:在第 77 行的 /home/xxx/xxx/wordpress/wp-content/plugins/MemorialCandles/memorial-candles.class.php 的对象上下文中使用 $this
类代码:
<?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 );
}
}
?>
提前致谢! :D
【问题讨论】:
-
问题的标签不需要在问题的标题中以吸引那些熟悉 Wordpress 的人的兴趣,请原谅我编辑删除它。此外,+1 用于在您第一次尝试时管理格式! :D 值得指出的是,网络上有一个特定的Wordpress site,它可能能够提供更具体的建议。
-
谢谢大卫,非常感谢! :) 我也会在那里发帖,我不知道有一个专门的 WordPress 论坛:D
标签: php class global wordpress