Php - 开发Wordpress插件

时间:2014-12-25 02:00:19

标签: php wordpress plugins

我正在开发我的第一个Wordpress-Plugin,现在,它应该做的只是:

  1. 安装时:创建表格。
  2. 我的代码:

    if ( ! class_exists ( 'My_Plugin' ) )
    {
    class My_Plugin
    {
        /**
         * Construct the plugin object
        **/
        public function __construct ()
        {
            // register actions
        }
    
        /**
         * Activate the plugin
        **/
        public static function activate ()
        {
            global $wpdb;
    
            $tableName = tableName ( $wpdb );
    
            if ( ! tableExists ( $wpdb , $tableName ) ) 
            {
                $sql = "CREATE TABLE " . $tableName . " (
                    id INT(9) UNSIGNED NOT NULL AUTO_INCREMENT,
                    any_column VARCHAR(255) NOT NULL,
                    UNIQUE KEY id (id)
                );";
    
                require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
                dbDelta($sql);
            }
        }
    
        /**
         * Deactivate the plugin
        **/     
        public static function deactivate ()
        {
            // Nothing
        }
    
    
        // Return table name
        public function tableName ( $wpdb , $tableName = 'new_table' )
        {
            $tableName = $wpdb->prefix . $tableName;
            return $tableName;
        }
    
    
        // Check if table already exists
        public function tableExists ( $wpdb , $tableName )
        {
            try
            {
                $result = $wpdb->query("SELECT 1 FROM $tableName LIMIT 1");
            } 
            catch ( Exception $e )
            {
                return FALSE;
            }
    
            return $result !== FALSE;
        }
    }
    }
    
    if ( class_exists ( 'My_Plugin' ) )
    {
        // Activation and Deactivation hooks
        register_activation_hook ( __FILE__ , array ( 'My_Plugin' , 'activate' ) );
        register_deactivation_hook ( __FILE__ , array ( 'My_Plugin' , 'deactivate' ) );
    
        // Instantiate the plugin class
        $plugin_template = new My_Plugin ();
    }
    

    但是会返回此错误:

    Fatal error: Call to undefined function tableName() in /www/htdocs/path/to/file.php on line
    

    但我无法看到我的错误,我真的不确定为什么这个功能未定义...任何人都可以帮我这个?

    问候!!

2 个答案:

答案 0 :(得分:2)

tableName()不是一个全局函数,它是My_Plugin的一种方法。简单的方法是将activate()deactivate()转换为非静态函数,并使用$this->tableName()

// Instantiate the plugin class
$plugin_template = new My_Plugin ();

register_activation_hook ( __FILE__ , array ( $plugin_template , 'activate' ) );
register_deactivation_hook ( __FILE__ , array ( $plugin_template , 'deactivate' ) );

public function activate ()
{
    global $wpdb;

    $tableName = $this->tableName ( $wpdb );

    if ( ! $this->tableExists ( $wpdb , $tableName ) ) 
    {
        $sql = "CREATE TABLE " . $tableName . " (
            id INT(9) UNSIGNED NOT NULL AUTO_INCREMENT,
            any_column VARCHAR(255) NOT NULL,
            UNIQUE KEY id (id)
        );";

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

否则,您需要在My_Plugin中构建activate()的新实例,然后通过该$class->tableName()访问它。

您可以查看manual了解详情。

答案 1 :(得分:1)

此处插件激活中创建表的功能
register_activation_hook( __ FILE __ ,' create_table');
function create_tabletag() { require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); $query="CREATE TABLE IF NOT EXISTS tab_list( tab_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(40), isvisible VARCHAR(20) )ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;"; dbDelta($query); //This function use to create table }

相关问题