Add a category filter to WordPress search

WordPress has a simple function to build a search form for your blog and in this tutorial I’ll show you how to add a category filter to it.

<?php get_search_form(); ?>

This function will look for a file called searchform.php inside your template folder: if it doesn’t exist it will output the standard search form. So, if it isn’t already in place, create your custom searchform.php and copy into it the default search form output. It should look similar to this:

<form role="search" method="get" id="searchform" action="<?php bloginfo('siteurl'); ?>">
    <label class="screen-reader-text" for="s">Search for:</label>
    <input type="text" value="" name="s" id="s" />
    <input type="submit" id="searchsubmit" value="Search" />

We want to add a select box to let the user search in a specific category or in all tags: to achive this we’ll use another default WordPress function.

The wp_dropdown_tags function will build for you a select box with all your tags. Take a look at the documentation for all the available options.

<?php wp_dropdown_tags( 'show_option_all=All tags' ); ?>
<!-- This is a sample output of the function -->

<select name='cat' id='cat' class='postform' >
  <option value='0' selected='selected'>All tags</option>
  <option class="level-0" value="1">Uncategorized</option>
  <option class="level-0" value="3">HTML &amp; CSS</option>
  <option class="level-0" value="4">Java</option>
  <option class="level-0" value="5">Links</option>
  <option class="level-0" value="6">Linux</option>
  <option class="level-0" value="9">Javascript</option>
  <option class="level-0" value="32">XML</option>

Now add the category select box to the form modifying your searchform.php like this:

<form role="search" method="get" id="searchform" action="<?php bloginfo('siteurl'); ?>">
    <label class="screen-reader-text" for="s">Search for:</label>
    <input type="text" value="" name="s" id="s" /> 
    in <?php wp_dropdown_tags( 'show_option_all=All tags' ); ?> 
    <input type="submit" id="searchsubmit" value="Search" />

That’s it. It just works out of the box.

A finishing touch

Now that you have a nice category filter in your search form it would be great to modify the search result page and add something like Search result for “foo” in category “bar”. Well, it’s quite easy: we need do add a simple function to the function.php file. Remember that every function in this file will be automatically available in your theme.

This is a simple function that gets the cat parameter from the request and if it’s not null it searches the category name. It has two input parameters so you can pass two strings to be displayed before and after the category name. If cat is null or empty or “0” (All tags) it will return an empty string.

function getCatSearchFilter($pre,$post){
  $category = "";
  $catId = htmlspecialchars($_GET["cat"]);

  if ($catId != null && $catId != '' && $catId != '0'){
    $category = $pre.get_cat_name($catId).$post;

  return $category;


Now open you search.php, add the function call to the page and you are done:

<h1>Search Results for <?php echo(get_search_query());?><?php echo(getCatSearchFilter(' in category ',''));?></h1>

Update 31.12.2011

Finally I wrote a blog post on adding a Multiple tags filter in WordPress search. Check it out!

When Runtime.exec() won't - JavaWorld

A well written article on JavaWorld that explains how to effectively use the Runtime.exec() method. It really saved me a lot of time.

When Runtime.exec() won’t - JavaWorld

Javascript getElementById() shortcut

Do you love the wonderful javascript dollar shortcut $('myId') but cannot use jQuery or Prototype? Don’t worry, you can define your custom shortcut for the too long document.getElementById() function:

function $(id) {
	return document.getElementById(id);

Now you can sobstitute this:

var divContent = document.getElementById('myId').innerHTML;

with this:

var divContent = $('myId').innerHTML;

Obviously you don’t get all the bells and whistles that you would get using jQuery, but it’s a start.

Get location coordinates using Google Maps

This article uses **v2** version of Google Maps API. You sould use [v3][5] by now

This is a simple tutorial on finding location’s coordinates using Google Maps APIs.

First of all you need to signup for a Google Maps API key, otherwise your script will not work. When you are done, start building a simple form with three fields: one for the location and the others for coordinates display.

  <input name="address" type="text" />
  <input onclick="getCoordinates()" type="button" value="Get coordinates" />
  <input name="lat" type="text" />
  <input name="lng" type="text" />

Now, we need to define the getCoordinates() function: this function must read the value of the address field, check if it’s valid and get its coordinates. To get coordinates we’ll use the GClientGeocoder class and its method getLatLng(address:String, callback:function):

Sends a request to Google servers to geocode the specified address. If the address was successfully located, the user-specified callback function is invoked with a GLatLng point. Otherwise, the callback function is given a null point. In case of ambiguous addresses, only the point for the best match is passed to the callback function.

var geocoder = new GClientGeocoder();

function getCoordinates(address){

  var address = document.myForm.address.value;

  if (address != ''){


      if (point != null){ =;
        document.myForm.lng.value = point.lng();

        alert("Location not found");
  } else {
    alert("Please insert a location");

Compact font styles

This is a simple tip that can help you reduce your style sheet size. Take a look at this CSS portion:

	font-family: Arial, Verdana, Helvetica, sans-serif;
	font-size: 1.1em;
	font-weight: bold;
	font-style: italic;
	line-height: 1.5em;
	font-variant: uppercase;

All these properties can be condensed into a one row expression using this syntax:

font: fontSize/lineHeight weight style variant family;

See the example below:

	font: 1.1em/1.5em bold italic uppercase Arial,Verdana,Helvetica,sans-serif;

Just remember that this syntax will only function if you specify both font-size and font-family.

Remove leading zeroes in XSL

A useful XSL template for removing leading zeros

<xsl:template name="removeLeadingZeros">  
  <xsl:param name="originalString"/>  
    <xsl:when test="starts-with($originalString,'0')">  
      <xsl:call-template name="removeLeadingZeros">  
        <xsl:with-param name="originalString">  
          <xsl:value-of select="substring-after($originalString,'0' )"/>  
      <xsl:value-of select="$originalString"/>  

Just insert it into your XSL file and use it as usual:

<xsl:call-template name="removeLeadingZeros">  
  <xsl:with-param name="originalString" select="$myOriginalString"/>  

Zipped folder backup

A simple bash script that zips a folder and sets the archive name to the current date.


# Archive name structure:

archiveName=`date +%d%m%Y_%H%M%S`.zip


zip -r $archiveName $folderName

Install OpenOffice 3.1 on Ubuntu

These are the steps to install OpenOffice 3.1 on Ubuntu. First of all download OpenOffice 3.1 from the official web site. When you’re done, follow these steps:

1. Remove previous version

sudo apt-get remove openoffice*.*

2. Remove settings folder This will remove ALL your OpenOffice settings

rm ~/ -rf

3. Expand the archive

tar -xvzf OOo_3.1.0_LinuxIntel_install_en-US_deb.tar.gz

4. Setup

cd OOO310_m11_native_packed-4_en-US.9399/DEBS/

sudo dpkg -i *.deb

5. Setup desktop integration

cd desktop-integration

sudo dpkg -i openoffice.org3.1-debian-menus_3.1-9393_all.deb

Java tail

A java implementation of the Unix tail command. This is a small customization of the java tail class created by Luigi Viggiano. I’ve just added some controls and added the update time parameter.

 * Java implementation of the Unix tail command
 * @param args[0] File name
 * @param args[1] Update time (seconds). Optional. Default value is 1 second
 * @author Luigi Viggiano (original author)
 * @author Alessandro Melandri (modified by)
 * */
public class Tail {
  static long sleepTime = 1000;
  public static void main(String[] args) throws IOException {
    if (args.length > 0){
      if (args.length > 1)
        sleepTime = Long.parseLong(args[1]) * 1000;
      BufferedReader input = new BufferedReader(new FileReader(args[0]));
      String currentLine = null;
      while (true) {
        if ((currentLine = input.readLine()) != null) {
        try {
        } catch (InterruptedException e) {
    } else {
      System.out.println("Missing parameter!\nUsage: java JavaTail fileName [updateTime (Seconds. default to 1 second)]");

This is also a Gist. Check it out!

VMWare and Linux

If you install VMware on linux you may find that some keys (up/down keys, home key, etc…) don’t work in the guest operating system. You need to create a config file inside the .vmware folder in your home directory and paste this text info the file:

xkeymap.keycode.108 = 0x138 # Alt_R
xkeymap.keycode.106 = 0x135 # KP_Divide
xkeymap.keycode.104 = 0x11c # KP_Enter
xkeymap.keycode.111 = 0x148 # Up
xkeymap.keycode.116 = 0x150 # Down
xkeymap.keycode.113 = 0x14b # Left
xkeymap.keycode.114 = 0x14d # Right
xkeymap.keycode.105 = 0x11d # Control_R
xkeymap.keycode.118 = 0x152 # Insert
xkeymap.keycode.119 = 0x153 # Delete
xkeymap.keycode.110 = 0x147 # Home
xkeymap.keycode.115 = 0x14f # End
xkeymap.keycode.112 = 0x149 # Prior
xkeymap.keycode.117 = 0x151 # Next
xkeymap.keycode.78 = 0x46 # Scroll_Lock
xkeymap.keycode.127 = 0x100 # Pause
xkeymap.keycode.133 = 0x15b # Meta_L
xkeymap.keycode.134 = 0x15c # Meta_R
xkeymap.keycode.135 = 0x15d # Menu