简体   繁体   中英

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.

(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.

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. 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?

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.

Therefore, adding the following line of code above dbDelta($sql) should fix the problem:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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