Support » Fixing WordPress » How to make any widget title clickable ??

  • Resolved Kartik Shete

    (@skartik)


    Hello everyone,

    I have different widgets not only text widgets. I want widget title click able. I have added Anchor tag in widget title but wordpress strips the HTML tags in widget title.

    How do i do that ??

    Or how to add extra field say “title link” to every widget & make it work ?

    Thanks in advance.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You could try the Plugin mentioned in this post: (not sure if it is still up-to-date etc.): http://wordpress.org/support/topic/make-text-widget-header-a-link?replies=5

    Or, you could try the ‘widget_title’ filter hook, but this assumes every widget class, in your theme/installed Plugins, defines this filter. In practice, only the widgets defined in the WordPress core are likely to support this filter consistently.

    Sorry David, it didn’t help me. But thanks for your support.

    As a test add this to your themes functions.php file:

    function widget_title_link( $title ) {
    	return "<a href=\"http://www.mysite.com\">".$title."</a>";
    }
    add_filter( 'widget_title', 'widget_title_link' );

    For all widgets (at least for all the WordPress core widgets) your title will now be turned into a link pointing to a fixed link.

    If you want a different link for each widget then you can use conditionals. Something like:

    function widget_title_link( $title ) {
        if( $title == "Categories" ) {
            return "<a href=\"http://www.mysite.com\">".$title."</a>";
        }
        elseif( $title == "Pages" ) {
            return "<a href=\"http://www.another.com\">".$title."</a>";
        }
        else {
            return "<a href=\"http://www.default.com\">".$title."</a>";
        }
    }
    add_filter( 'widget_title', 'widget_title_link' );

    the answer from Maxaud here works really great.

    Basically, enter the title of widget like so:

    http://url.com|Widget Title Here

    And also you need to add this php:

    <?php
    /*
    Plugin Name: Custom Widget Title Links
    Plugin URI: http://www.playforward.net/
    Description: Allows you to define a link that is wrapped around widget titles.
    Version: 1.0
    Author: Playforward | Dustin Dempsey
    Author URI: http://www.playforward.net/
    */
    
    function custom_widget_link( $title ) {
    
    	// assume there's a link attached to the title because it starts with ww, http, or /
    	if ( ( substr( $title, 0, 4) == "www." ) || ( substr( $title, 0, 4) == "http" ) || ( substr( $title, 0, 1) == "/" ) ) {
    
    		// split our title in half
    		$title_pieces = explode( "|", $title );
    
    		// if there's two pieces
    		if ( count( $title_pieces ) == 2 ) {
    
    			// add http if it's just www
    			if ( substr( $title, 0, 4) == "www." ) {
    				$title_pieces[0] = str_replace( "www.", "http://www.", $title_pieces[0] );
    			}
    
    			// create new title from url and extracted title
    			$title = '<a href="' . $title_pieces[0] . '" title="' . $title_pieces[1] . '">' . $title_pieces[1] . '</a>';
    		}
    	}
    
    	return $title;
    }
    add_filter( "widget_title", "custom_widget_link" );
    
    ?>

    Thank you very much jj9delacruz. I already manage to achieve this somehow above way.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to make any widget title clickable ??’ is closed to new replies.