WordPress blogroll link order – Change widget behaviour

Print Friendly, PDF & Email

Another quickie here. Although people use various blogroll plugins, sometimes the standard widget has it’s place (some themes don’t allow custom code to be used in some areas of your blog, for example). The problem is that the standard widget only displays links in alphabetical order based on the link name. There are two ways you can correct this.

1.) Change the default behaviour of the script that produces the blogroll lists

No problem, just go along to /wp-includes/bookmark-template.php – and find the code around line 198

function wp_list_bookmarks($args = '') {
	$defaults = array(
		'orderby' => 'rating', 'order' => 'DESC',
		'limit' => -1, 'category' => '', 'exclude_category' => '',
		'category_name' => '', 'hide_invisible' => 1,
		'show_updated' => 0, 'echo' => 1,
		'categorize' => 1, 'title_li' => __('Bookmarks'),
		'title_before' => '<h2>', 'title_after' => '</h2>',
		'category_orderby' => 'name', 'category_order' => 'ASC',
		'class' => 'linkcat', 'category_before' => '<li id="%id" class="%class">',
		'category_after' => '</li>'

You can see here, I have already changed the default behaviour to order the list by rating, in descending order. So, I then just applied a rating of 10 to link items I want at the top of my list, and 0 to the ones at the bottom! Simple.

Valid values for the orderby field are:

  • id
  • url
  • name
  • target
  • description
  • owner
  • rating
  • updated
  • rel
  • notes
  • rss
  • length
  • rand – Display bookmarks in random order.

The order field can be set to the following valid values : ASC (ascending), or DESC (descending)

2.) Change the standard widget code to submit extra parameters

Edit the file /wp-includes/default-widgets.php and go look for the Links Widget Class (line 90 for WP v2.9.2). Look a little further down and you will see the call to the function wp_list_bookmarks.

Here is the block of code involved, with a couple of extra array parameters amended to the end to control the sort criteria and ordering:

wp_list_bookmarks(apply_filters('widget_links_args', array(
			'title_before' => $before_title, 'title_after' => $after_title,
			'category_before' => $before_widget, 'category_after' => $after_widget,
			'show_images' => $show_images, 'show_description' => $show_description,
			'show_name' => $show_name, 'show_rating' => $show_rating,
			'category' => $category, 'class' => 'linkcat widget',
			'orderby' => 'rating', 'order' => 'DESC'

You can see where I have added the orderby and order array values. Be careful to include the comma at the end of the previous line otherwise you will get syntax errors.

Again, you can use all of the values for the orderby field that are shown above for method 1.


Tags: ,

4 Responses to “WordPress blogroll link order – Change widget behaviour”