A beginner's guide to using snippets

Last modified: March 9, 2009 - 23:23

A snippet is a small piece of code that you can use to provide an extra function or to modify the appearance of your site. Code snippets are powerful because they often let you do things which are impossible using the Drupal administration interface or using add-on modules. You can use a snippet even if you don't understand the code or the programming language in it. There are several kinds of snippets (for example, PHP, CSS, JavaScript and SQL) and there are many different ways to add them to your site.

The method you use to add the snippet is important. A snippet probably won't give the expected results if it's in the wrong place. Many snippets are provided without clear details of how to use them - the snippet author just assumes you know what to do with that kind of snippet. So you may need to do some research, or ask how to use it.

To be safe, before you add a new snippet, always make backups of any files or content you are editing (if you're paranoid, or adding an SQL snippet, back up your database as well)

Adding PHP snippets

There are several ways to add PHP snippets.

Adding a PHP snippet by editing a PHP file

Many PHP snippets are added by directly editing a PHP file in your Drupal directory. To do this, you need access to the files (for example, using Telnet/SSH, FTP, or Cpanel's File Manager).

After you add a snippet for the first time, you may see this error:
Parse error: syntax error, unexpected '<'

This error usually occurs because unwanted php tags were not removed from the new snippet. On Drupal.org PHP snippets are usually presented with '<?php' at the beginning, and '?>' at the end. They look like this:

<?php
/** The actual snippet starts on this line. But this line is just a comment, it doesn't do anything */
function do_very_complicated_stuff ($drupal) {
 
$inexplicable = confusing(
etc...

/** The snippet ends here. This line is just a comment */
?>

PHP snippet authors put these tags in because it makes Drupal.org display PHP with helpful colored syntax highlighting. To prevent PHP syntax errors, when you add the snippet, delete these two PHP tags from the beginning and the end of the snippet (don't delete any other <?php or ?> tags in the snippet or in the file you are editing).

Adding PHP snippets through the Drupal user interface

Sometimes you don't need to directly edit files to add a PHP snippet. You can use the administration interface to insert PHP snippets into certain sections of the page. For example, PHP snippets can be included in specific blocks, nodes or views in this way. Some PHP snippets are designed to be used in this way and may not produce the expected results otherwise.

To insert PHP snippets this way, you need to make sure the PHP input filter is enabled. Go to Administer › Site building › Modules, and check PHP Filter under core - optional.

Now when you create new content, a PHP option should be available under input formats - you must select this when you paste in a PHP code snippet. If this option does not appear, the account you are using may not have permission to use this format, go to Administer › Site configuration › Input formats and select configure next to the PHP code option. You might also need to visit Administer › User management › Permissions. Only allow trusted administrators to use the PHP input format.

If you've replaced the standard Drupal text area boxes with a rich text editor such as FCKedit or TinyMCE, click the Source (FCKedit) or HTML (TinyMCE) option on the editor's toolbar before you paste in PHP code. If you don't do this, you'll just see the PHP code itself appear on the page.

You probably do not need to remove the initial and closing PHP tags when you add a PHP snippet to your site this way. For example, PHP snippets used in 'Page specific visibility settings' for blocks do require the tags.

See also: How to insert and use the PHP Snippets in your pages. (from 2005 - may be out of date) http://drupal.org/node/23223
How to use views snippets http://drupal.org/node/75959
How to use PHP Snippets with the front_page.module http://drupal.org/node/23224

Adding more than one PHP snippet

Using multiple PHP snippets. (This is from 2005, so it may be out of date. The comments provide more up-to-date information): http://drupal.org/node/24258

 

How to add SQL snippets

How to use an SQL snippet: http://drupal.org/node/41618
This section is a work in progress. Please add to it. You can post a comment, or just edit this page yourself.

 

 

How to add JavaScript Snippets

This section is incomplete. Please add to it by posting a comment or editing this page.

There are several ways to add JavaScript snippets to your site. They can be embedded into an existing html page, added to an existing .js file, or added as separate files which are called when the HTML page loads.

Adding JS as a separate, external file

In this method, you upload the JavaScript to your site as a individual file, and add a PHP code snippet to a .php file. (When a visitor views the page, this PHP snippet creates HTML code which makes their browser load the .js file). The PHP snippet will look something like this:

<?php
print drupal_add_js('path-to/javascript.js', 'theme');
?>

This method is often used to load .js files by adding that PHP snippet to the end of template.php in your site's theme directory.

See also: How can I add a Javascript? http://drupal.org/node/333056

Troubleshooting JavaScript Snippets

In FireFox, use Tools > Error Console. Clear the error console and then look at it when you load the page. For example, it will let you know if an external .js file didn't load because you got the path wrong. Note: some errors can be safely ignored.

 

About this page


I hope this can be a central source for simple and easy-to-understand instructions about how to add the most common kinds of snippets to a site. This is a work in progress. I don't really know very much about this subject, so please help by correcting errors and adding more information. Please edit this page yourself, or, if you add a comment, I'll include the information in the page bsimon

 
 

Drupal is a registered trademark of Dries Buytaert.