WP Widget : Random Links

WordpressI have a nice list of Widgets to share, and on top of that I am willing to create widgets on demand, since some people love to have something in a sidebox but seem to struggle to get this content there in a simple way, I thought it would be nice to create widgets you can control easily from the widget-panel inside the wp-admin. The first widget I’m going to share is the LinkLove Widget. It is similar to the build in links widget but this version is different as it will show an X amount of random links from your complete blogroll.First of all we have to edit the functions.php that can be found in your themes folder. open it and add this line:

<?php include (TEMPLATEPATH . '/widgets.php'); ?>

Now we have to create a file where we are going to store our custom widgets. name it widgets.php
good, open the file and enter the following code.

<?php
/**********************************************************
Widget for Wordpress 2.X By Sebastiano Bellinzis
http://sebastiano.ezion.cc
Please do not delete ..thank you!
***********************************************************/

// Main function that we will initialize later on.
function widget_linklove_register() {
	if ( function_exists("register_sidebar_widget") ) :

	// this function will draw the content (In this case the random links)
	function widget_linklove($args){
		extract($args);
		$options = get_option("widget_linklove");
		$buff ="";

		$buff.= $before_widget;
		$buff.= $before_title . $options["title"] . $after_title;
		$buff.= "<ul>";
		echo $buff;	

		get_links("-1", "<li>", "</li>", "", FALSE, "rand", FALSE, FALSE, $options["count"], TRUE);

		$buff= "</ul>";
		$buff.= $after_widget;
		echo $buff;
	}

	// This function will draw the tiny menu in the admin panel , we can edit NAME and COUNT(quantity of links)
	function widget_linklove_control() {
		$options = $newoptions = get_option("widget_linklove");

		if ( $_POST["linklove-submit"] ) {

			// set the title
			$newoptions["title"] = strip_tags(stripslashes($_POST["linklove-title"]));
			if ( empty($newoptions["title"]) ) $newoptions["title"] = "LinkLove";
			//quantity of links
			$newoptions["count"] = $_POST["linklove-count"];
			if ( is_int($newoptions["count"])  ) $newoptions["count"] = "10";
		}

		if ( $options != $newoptions ) {
				$options = $newoptions;
				update_option("widget_linklove", $options);
		}

		$title = htmlspecialchars($options["title"], ENT_QUOTES);
		$count = $options["count"];

		$buff=	 "<p>"
				."<label for=\"linklove-title\">"._e('Title:').""
				."<input style=\"width: 250px;\" id=\"linklove-title\" name=\"linklove-title\" type=\"text\" value=\"".$title."\" 

/></label></p>"

				."<p><label for=\"linklove-count\">"._e('How Many Links:').""
				."<input style=\"width: 50px;\" id=\"linklove-count\" name=\"linklove-count\" type=\"text\" value=\"".$count."\" 

/></label></p>"

				."<input type=\"hidden\" id=\"linklove-submit\" name=\"linklove-submit\" value=\"1\" />";

		echo $buff;
	}	

	// define the 2 functions from above (Wordpress needs to know what to use and where and how and why!)
	register_sidebar_widget("Linklove", "widget_linklove", null, "linklove");
	register_widget_control("Linklove", "widget_linklove_control", null, 75, "linklove");
	endif;
}

/**********************************************************
Keep the functions above this line

Keep main execution commands below this line
***********************************************************/

// Here we initialize the function from above. we add it to INIT so the function will be executed together with the main functions in WP.
add_action("init", "widget_linklove_register");
?>
  1. save the files
  2. upload to your server
  3. go to your admin panel
  4. Presentation Tab
  5. widget Tab
  6. drag and drop the LinkLove Widget into the sidebox
  7. Name it, and define a quantity of links that should de shown
  8. save it.

And thats all! enjoy and if you encounter any problem or bug, don’t hesitate to leave a comment!

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • Furl
  • Live
  • Ma.gnolia
  • NewsVine
  • StumbleUpon
  • Technorati
  • TwitThis
  • Chawlk
  • Propeller
  • Ycombinator
Tags: , ,

Leave a Reply