I love WordPress’ sidebar widgets. I also despise coding them.
I love how they let me offload menial management tasks directly to clients, avoiding all the “Change this word to another word!” e-mails. But every time I code them, it seems to involve 15 minutes of Googling, copy-pasting from a previous widget, and looking at documentation.
So I created this TextMate “snippet” to make it easier:
class ${1:PREFIX_Name}_Widget extends WP_Widget { function $1_Widget() { \$widget_ops = array( 'classname' => '${2:CSS class name}', 'description' => '${3:Description}' ); \$this->WP_Widget( '$2', '${4:Title}', \$widget_ops ); } function widget( \$args, \$instance ) { extract( \$args, EXTR_SKIP ); echo \$before_widget; echo \$before_title; echo '$4'; // Can set this with a widget option, or omit altogether echo \$after_title; // // Widget display logic goes here // echo \$after_widget; } function update( \$new_instance, \$old_instance ) { // update logic goes here \$updated_instance = \$new_instance; return \$updated_instance; } function form( \$instance ) { \$instance = wp_parse_args( (array) \$instance, array( ${5:array of option_name => value pairs} ) ); // display field names here using: // \$this->get_field_id('option_name') - the CSS ID // \$this->get_field_name('option_name') - the HTML name // \$instance['option_name'] - the option value } } add_action( 'widgets_init', create_function( '', "register_widget('$1_Widget');" ) );
Just create a WordPress TextMate bundle, create a new snippet, paste in that code and give it a trigger like wpwidget. Then just type that trigger, press TAB, and you’ll be in the first field. Type, hit TAB to go to the next field. Places where you enter the same thing twice are handled—you only have to enter it once. There are some helpful comments that’ll guide you through the rest once you’ve filled out the basics. I find that with this snippet, I can code up a new widget in a couple of minutes, tops. This is definitely going to make it more likely for me to create a widget for a client instead of just cheating and editing their theme by hand myself.
Before I asked I took at look…yep can use in Espresso too! 😀
Code snippets FTW
Wow! That’s great that they went with TextMate’s snippet formatting instead of making their own.
It looks to be close. I might have to fill in some of it by hand. A lot easier though!
Oooh I love it when people take things I have been meaning to do forever and do them for me.
Thanks a lot.
Works well with Gedit for Gnome also. Great job. Thanks!
Nicely done! I don’t do much WP coding these days but always appreciate using TextMate to shave off time. I still have a dusty TextMate book I need to read… just learned a few weeks ago I can hit ctrl+shift+> to cycle through various for rails.
cheers,
quite useful!
Nice. Mind if I integrate that in the WordPress.tmbundle? ( http://github.com/Gipetto/wordpress.tmbundle ).
This is not spam, I’d really like to drop this in to our bundle.
Shawn, that’d be great!
Cool, its been added. I’ve attributed you on the Wiki page on GitHub and will also put that in an about/help popup in the Bundle when I get that added.
Thanks again.
Thanks, Mark, it is awesome addition to Yoast’s TextMate WordPress bundle, which is my most used thingie.
Shame that those aren’t available for my love – Coda.
Thanks for this. TextMate snippets are hugely time-saving. This is a great one.
Those without TextMate can do the some type of thing with an online tool I created over at Widgetifyr.com. You can created the 2.8 style class as done here or the previous no class based style from the same base widget code.
Awesome tutorial, except I have no idea what you’re talking about. What do you mean by ‘create a widget for a client instead of just cheating and editing their theme by hand myself’?
I’d really love some clarification on that!
That’s in an about/help popup in the Bundle when I get to added it.
nice thanks a lot this well help me after I learn how to use wordpress 😀