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

I am new to plugin and WP development, so I need help.

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

It should be used several times, on many pages.

What I decided to do was create my own table in the WP database, although I read here that it might not be necessary in the mail: Developing a WordPress plugin using OOP

I fight in several fields. At first, I had problems to find a worthy explanation of how to create a widget that can be used several times. Ok, I solved it using:

class FeatDispWidget extends WP_Widget {...} 

and it really works, I can have multiple instances and the data is saved by wp_options.

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

this is part of my code, a constructor and an attempt to call a db function that constantly changes errors.

 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 are:

1) Can I extend WP_widget, like me, and display it on both pages and in the sidebar? 2) I need to extend WP_widget to have the widget created several times (once on each page) 3) If I extend WP_widget, where I put $ wpdb to create a table in the wp database

So, this time, I think I did something complicated for my first widget, but I'm not giving up! :)

+4
source share
1 answer

$wpdb is simply the name of a global instance of the Wordpress database shell. It is best to set the table name in your constructor and include a reference to the global database object as a class parameter, for example:

 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 } 

Then you can go to $this->wpdb in the other methods of the class.

For your other questions, you can add additional widget-ready regions to your site’s theme using register_sidebar and dynamic_sidebar . Widgets subclassed from WP_Widget can be reused on multiple sidebars without further changes. However, if you want to use it on certain pages (i.e., Attached to the message), the widget really is not the right solution.

+12
source

Source: https://habr.com/ru/post/1396200/


All Articles