PHP forward magic methods

function __call($name, $arguments) {
	if(method_exists($this->Email, $name)) {
		call_user_func_array(array($this->Email,$name),$arguments);
	} else {
		trigger_error('Method '.$name.' does not exist', E_USER_ERROR);
	}
}
function __get($name) {
	if(property_exists($this->Email, $name)) {
		return $this->Email->$name;
	} else {
		trigger_error('Property '.$name.' does not exist', E_USER_ERROR);
	}
}
function __set($name, $value) {
	if(property_exists($this->Email, $name)) {
		$this->Email->$name = $value;
	} else {
		trigger_error('Property '.$name.' does not exist', E_USER_ERROR);
	}
}

source

Multiple Widget Areas

if (function_exists('register_sidebar')) {

	register_sidebar(array(
		'name' => 'Header',
		'id'   => 'header',
		'description'   => 'This is the widgetized header.',
		'before_widget' => '<div id="%1$s" class="widg %2$s">',
		'after_widget'  => '</div>',
		'before_title'  => '<h3>',
		'after_title'   => '</h3>'
	));
	register_sidebar(array(
		'name' => 'Sidebar',
		'id'   => 'sidebar',
		'description'   => 'This is the widgetized sidebar.',
		'before_widget' => '<div id="%1$s" class="widg %2$s">',
		'after_widget'  => '</div>',
		'before_title'  => '<h3>',
		'after_title'   => '</h3>'
	));
	register_sidebar(array(
		'name' => 'Footer',
		'id'   => 'footer',
		'description'   => 'This is the widgetized footer.',
		'before_widget' => '<div id="%1$s" class="widg %2$s">',
		'after_widget'  => '</div>',
		'before_title'  => '<h3>',
		'after_title'   => '</h3>'
	));

}

source

custom wpquery begin

<?php $my_query = new WP_Query('page_id=19&showposts=1');
while ($my_query->have_posts()) : $my_query->the_post();?>

source

Case insensitive jQuery :contains selector

// For jQuery 1.2

jQuery.extend(
    jQuery.expr[':'], { 
        Contains : "jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0" 
});


// For jQuery 1.3 (thanks @user95227) and later you need

jQuery.expr[':'].Contains = function(a,i,m){
     return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;
};

source

List upcoming Events in Sidebar via PHP Code Widget

<?php $today = date ('Y-m-d'); ?>
<?php query_posts('cat=6&showposts=5&meta_key=event_date&meta_compare=>=&meta_value='.$today.'&orderby=meta_value&order=ASC'); ?>
<?php if ( have_posts() ) : ?>
  <ul>
  <?php while ( have_posts() ) : the_post(); ?>
   	<li class="cat-post-item"><a class="post-title" title="Permalink to <?php the_title() ?>" rel="bookmark" href="<?php the_permalink() ?>"><?php the_title() ?></a></li>   
  <?php endwhile; ?>
  </ul>
<?php endif; wp_reset_query(); ?>

source

Only allow AJAX for a HTTP Request

// If the request did not come from AJAX, exit:
if($_SERVER['HTTP_X_REQUESTED_WITH'] !='XMLHttpRequest'){
	exit;
}

source

WordPress – show top 10 tags by usage

<?php
function top_tags() {
	$tags = get_tags();

	if (empty($tags))
		return;

	$counts = $tag_links = array();
	foreach ( (array) $tags as $tag ) {
		$counts[$tag->name] = $tag->count;
		$tag_links[$tag->name] = get_tag_link( $tag->term_id );
	}

	asort($counts);
	$counts = array_reverse( $counts, true );

	$i = 0;
	foreach ( $counts as $tag => $count ) {
		$i++;
		$tag_link = clean_url($tag_links[$tag]);
		$tag = str_replace(' ', '&nbsp;', wp_specialchars( $tag ));
		if($i < 11){
			print "<li><a href="$tag_link">$tag ($count)</a></li>";
		}
	}
}
?>
<h4>Top tags</h4>
<ul>
<?php
top_tags();
?>
</ul>

source

Chat system example

<?php
/**
 * Add a message to the chat
 * @param Redis $redis Redis connection handler
 * @param String $message message to add
 * @return void
 */
function addMessage($redis, $message) {
	$redis->lPush("chat", $message);
	if($redis->lLen("chat") > 100) {
		$redis->rPop("chat");
	}
}

source

Draw with NSBezierPath

- (void) drawArrow
{
	NSLog( @"state = %i", state );

	// background
	NSRect bounds = [self bounds];
	[[NSColor orangeColor] set];
	NSRectFill( bounds );
	
	// arrow
	CGFloat width = bounds.size.width;
	CGFloat height = bounds.size.height;
	
	// no alloc and so no release in the end.
	NSBezierPath* path = [NSBezierPath bezierPath];
	
	[path moveToPoint: NSMakePoint( width * 0.35, height * 0.1 )];
	
	[path lineToPoint: NSMakePoint( width * 0.65, height * 0.1 )];
	[path lineToPoint: NSMakePoint( width * 0.65, height * 0.6 )];
	[path lineToPoint: NSMakePoint( width * 0.9, height * 0.6 )];
	[path lineToPoint: NSMakePoint( width * 0.5, height * 0.9 )];
	[path lineToPoint: NSMakePoint( width * 0.1, height * 0.6 )];
	[path lineToPoint: NSMakePoint( width * 0.35, height * 0.6 )];
	
	[path closePath];
	
	[[NSColor blueColor] set];
	[path fill];
	
	[[NSColor whiteColor] set];
	[path stroke];
}

- (void)drawHouse
{
	NSLog( @"state = %i", state );
	NSRect bounds = [self bounds];
	[[NSColor yellowColor] set];
	NSRectFill( bounds );	
	
	CGFloat width = bounds.size.width;
	CGFloat height = bounds.size.height;
	
	NSBezierPath *path = [NSBezierPath bezierPath];
	
	[path moveToPoint: NSMakePoint( width * 0.1, height * 0.6 )];
	
	[path lineToPoint: NSMakePoint( width * 0.1, height * 0.1 )];
	[path lineToPoint: NSMakePoint( width * 0.9, height * 0.1 )];
	[path lineToPoint: NSMakePoint( width * 0.9, height * 0.6 )];
	[path lineToPoint: NSMakePoint( width * 0.1, height * 0.6 )];
	[path lineToPoint: NSMakePoint( width * 0.5, height * 0.9 )];
	[path lineToPoint: NSMakePoint( width * 0.9, height * 0.6 )];
	 
	[path closePath];
	
	[[NSColor whiteColor] set];
	[path fill];
	 
	[[NSColor blueColor] set];
	[path stroke];
}


- (void)drawRect:(NSRect)dirtyRect 
{
	[NSGraphicsContext saveGraphicsState];
	
	//[self drawHouse]; 
	[self drawArrow];
	
	[NSGraphicsContext restoreGraphicsState];
}

source

Nested for loop

void setup() {
  size(400, 400);
  background(0);
  smooth();
  stroke(255, 50);
  //noStroke();
}

void draw() {
  
  for(int i = 0; i < width; i+=50) {
    
    for(int j = 0; j < height; j+=50) {
      fill(100, 0, 150, 100);
      ellipse(25 + i, 25 + j, 50, 50);
      fill(20, 100, 0, 50);
      rect(i, j, 50, 50);
      println("i = " + i + " j = " + j);
    }
    
  }
  
}

source