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;
  }
}
?>

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;
}
?>

WordPress – Disable Auto Save

WordPress’s Auto-Save feature is a really nice but there are some drawbacks… this feature increases your database usage. So for those of you that are on really bad shared hosting accounts or just want to turn it off, here’s a quick way of doing it.

Open and insert the following line in your wp-config.php file.

define('WP_POST_REVISIONS', false);

Another method is to remove all of the entries from the database from time to time. You can do tun this query to do it::

DELETE FROM wp_posts WHERE post_type = "revision";