Categories

WordPress custom menu

Add to functions.php:

add_action('init', 'my_custom_menu');
 
function my_custom_menu() {
register_nav_menu('my_menu', ('My Menu'));
}

Add to your template:

<?php wp_nav_menu(array('menu' => 'my_menu')); ?>

Add Buttons to WordPress wysiwyg editor

function enable_more_buttons($buttons) {
 $buttons[] = 'hr';
 $buttons[] = 'sub';
 $buttons[] = 'sup';
 $buttons[] = 'fontselect';
 $buttons[] = 'fontsizeselect';
 $buttons[] = 'cleanup';
 $buttons[] = 'styleselect';
 return $buttons;
}
add_filter("mce_buttons_3", "enable_more_buttons"

delete files in a directory that are X days old

<?php
 
$dir = '/path/to/dir';
if ($handle = opendir($dir)) 
{
  /* This is the correct way to loop over the directory. */
  while (false !== ($file = readdir($handle))) 
 {
    if ($file[0] == '.' || is_dir("$dir/$file")) 
    {
       // ignore hidden files and directories
       continue;
    }
    if ((time() - filemtime($file)) > ($days *86400)) 
    { //7 days
      unlink("$dir/$file");
    }
  }
  closedir($handle);
}
?>

Extract URLs From HTML

function extract_urls($data)
{
  $links = array();
  $pos = (int) 0;
  $i   = (int) 0;

  while (!(($pos = strpos($data,"<a",$pos)) === false)) {
    $startpos = strpos($data,"\"", $pos);
    $endpos = strpos($data,"\">",$pos);
    $tag = trim(substr($data,$startpos+1,($endpos-$startpos-1)));
    $tag = substr($tag, 0, strlen($tag));
    $end_tag_pos = strpos($data,'</a>',$endpos);
    $linked_text = strip_tags(substr($data, $endpos+2, ($end_tag_pos-$endpos-2)));
    $links[$i][0] = stripslashes($linked_text);
    $links[$i][1] = stripslashes($tag);
    $pos++;
    $i++;
  }
  return $links;
}

Get MySQL stats

<?php
$db_host = "";
$db_user = "";
$db_pass = "";
$db_name = "";

##
##	sqlstats()
##	-> Get mysql stats and display them
##

function sqlstats() {		
	## Connect to mysql and gather data
	$link = mysql_connect($db_host,$db_user,$db_pass) or die("Unable to connect to MySQL");
	
	$stats = explode('  ', mysql_stat($link));
	
	foreach($stats as $key => $value) {
		echo $value."<br/>";
	}
	
	## Data gather complete, close MySQL connection
	mysql_close();
}
?>