Add custom fields under wp general settings

To add custom fields under wordpress general settings without hacking core code use this as example. Of course you can make it better and write some fancy class for doing this.

add_filter('admin_init', 'my_general_settings_register_fields');

function my_general_settings_register_fields()
	register_setting('general', 'my_field', 'esc_attr');
	add_settings_field('my_field', '<label for="my_field">'.__('My Field' , 'my_field' ).'</label>' , 'my_general_settings_fields_html', 'general');

function my_general_settings_fields_html()
	$value = get_option( 'my_field', '' );
	echo '<input type="text" id="my_field" name="my_field" value="' . $value . '" />';

Do something when user click somewhere outside of wrapper

To close or do something when users clicks outside of some element following few lines of code can be helpful.

$(document).click(function(event) {
	if ($($('#first_wrapper')) == -1 &&
		$($('.second_wrapper')) == -1) {
		// Run something

WordPress custom menu display

Just a small example of how to build custom menu output in WordPress.

$menu_args		= array('menu' => 'Main menu', 'echo' => 0) ;
$menu_string	= wp_nav_menu($menu_args);
$xml	= new SimpleXMLElement($menu_string);
$out	= false;

foreach ($xml->ul[0] as $li) 
	$classes = array('current-page-ancestor', 'current-menu-item', 'current-menu-parent');
	if (strstr($li['class'], 'current-page-ancestor') || 
		strstr($li['class'], 'current-menu-item') || 
		strstr($li['class'], 'current-menu-parent')) 
		$out = $li->ul;      
		$i = 0;
		if (isset($out->li) && !empty($out->li))
			$out['id']		= 'sub_menu';
			$out['class']	= 'clear';
			foreach ($out->li as $li_sub) 
				if (!strstr($li_sub['class'], 'current-page-ancestor') && 
					!strstr($li_sub['class'], 'current-menu-item') && 
					!strstr($li_sub['class'], 'current-menu-parent')) 
if (!empty($out)) {
	 echo $out->asXML(); 

Replace menu links in Drupal 7 using menu_link & menu_tree hooks

Want to add span elements to menu, change ul id or add classes? Need images in menu or some custom style? You can do that by using menu_link for menu items and menu_tree hook for menus itself. To replace all menus remove __MENUNAME.

// Customize menu li & links
function MYTHEME_menu_link__MENUNAME($variables) 
	$element	= $variables['element'];
	$sub_menu	= '';

	if ($element['#below']) {
		$sub_menu = drupal_render($element['#below']);

	// Enable this to use html in title, eg img, span or something else...
	$element['#localized_options']['html'] = true;

	$link = l('<span>' . $element['#title'] . '</span>', $element['#href'], $element['#localized_options']);

	return '<li' . drupal_attributes($element['#attributes']) . '>' . $link . $sub_menu . "</li>\n";

// Customize menu ul
function MYTHEME_menu_tree__MENUNAME($variables) 
	// Change id of menu ul
	return '<ul id="my-custom-menu-id">' . $variables['tree'] . '</ul>';

You can probably use preprocess_menu_link or preprocess_links as well, but this seems to be easiest way. Read more about it here.

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);


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' )

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

// 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

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');