Drupal Tip #6: Removing Unused Taxonomy Terms

One not so great side effect of using free tagging with Drupal is that you will gradually build up a huge list of taxonomy terms that aren't being used. Typically this happens when nodes are delete that have some random taxonomy terms which arent used by any other nodes. There is a simple SQL statement you can run that will remove all unused taxonomy terms:

DELETE FROM term_data USING term_data LEFT JOIN term_node ON term_data.tid = term_node.tid WHERE term_node.tid IS NULL AND term_data.vid = vocabulary_id_#

Remember to add the Vocabulary ID you want to clean up. You wouldn't want to accidentally remove terms from a large static vocabulary. I use the SQL Cron module to run this statement automatically everytime cron.php is executed. This way I never have to worry about manually cleaning up my vocabularies.

Drupal Tip #5 - Removing the Date and Author from Nodes

By default when you create a new page or story in Drupal you will see the date the node was created along with the author of the node. Depending on your site you may want to hide this information. Luckily this is easily accomplished.

First go to the theme administration page at yoursite.com/admin/build/themes. Then click on the "Configure" tab at the top of the page. Over on the right side of the page you will see a section labelled "Display post information on". In this section you should see a list of content types. Uncheck the boxes for the content types you wish to remove the date/author info from and save your changes. Now when you view these types of nodes the date and author info will be gone. Please note this just suppresses the display of the date and author data, it does not remove the information from your database.

Drupal Tip #4 - Make the Site Search tool visible to everyone

This is a very basic tip but some may find it helpful. The base drupal installation comes with a search module. You may notice that the search box appears when you are logged in as administrator but disappears if you are not.

You can easily enable search functionality for all your site visitors. First make sure you have the site module enabled. You can do this at http://yoursitename.com/admin/build/modules. Next visit the permissions tab at http://yoursitename.com/admin/user/permissions. Scroll down to the search module section and check the boxes next to search content. Now save your changes and everyone can access the search box.

Drupal Tip #3: Introducing SQL Cron

SQL Cron is a Drupal module that allows you to create and manage a set of SQL statements that are executed during cron runs. The last execution time and execution result is displayed for each SQL statement. Any SQL statement that is valid for the Drupal db_query function can be used. This includes INSERT, TRUNCATE, UPDATE, DELETE, etc. This module is similar to the DB Cron module which hasn't been maintained since Drupal 4.7.

This module can be useful for pruning and updating custom database tables used by your Drupal installation. For example, you may want to periodically update information about your top rated or most viewed content in a custom table. You may have a log of user visits or actions that you want to truncate or prune periodically. SQL Cron provides you an easy method for accomplishing this without having to set up separate cron jobs on your web server.

You can download this module here: SQL Cron for Drupal 6

Screenshot #1: The SQL Cron menu link on the main administration page.

Screenshot #2: The SQL Cron list menu. Notice some statements are purposely incorrect to test statement results.

Screenshot #3: The SQL Cron result menu.

Drupal Tip #2: Display Information About Your Visitors

Every now and then you may want to access some information about a website visitor. You might want to provide this information to them or perhaps you want to display different text, images, etc. depending on what host they are from. This article highlights using some simple php code to retrieve visitor info such as ip address, hostname, browser type, etc. The following $_SERVER variables give you access to this information -

IP address of visitor:
$_SERVER['REMOTE_ADDR'] = 38.107.179.207

Hostname of visitor:
gethostbyaddr($_SERVER['REMOTE_ADDR']) = 38.107.179.207

Port used by visitor:
$_SERVER['REMOTE_PORT'] = 60872

Software used by visitor:
$_SERVER['HTTP_USER_AGENT'] = CCBot/1.0 (+http://www.commoncrawl.org/bot.html)

Referring page:
$_SERVER['HTTP_REFERER'] =

Page Request Timestamp:
date(DATE_RFC822, $_SERVER['REQUEST_TIME']) = Sun, 20 May 12 09:17:21 -0500
Subscribe to michaelosmith.com RSS