CodeGurus

Web development tips & tricks. Code samples and snippets to make you code better. Just IT!

  • Home
  • Cheat sheets
  • Inspiration
  • Tools
RSS

Add custom fields under wp general settings

Posted on January 8, 2012 by Alvar

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 . '" />';
}
Categories: Wordpress, Wordpress development | Tags: admin_init, custom fields, general settings, wordpress

Do something when user click somewhere outside of wrapper

Posted on January 8, 2012 by Alvar

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 ($(event.target).parents().index($('#first_wrapper')) == -1 &&
		$(event.target).parents().index($('.second_wrapper')) == -1) {
		// Run something
	}
});
Categories: HTML/javascript, jQuery | Tags: body, click, document, jquery, outside, tabs, wrapper

Toggl time-tracking

Posted on January 5, 2012 by Alvar

Simple time-tracking that just works. Timesheet killer with extra tools for desktop & mobile, plus they also have API for some custom magic.

They also have Free plan, up to 5 users with basic features.

Sign-up here

Categories: Tools | Tags: desktop, increase income, mobile, time-tracking, timesheet, toggl

WordPress “wp_nav_menu” separate submenu output

Posted on January 2, 2012 by tauno
/**
 * WP_nav_menu separate submenu output.
 *
 * Optional $args contents:
 *
 * string theme_location - The menu that is desired.  Accepts (matching in order) id, slug, name. Defaults to blank.
 * string xpath - Optional. xPath syntax.
 * string before - Optional. Text before the menu tree.
 * string after - Optional. Text after the menu tree.
 * bool echo - Optional, default is TRUE. Whether to echo the menu or return it.
 *
 * @author Tauno Hanni
 * @param array $args Arguments
 * @return String If $echo value is set to FALSE.
 */
function px_the_submenu( $args = array() )
{

	$defaults = array(
		'theme_location' => '',
		'xpath' => "./li[contains(@class,'current-menu-item') or contains(@class,'current-menu-ancestor')]/ul",
		'before' => '',
		'after' => '',
		'echo' => true
	);
	$args = wp_parse_args( $args, $defaults );
	$args = (object) $args;

	$output = array();

	$menu_tree = wp_nav_menu( array( 'theme_location' => $args->theme_location, 'container' => '', 'echo' => false ) );
	$menu_tree_XML = new SimpleXMLElement( $menu_tree );
	$path = $menu_tree_XML->xpath( $args->xpath );

	$output[] = $args->before;

	if( ! empty( $path ) )
	{
		$output[] = $path[0]->asXML();
	}

	$output[] = $args->after;

	if( $args->echo )
		echo implode('', $output );
	else
		return implode('', $output );

}
Categories: Wordpress, Wordpress development | Tags: custom, menu, theme, wordpress, xpath

WordPress custom menu display

Posted on December 7, 2011 by Alvar

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'))
				{
					unset($out->li[$i]->ul);
				}
				$i++;
			}
		}
		break;;
	}
}
if (!empty($out)) {
	 echo $out->asXML();
}
Categories: Wordpress, Wordpress development | Tags: custom menu, multi level menu, submenu, wordpress

Drupal 7 schema & datetime

Posted on November 18, 2011 by Alvar

When working on db schemas in Drupal it’s good to know that syntax for datetime is somewhat changed.

Before it (Drupal 6) it was:

'type' => 'datetime',

now in Drupal 7 it’s:

'mysql_type' => 'datetime',  // for mysql
Categories: Drupal, Hooks | Tags: database, datetime, drupal 7, hook_schema, type

Replace menu links in Drupal 7 using menu_link & menu_tree hooks

Posted on October 25, 2011 by Alvar

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.

Categories: Drupal, Hooks | Tags: customize, drupal 7, hook, menus, menu_link, menu_tree, replace

Requiring files in Drupal 6 & 7

Posted on October 11, 2011 by Alvar

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');
Categories: Drupal, Functions | Tags: drupal, drupal 7, include, load file, module, require

Logging in Drupal 7

Posted on October 11, 2011 by Alvar

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

 

Categories: Drupal, Functions | Tags: drupal 7, errors, log, logging, messages, watchdog

Change default Joomla 404 behaviour or style

Posted on October 6, 2011 by Alvar

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.

Categories: Joomla, Themes | Tags: 404, custom, error, joomla, template
Previous Entries
  • Recent Posts

    • Add custom fields under wp general settings
    • Do something when user click somewhere outside of wrapper
    • Toggl time-tracking
    • WordPress “wp_nav_menu” separate submenu output
    • WordPress custom menu display
  • Categories

    • Drupal
      • Functions
      • Hooks
    • HTML/javascript
      • jQuery
    • Joomla
    • Mobile
    • Themes
    • Tools
    • Wordpress
      • Plugins
      • Wordpress development
  • Tags

    404 add content autoupdate custom custom columns custom field custom post type drupal drupal 7 error errors files framework fs_method GeoMashup hierarchical html html5 if exists javascript joomla jquery logging MailChimp manage_posts_custom_column mobile newsletter newsletter form node node_save plugin programmatically scrollto template theme touch ui update watchdog without ftp wordpress wordpress core wordpress development wordpress header xhtml
  • Tweets

    Rabarberihooaeg on kätte jõudnud. Selle kevade esimene rabarberikook. Mmmmm... http://t.co/3b44dcvo
    7 hours ago
    Terviseks http://t.co/k3wcpH95
    2 days ago
    Back from one-day conference held in the heart of Helsinki. #webshaped was a great success! Hopefully... http://t.co/b8QVsSZ6
    3 days ago
    On my way to #webshaped, running a bit late tho http://t.co/6XGiZEou
    4 days ago
    http://t.co/nPAewefs
    6 days ago
  • Archives

  • Sign up for our mailing list.

  • Net Premium: web dev education from the professionals.

  • Learn Drupal with tons of focused Drupal tutorials at Build a Module.com

© CodeGurus. Proudly Powered by WordPress | Nest Theme by YChong