within my singleton I have these functions

class plugin {

  private static $_instance;

  private function __construct() {
    //does all add_actions & filters

  public static function getInstance() {
    if ( ! ( self::$_instance instanceof self ) ) {
        self::$_instance = new self();

    return self::$_instance;

  static function create_order_table() {
    global $wpdb;

      $table_name = $wpdb->prefix . "order";
      $charset_collate = $wpdb->get_charset_collate();

      $sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        name varchar(200) NOT NULL,
        description int(11) NOT NULL,
        alt_text int(11) NOT NULL,
        sort_order int(11) NOT NULL,
        time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
        PRIMARY KEY  (id)
      ) $charset_collate;";

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

      add_option( 'db_version', DB_VERSION );

  * on activation the plugin creates databases
  * @access public
  * @since 1.0
  public function activate() {

        // DATABASE
        //creates table
    register_activation_hook( __FILE__, array( &$this, 'create_order_table' ) );

and after I init my class I do this:

global $plugin;

if ( ! $plugin ) {
  $plugin = plugin::getInstance();

but nothing happens, why isn't it taking?

Any help is much appreciated.

  • It's perfectly working for me. Would you please give the full class code?
    – CodeMascot
    Dec 13, 2016 at 17:05
  • sure. but it's kinda long so i'll just give you some of the methods. Dec 13, 2016 at 17:27
  • I updated the question with the class Dec 13, 2016 at 17:38

1 Answer 1


There is an error here. You've not declared _register_hooks() method. And I instantiated the class like below-

global $p;

    $p = plugin::getInstance();

And it worked. For your case that has not worked cause WordPress uses a variable with name $plugin in its life cycle. And your code checks it's empty or not. Obviously it's not gonna be empty. So your $plugin = plugin::getInstance(); never executed. For using this singleton pattern your better declare the variable $plugin with a different name. Like I declared it as $p here. Better you prefix with something unique. Hope that suggestion helps you.

  • I don't understand how it works for you? Dec 13, 2016 at 20:51
  • I have exactly that changed the name and everything and it still won't create a database I look in the logs and nothing shows up. Is there another way to see bugs? Dec 13, 2016 at 20:53
  • when I put register_activation_hook( __FILE__, array( $myy_plugin, 'create_order_table' ) ); inside the if statement it works but not when I call it using the activate() function... Dec 13, 2016 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.