简体   繁体   English

具有类,名称空间和psr-0自动加载功能的Wordpress插件

[英]Wordpress plugin with classes, namespaces and psr-0 autoloading

I'm pretty new at this Wordpress thingy and I've now made my own theme and would like to create a plugin that handles my slideshow. 我在这个Wordpress上还挺陌生的,现在我做了自己的主题,想创建一个可以处理幻灯片的插件。

(I have to teach this for some pupils later on, so I cannot just use an existing plugin) (稍后我必须为一些学生教这个,所以我不能只使用现有的插件)

I have implemented the autoloader from http://www.php-fig.org/psr/psr-0 at the bottom and it works fine. 我已经从底部的http://www.php-fig.org/psr/psr-0实现了自动加载器,并且工作正常。

I can call all the methods in my class and everything, so thats fine. 我可以调用类中的所有方法以及所有方法,所以没关系。 Here's the actual question: 这是实际的问题:

When the 当。。。的时候

register_activation_hook(__FILE__, array('vendor\Keystroke\KeystrokeSlider', 'install'));

is called, then it runs the method 被调用,然后运行方法

class KeystrokeSlider{

    const VERSION = '1.0.0';

    static public function install(){
         global $wpdb;

         $charset_collate = $wpdb->get_charset_collate();

         $tableName = $wpdb->prefix . 'ks_albums';
         $sql = "CREATE TABLE IF NOT EXISTS $tableName (
             id INT(11) NOT NULL AUTO_INCREMENT,
             name VARCHAR(255) NULL,
             created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
             PRIMARY KEY  (id)
         ) $charset_collate;";

         self::upgrade();

         dbDelta($sql);

         add_option('keystroke_slider_version', self::VERSION);
    }
}

but the dbDelta() is an undefined function. 但是dbDelta()是未定义的函数。 I could see that it was trying to set the namespace in front of the function (I don't know why, since it's not at class method, but a function, right?). 我可以看到它正在尝试在函数前面设置名称空间(我不知道为什么,因为它不是类方法,而是函数,对吗?)。 Anyway I've tried calling it like this 无论如何,我试图这样称呼它

\dbDelta()

That seems to be working, but It still can't find the function. 这似乎可行,但是仍然找不到该功能。 Can I manually require the dbDelta in the class or is there something else wrong? 我可以在类中手动要求dbDelta还是其他错误?

Sorry for the very long question... 很抱歉很长的问题...

Ulrik McArdle 乌里克·麦克阿德尔

According to the WP manual in order to use the dbDelta function you first need to include the upgrade file which contains that function. 根据WP手册 ,为了使用dbDelta函数,您首先需要包括包含该函数的upgrade文件。

Therefore, adding the following line of code above dbDelta($sql) should fix the problem: 因此,在dbDelta($sql)上方添加以下代码行可以解决此问题:

require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM