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.191.83

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

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

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']) = Tue, 09 Mar 10 18:00:03 -0800

Drupal Tip #1 - Embedding A Database Query Into Page Or Story Content

I have been busy setting up a few websites based on the Drupal CMS. I thought it might be a good idea to post a few tips on things I have figured out along the way.

One of the most helpful things I have figured out is how to include output from SQL queries into page content. This is very powerful as you can add almost any dynamic content you can think of. For instance, lets say you wanted to display the number of stories you have added to your site. You want to display this number on your front page. You can accomplish this by using embedded php to query your database. When you are editing the node that you will be adding the query to, make sure you select the "PHP Code" input format. This keeps Drupal from filtering out your PHP code.

*** Just a quick warning - you DO NOT want to allow just anyone the ability to use php scripts on your pages. By default only the "administrator" user has the ability to select the "PHP Code" input format. It is possible for malicious users to do some really nasty things to your site if you allow just anyone PHP access.

Here is the snippet that does the work:

<?php
    $story_query = db_query(db_rewrite_sql("SELECT count(node.nid) AS count FROM node WHERE (node.type = 'story' OR node.type = 'page') AND node.status = 1"));
    $story_count = db_fetch_object($story_query);
    print "<div align=center><b>" . $story_count->count . "</b> articles and counting . . .</div>";
?>


and here are the results:

7 articles and counting . . .

The query above is a very simple example. It just gives you a count of all nodes of type 'page' or 'story' that are published. Using this method you can extract any information you like and display it inside your nodes.

Welcome

Welcome to michaelosmith.com

Just starting to get the site up and running. I am quite busy at work so don't expect much any time soon -

Motifaketional.com - fake motivational posters
MarylandFriedChicken.com - Maryland Fried Chicken Restaurants
Canineplanet.net - Dog Breeder Listings
My del.icio.us Page

Syndicate content