WordPress.org

Support

Support » Plugins and Hacks » Hacks » How to correctly widgetize my plugin with register and actions?

How to correctly widgetize my plugin with register and actions?

  • I’ve been learning how to create plugins after creating one with a admin page and user page that used options I wanted to create it again but make it a widget instead. Can anyone help me figure out where I need to register my widget and add the proper actions etc. I can write the functions to create the output and use the API to look up different hooks to a point but the actions, registering the widget and filters are confusing me. Can’t figure out where I should put them in my plugin so they actually will show the widget allowing it to be dropped into a sidebar and then displayed . I just need some help figuring this out if anyone can take the time. Thanks in advance.

    It’s just a widget that will return the Latest Posts.

    My failed attempt at widgetizing my plugin 🙁
    http://pastebin.com/b9uxSvq4

Viewing 7 replies - 1 through 7 (of 7 total)
  • You are using the old widget API. I would not recommend this. A (not so) new widget API is available since WordPress 2.8. Everything is well explained in the codex.

    Keys are creating a new class which extends WP_Widget and registering the widget using ‘widgets_init’ hook. There are some external links at the bottom of the page which may help you too.

    Thanks, that helps. Even reading the comments for the update function I don’t really understand what it does http://bit.ly/w1N8wY. So I would still use the options page setup or is that what update is supposed to handle. Seems the rest comes over 1:1. Thanks for the help

    Maybe it could be clearer if you look at the default widgets code. You can find the source file in wp-includes/default-widgets.php. I always learn a lot looking at the WordPress core functions.

    So I’ve looked through the widgets and set up my plugin using the 2.8 Widget API to look like this http://pastebin.com/aZTq9ASh. It shows up and it shows the default values when dropped into a sidebar but it doesn’t seem to save any of the options. I’ve got the same setup as the example and the default-widgets.php. I’ve named the form fields the same etc but when I save a new show_number it whips the defaults, refreshing the widget page whips the defaults and they don’t load in the sidebar. I definitely don’t understand how they work or why the same setup didn’t from default-widgets.

    Found it I had used get_field_id in the form elements name instead of get_field_name. Thanks for all the help

    Sorry last question, where would you load styles and scripts. I found this snippet but it doesn’t seem to do the trick in the couple places I’ve tried placing it.

    You have to use wp_enqueue_script and wp_enqueue_style.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to correctly widgetize my plugin with register and actions?’ is closed to new replies.
Skip to toolbar