The Drupal link function is a handy and easy way to create hyperlinks in your module or theme.

The syntax for the link function is:

print l(<em>anchor text</em>, <em>path</em>);

This is an example of a simple link that might exist in a theme template file:

print l('See full post', 'node/'.$node->nid);

The above example would create the following hyper link (assuming that the given $node->nid is 1):

<a href="node/1">See full post</a>


You can easily add attributes to the link. For example, lets say you want to add a class to your anchor link called “see-more” and you want to add target="_blank"

Drupal 5:

$attributes = array( 'class' => 'see-more', 'target' => '_blank' );
print l('See full post', 'node/'.$node->nid, $attributes);

Drupal 6:

$options['attributes'] = array( 'class' => 'see-more', 'target' => '_blank' );
print l('See full post', 'node/'.$node->nid, $options);

The above examples would create the following hyper link:

<a href="node/1" class="see-more" target="_blank">See full post</a>

External links

If the link is external, then use the full URL including the http://. For example:

print l('Google', '');

This would create:

<a href="">Google</a>

Why bother?

It is generally easier to create a hyper link using the link l() function that typing out the full html for an anchor link. Using the link function is more secure because check_plain and check_url will run automatically, which helps prevent xss attacks. And finally, active classes are automatically assigned when using the link function.

For more information, see the Drupal API page on the Drupal link function