Requiring files in Drupal 6 & 7

I have seen a lot of Drupal modules where requiring php files is done like this

$module = 'my_module';
require_once(DRUPAL_ROOT . '/' . drupal_get_path('module', $module) . '/test.php');

While this is totally right i still encourage you to use special function for this called “module_load_include“.

module_load_include('php', 'my_module', 'test');

Logging in Drupal 7

In case you need to log errors and other messages  in Drupal 7 use may use it’s watchdog function. Same thing will work for Drupal 6.

watchdog($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL)

Example with some data:

watchdog('Booking', 'Sucessfully booked for %persons', array('%persons' => 5), WATCHDOG_INFO);

Some possible severity levels are WATCHDOG_EMERGENCY, WATCHDOG_ALERT, WATCHDOG_CRITICAL, WATCHDOG_ERROR, WATCHDOG_WARNING, WATCHDOG_NOTICE, WATCHDOG_INFO, WATCHDOG_DEBUG.

Read more from Drupal 7 documentation

 

Change default Joomla 404 behaviour or style

To show custom 404 page or redirect user on some error to home page or somewhere else copy file error.php located under /templates/system/ into youre template folder eg /templates/mytemplate/error.php.

For example to redirect user on 404 add these lines right after defined( ‘_JEXEC’ ) or die( ‘Restricted access’ );. This works for Joomla 1.5.

if ( $this->error->code == '404' )
{
	header('http://www.google.com');
	exit;
}

For Joomla 1.6 and 1.7 use

$this->error->getCode() == 404

Of course you can just change style and html of the same file. This is just example for the case you want to redirect user to another page and dont want them too see 404 at all or to add some other custom logic.

Adding content programmatically in Drupal 7

Sometimes you really need to add content programmatically and here’s a little sample about the basics.

$node = new stdClass(); 

// Set content type
$node->type = 'article';

// Prepare defaults
node_object_prepare($node);

// Define language (currently language neutral)
$node->language = LANGUAGE_NONE;

// Basic content
$node->title = 'Test';
$node->body[$node->language][0]['value'] = 'Body text';

// Example if using custom fields
$node->field_CUSTOM_FIELD[$node->language][0]['value'] = 'Value';
// Example if using fields that are taxonomy type
$node->field_CUSTOM_FIELD_TAXONOMY[$node->language][0]['tid'] = 1;

// Save node
node_save($node);

For updating nodes load them first with:

// Node id you want to update
$nid = 1;
$node = node_load($nid);

And if you want to use revisions add the following:

$node->revision = 1;
$node->log = 'Node updated at ' . date('F j, Y, G:i');

Most promising mobile UI frameworks

Simple list of some well known mobile UI frameworks you may want to use on your next mobile web project.

jQuery Mobile
Touch-Optimized Web Framework for Smartphones & Tablets

jQTouch
A jQuery plugin for mobile web development on the iPhone, Android, iPod Touch, and other forward-thinking devices

Sencha Touch
The First HTML5 Mobile Web App Framework

Jo
The simple app framework for HTML5. For iOS, Android, webOS, BlackBerry, Chrome OS & anything else with HTML5

iUI
User Interface Framework for Mobile Web Devices

SproutCore
SproutCore is an open-source framework for building blazingly fast, innovative user experiences on the web

Custom columns for custom post type

When using hook “manage_MY_TYPE_NAME_posts_columns” or “manage_MY_TYPE_NAME_posts_custom_column” on WP version >= 3.1 the syntax is little bit changed.

Here’s the example:

add_filter('manage_MY_TYPE_NAME_posts_custom_column', 'my_column_values');
add_filter('manage_MY_TYPE_NAME_posts_columns', 'my_columns');

function my_columns($columns)
{
	$columns['custom_col'] = __('Field name text');
	return $columns;
}

function my_column_values($name, $post_id)
{
	global $post, $typenow;

	// Use $typenow to check custom post type

	switch ($name)
	{
		case 'custom_col':
			echo 'value';
		break;
	}
}

Drupal template suggestions

To find out possibilities for naming template files add following line to related template hook in your template.php eg THEME_preprocess_HOOK():

print_r($variables['theme_hook_suggestions']);

Some hooks to look at _html, _page, _node, _block, _pane, _views_view_field, _comment, _search_result, _search_results, _user_profile.

Scroll to an anchor

Smooth javascript scrolling.


<script type="text/javascript">// <![CDATA[
		function goToByScroll(id){
     			$('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
		}

// ]]></script>

<a onclick=”goToByScroll(‘1’)” href=”javascript:void(0)”>Go to anchor 1</a>

<div id=”1″>content</div>

Disable wordpress core updates display

Ever had a situation where you have given admin right to the client?  What usally happens is that client will update the wordpress core. If we are talking about custom solutions, its most definitely not safe to update the core. Here’s a solution for disabling  the opportunity to update WordPress core.

add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );
add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );

# 2.8 to 3.0:
remove_action( 'wp_version_check', 'wp_version_check' );
remove_action( 'admin_init', '_maybe_update_core' );
add_filter( 'pre_transient_update_core', create_function( '$a', "return null;" ) );

# 3.0:
add_filter( 'pre_site_transient_update_core', create_function( '$a', "return null;" ) );