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 ID for your widget (if targeting a specific widget class).

Leave a Comment