Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7575211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:37:52+00:00 2026-05-30T16:37:52+00:00

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

  • 0

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! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:37:53+00:00Added an answer on May 30, 2026 at 4:37 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I would like to run a str_replace or preg_replace which looks for certain words
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.