Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm new to plugin and WP development, so I need some help.

What I'm trying to do is plugin that would use/reuse certain images from media folder and display them in side widget, per certain page, in a way that I like, or using shortcodes within certain page. (so should work on both posts\pages and sidebar widgets).

It should be used multiple times, on many many pages.

What I've decided to do is create my own table in WP database, even though I've read here on post that it is maybe not needed: WordPress plugin development using OOP

I struggle on several fields. First I had problems to find decent explanation how to create widget that can be used multiple times. OK, I have resolved this using:

class FeatDispWidget extends WP_Widget {...}

examples, and it realy works, I can have multiple instances and data is saved do wp_options.

Now, I'm trying to use $wpdb. And from all possible examples i see I have to use global $wpdb or include some php files, or extend wpdb with my own DB class, but what is the best / correct way in OOP approach ?

this is part of my code, constructor and try to call db function that gives me errors all the time.

class FeatDispWidget extends WP_Widget {

     private $featdisplayer_table;
     $featdisplayer_table = $wpdb->prefix . 'featdisplayer';


/**
 * Widget setup.
 */
function FeatDispWidget() {
    /* Widget settings. */
    $widget_ops = array( 'classname' => 'featdisp', 'description' => __('Sexy Feature Displayer.', 'featdisp') );

    /* Widget control settings. */
    $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'featdisp-widget' );

    /* Create the widget. */
    $this->WP_Widget( 'featdisp-widget', __('Feature Displayer Widget', 'featdisp'), $widget_ops, $control_ops );
}




    function featDispDBsetup (){
        global $wpdb;
        global $featdisplayer_table;


        if ( $wpdb->get_var( "show tables like '$featdisplayer_table'" ) != $featdisplayer_table ) {
    $sql = "CREATE TABLE $featdisplayer_table (".
        "sandf_id INT NOT NULL AUTO_INCREMENT, ".
        "type VARCHAR( 30 ) NOT NULL, ".
        "attachid INT NOT NULL, ".
        "setid INT NOT NULL, ".
        "imgpath LONGTEXT NOT NULL, ".
                    "title LONGTEXT NOT NULL, ".
        "desc LONGTEXT, ".
                    "linkto LONGTEXT, ".
                    "altertext LONGTEXT, ".
                    "txtnxttoimg LONGTEXT, ".
                    "sortorder INT, ".
        ")";

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

    }

So my questions:

1) can I extend WP_widget, as I do, and display it on both pages and sidebar ? 2) do I need to extend WP_widget in order to have widget instantiated several times (once on each page) 3) If I extend WP_widget, where do I put $wpdb in order to create table in wp database

So, this is it for now, I think I took something to complex for my first widget, but I'm not giving up! :)

like image 286
Balkyto Avatar asked Dec 17 '22 04:12

Balkyto


1 Answers

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

like image 112
rjz Avatar answered Feb 15 '23 11:02

rjz