• Resolved jens_sj

    (@jens_sj)


    Hi,

    I am creating my own widget, which is displaying some information stored in the backend – just as all the “examples” on creating your own custom widget works.

    I now would like to create a page, where I also display some of this information. My idea was to use a shortcode to display the output. In my plugin code I have done the following:

    class GM_Server_Status extends WP_Widget {
    (...)
        public function display_gm_status($atts) {
            extract(shortcode_atts(
                            array(
                'gm' => '',
                            ), $atts)
            );
        }
    }
    add_shortcode('gmstatus', array('GM_Server_Status', 'display_gm_status'));

    I would like to be able to get the $instance values inside the display_gm_status function… But how? Is it possible?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    get the $instance values inside the display_gm_status function

    I don’t see any $instance in your function, plus, as this is the shortcode handler function, you can of course get any values within it, so I’m not sure what you really want.

    The actual widget instances are stored in global $wp_widget_factory->widgets but I suspect that is not what you really want. Your widget is getting values from somewhere outside of the widget instance. I’d recommend you locate that source and have your shortcode get values directly from there.

    I can help you find that if you post your widget code (use pastebin.com and post the link here) and give me a better idea of what values you actually need.

    Thread Starter jens_sj

    (@jens_sj)

    Hi Bcworkz,

    Thank you for taking your time to have a look ??

    I have pasted my widget file here: https://pastebin.com/HUs1HpVC
    On line 177, you can see where I would like to have the $instance variable.

    Moderator bcworkz

    (@bcworkz)

    Thanks for doing that. I see now I could have mostly answered without the entire widget code, but I kinda needed it to get my head on straight, as well fill in a few details.

    While you could probably still get at $instance from the widget factory, I’m leery of doing so, I’m unsure how reliable that source is when your shortcode handler executes, it’s usually before widgets are rendered. Best to go straight to the original source anyway. All widget settings are stored in the options table. Your widget settings should be under ‘widget_gmserverstatus_widget’. Use phpMyAdmin or similar to verify this.

    Use something like $option = get_option('widget_gmserverstatus_widget'); to get all settings related to your widget. It will be in the form of an array. var_dump the returned array to see the array structure and determine just how you would access each value. As a wild guess, it’ll probably be something like $option[1][$gm]['status']. The var_dump will tell you for sure.

    Thread Starter jens_sj

    (@jens_sj)

    That was EXACTLY what I was looking for – thank you so much for your help – much appreciated ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Use widget instance in custom functions’ is closed to new replies.