Drupal – Check if a User has a specific role

Here is a quick way to determine if a user has a specific role

<?php
  // Bring the user object into scope.
  global $user;

  // Check to see if $user has the administrator user role.
  if (in_array('administrator', array_values($user->roles))) {
    // Do something.
  }
?>

Drupal – Adding Javascript to your module

When creating your own Drupal module, you may need to add some styling or Javascript to improve the usability of your module. Here is how to do it.

drupal_add_js(drupal_get_path('module', 'MODULE_NAME') . '/common.js');

Similarly you can add CSS to your module as well

drupal_add_css(drupal_get_path('module', 'MODULE_NAME') . '/styles.css');

Drupal – Use hook_form_alter() to set redirect path on the form

One popular use of this hook is to change the destination of a form submission. Here is how it is accomplished:

<?php
function YOURMODULE_form_alter($form_id, &$form) {
  switch ($form['#id']) {
    case 'node-form':
       if ($form['type']['#value'] == 'story') {
         $form['#redirect'] = 'new/url';
       }
     break;
  }
}
?>

Excellent Analytics – Import Google Analytics into Excel

I ran into this nice Excel Plugin that lets you import web analytics data from Google Analytics into a spreadsheet. It’s an open source project and 100% free to download and use for individuals and businesses.

Wordpress – List Scheduled Posts

If you ever wanted to show you readers posts that are scheduled to be published, here’s how to do it.

<?php
$result = new WP_Query('post_status=future&order=DESC&showposts=5');
if ($result->have_posts()) {
    while ($result->have_posts()) : $result->the_post(); ?>
        <?php the_title(); ?>
    <?php endwhile;
}
?>