What is the correct way to build a widget using OOP

You can simply put your init code within the constructor of the class. For example: class myWidget extends WP_Widget{ function myWidget(){ // Init code here } function widget( $args, $instance ) { // The widget code wp_enqueue_script(…); wp_enqueue_style(…); } // Over methods… } register_widget(‘myWidget’); My preference is to actually put the enqueue calls within the … Read more

Sharing Dynamic Sidebars across Multisite Blogs

Unfortunately, the switch_to_blog() method isn’t going to work for this purpose. switch_to_blog() is really only a partial switch – it makes some modifications to $wpdb that help with database queries. But it’s not a complete switch in the way you might imagine. In particular, dynamic_sidebar() depends on global called $wp_registered_sidebars. This global is populated by … Read more

Using wp_category_checklist in a widget

The problem is that to make the update method of widget class work, the name inputs on the form method should be set via $this->get_get_field_name(‘name_of_the_field’); but wp_category_checklist has no argument to setup the name of the inputs (checkboxes). However, wp_category_checklist uses a walker class to print the checkboxes and allow customizing it. By default the … Read more

Get number of widgets in sidebar

After some research and based on the answer from Eugene Manuilov I made a function that adds custom classes to widgets in a specific sidebar (‘sidebar-bottom’ in my case) based on the number of widgets set in that sidebar. This will suit perfectly in horizontal sidebars and themes based on twitter bootstrap that need spanX … Read more

Code for Recent Posts Widget

The default Recent Posts Widget code is in includes/default-widgets.php but you should not be hacking Core code. Copy that function to your theme’s functions.php, rename it, and create your own customized widget.