Where to put $wpdb with OOP plugin / widget using and WP_widget extension Where to put $wpdb with OOP plugin / widget using and WP_widget extension wordpress wordpress

Where to put $wpdb with OOP plugin / widget using and WP_widget extension


$wpdb is just the name of the global instance of Wordpress' database wrapper. Your best bet might be to set your table name in your constructor and include a reference to the global database object as a class parameter, e.g.:

class FeatDispWidget extends WP_Widget {  private     $featdisplayer_table,    $wpdb;  public function __construct() {    global $wpdb;    $this->wpdb = &$wpdb;    $this->featdisplayer_table = $this->wpdb->prefix . 'featdisplayer';  }  // .. the rest of your widget goes here}

You can then go on referring to $this->wpdb in your other class methods.

As for your other questions, you can add additional "widget-ready" regions to your site's theme by using the register_sidebar and dynamic_sidebar functions. Widgets sub-classed from WP_Widget can be re-used on multiple sidebars without additional modifications. If you want to use it on specific pages (i.e., attached to post content), however, a widget really isn't the right solution.