Is there any way to dynamically alter widget titles?

You can use the widget_display_callback (fired, predictably, just prior to displaying a widget 🙂 ). add_filter(‘widget_display_callback’,’wptuts54095_widget_custom_title’,10,3); function wptuts54095_widget_custom_title($instance, $widget, $args){ if ( is_single() ){ //On a single post. $title = get_the_title(); $instance[‘title’] = $instance[‘title’].’ ‘.$title; } return $instance; } The $widget argument is an object of your widget class, and so $widget->id_base will contain the … Read more

How to enqueue script if widget is displayed on page?

You should be able to call wp_enqueue_script() as part of your Widget output. Edit Quick-and-dirty, using the bare-bones Widgets API class example: <?php class wpse48337_Widget extends WP_Widget { public function __construct() { // widget actual processes } public function form( $instance ) { // outputs the options form on admin } public function update( $new_instance, … Read more

Limit the number of inactive widgets

Tested under v3.2.1: $sidebars = wp_get_sidebars_widgets(); if(count($sidebars[‘wp_inactive_widgets’]) > 10){ $new_inactive = array_slice($sidebars[‘wp_inactive_widgets’],-10,10); // remove the dead widget options $dead_inactive = array_slice($sidebars[‘wp_inactive_widgets’],0,count($sidebars[‘wp_inactive_widgets’])-10); foreach($dead_inactive as $dead){ $pos = strpos($dead,’-‘); $widget_name = substr($dead,0,$pos); $widget_number = substr($dead,$pos+1); $option = get_option(‘widget_’.$widget_name); unset($option[$widget_number]); update_option(‘widget_’.$widget_name,$option); } // save our new widget setup $sidebars[‘wp_inactive_widgets’] = $new_inactive; wp_set_sidebars_widgets($sidebars); } The above code limits the … Read more

What is the difference between wp_register_sidebar_widget and register_widget?

wp_register_sidebar_widget() is part of the old widgets API. Sidebar widgets used to be built procedurally … in a non-reusable fashion (i.e. you could only ever have one of each). register_widget() was introduced with the new Widgets API and takes an object/class as an input rather than actual widget parameters. WordPress can instantiate as many copies … Read more