Add Links to Widgets

Sometimes you need the titles of your widgets to be links. WordPress doesn’t natively offer an easy way to do this. There are several plugins that do this successfully, but I find they get cluttered with requests to donate and, often, further confuse my clients rather than make things super simple. So, after struggling with this for quite some time and just resolving to having to modify the code of those plugins to manually remove the donate requests, update requests, extensive title lengths, etc., I finally looked into just adding some links to the widgets myself. Turns out, it’s easier than it seems.

Open your functions.php file and place the following:

// Add links to all widgets, so I don't have to use a plugin
// Let's trying dynamically adding icon forms to the all widgets.
function link_widget_form_extend( $instance, $widget ) {
	if ( !isset($instance['linknew']) )
		$instance['linknew'] = null;

	$row = "<p>\n";
	$row .= "\t<label for='widget-{$widget->id_base}-{$widget->number}-classes'>Link</label>\n";
	$row .= "\t<input type='text' name='widget-{$widget->id_base}[{$widget->number}][linknew]' 
id='widget-{$widget->id_base}-{$widget->number}-classes' class='widefat' value='{$instance['linknew']}'/>\n"; $row .= "</p>\n"; echo $row; return $instance; } add_filter('widget_form_callback', 'link_widget_form_extend', 10, 2); function link_widget_update( $instance, $new_instance ) { $instance['linknew'] = $new_instance['linknew']; return $instance; } add_filter( 'widget_update_callback', 'link_widget_update', 10, 2 ); function link_dynamic_sidebar_params( $params ) { global $wp_registered_widgets; $widget_id = $params[0]['widget_id']; $widget_obj = $wp_registered_widgets[$widget_id]; $widget_opt = get_option($widget_obj['callback'][0]->option_name); $widget_num = $widget_obj['params'][0]['number']; if ( isset($widget_opt[$widget_num]['linknew']) && !empty($widget_opt[$widget_num]['linknew']) ) $params[0]['before_title'] .= "<a href='{$widget_opt[$widget_num]['linknew']}'>"; $params[0]['after_title'].= "</a>"; return $params; } add_filter( 'dynamic_sidebar_params', 'link_dynamic_sidebar_params' );

And that’s it. You should now have a link option in each of your widgets.