Link Widget Titles to a Page
Adds a "Title link" field to every classic widget, turning its title into a link, for example from "Latest News" to your news page.
Opens WPIDE on
Code
add_action( 'in_widget_form', function ( $widget, $return, $instance ) {
$id = $widget->get_field_id( 'wpide_title_link' );
echo '<p><label for="' . esc_attr( $id ) . '">Title link (optional):</label> <input type="url" class="widefat" id="' . esc_attr( $id ) . '" name="' . esc_attr( $widget->get_field_name( 'wpide_title_link' ) ) . '" value="' . esc_attr( isset( $instance['wpide_title_link'] ) ? $instance['wpide_title_link'] : '' ) . '" placeholder="https://" /></p>';
}, 10, 3 );
add_filter( 'widget_update_callback', function ( $instance, $new_instance ) {
$instance['wpide_title_link'] = isset( $new_instance['wpide_title_link'] ) ? esc_url_raw( trim( $new_instance['wpide_title_link'] ) ) : '';
return $instance;
}, 10, 2 );
add_filter( 'widget_title', function ( $title, $instance = array() ) {
if ( is_admin() || '' === (string) $title || empty( $instance['wpide_title_link'] ) ) {
return $title;
}
return '<a href="' . esc_url( $instance['wpide_title_link'] ) . '">' . $title . '</a>';
}, 20, 2 );
Notes
Core widgets show the link; third-party widgets that escape their own titles may show the title as plain text.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.