通过扩展类在其外部使用wordpress 4.0


Use wordpress 4.0 outside it by extending a class?

我正试图在wordpress之外的脚本上使用wordpress函数和$wbdb,但我不知道如何做到这一点。

我试过了:

require_once('./wp-load.php' ); // this is the correct path is tested.
class cron extends wpdb {    
  public function results(){
      $sql = 'SELECT sub_id,email,cate_id FROM co_subsriber WHERE status = 0 ORDER BY sub_id ASC LIMIT '.$start.',750'; // $start =0
      $records = $wpdb->get_results($sql);
   }
}

我得到错误

Warning: Missing argument 1 for wpdb::__construct(), called in wp-db.php on line 578
Warning: Missing argument 2 for wpdb::__construct() called in wp-db.php on line 578
Warning: Missing argument 3 for wpdb::__construct() called in wp-db.php on line 578
Warning: Missing argument 4 for wpdb::__construct() called in wp-db.php on line 578
Notice: Undefined variable: dbuser wp-db.php on line 602 and all other pass, hostname...

无法选择数据库。。。。

我需要提到

require_once('./wp-load.php' );

并且使用简单的PHP,没有带类的OOP,它工作得很好。

那么我到底应该扩展什么类呢?

问题是您没有用正确的参数调用wpdb类的构造函数。

你需要做这样的事情:

class cron extends wpdb {
  function __construct() {
    parent::__construct( /* params here */ )
  }
}

但这是完全不必要的,因为$wpdb已经在wp-load.php 中安装

只需这样做:

require_once('./wp-load.php' );
class Cron {
  private $wpdb;
  function __construct( $wpdb ) {
    $this->wpdb = $wpdb;
  }
  public function results() {
    $sql = 'SELECT sub_id,email,cate_id FROM co_subsriber WHERE status = 0 ORDER BY sub_id ASC LIMIT '.$start.',750'; // $start =0
    $records = $this->wpdb->get_results($sql);
  }
}

现在你安装你的类:

$cron = new Cron( $wpdb );