Tag Archive for php

FirePHP example

// start ouput buffering
ob_start();

// be sure you have uploaded the FirePHP lib on your server
require_once('lib/FirePHPCore/FirePHP.class.php');

// instantiate firephp, false would have turned logging off
$firephp = FirePHP::getInstance(true);

// See some more examples at:
// <a href="http://www.firephp.org/HQ/Use.html" >http://www.firephp.org/HQ/Use.html</a>
$firephp->log('Message','Bob was here');

source

PHP getNumberSelect

// FUNCTION: getNumberSelect
//
// create a select box for selecting number from min - max.
//
// $name -> name of the form element, also the id for css selecting.
// $min -> the starting number
// $max -> the end number
// $default -> the default that you want to be selected when shown.
//
//  ex,   getNumberSelect( "birthdate_day" , 1 , 31 , 1 )
//
function getNumberSelect( $name , $min , $max , $default = null )
{
$temp = "";

// if it is not set from the form then show this one as selcted...
if( ! isset( $_POST[$name] ) )
{
$_POST[$name] = intval( $default );
}

// the id is set for styling feel free to mod
$temp .= "<select name="".$name."" id="".$name."" >
";

// we go from min to max, yes yes yes min should be < max
// what fool would call it switched?
while( $min <= $max )
{
$temp .= "<option value="".$min.""";

if( intval( $_POST[$name] ) == intval( $min ) )
{
$temp .= " SELECTED ";
}

// I always put the spaces there cause all the browsers do weird things
// with select boxes and this seemed to make things ok.
$temp .= ">".$min."&nbsp;&nbsp;</option>
";
$min++;
}
$temp .= "</select>
";
return $temp;
}

source

image watermark with php

<?php

$main_img 		= "image.jpg"; // main big photo / picture
$watermark_img	= "watermark.gif"; // use GIF or PNG, JPEG has no tranparency support
$padding 		= 3; // distance to border in pixels for watermark image
$opacity		= 100;	// image opacity for transparent watermark

$watermark 	= imagecreatefromgif($watermark_img); // create watermark
$image 		= imagecreatefromjpeg($main_img); // create main graphic

if(!$image || !$watermark) die("Error: main image or watermark could not be loaded!");

$watermark_size 	= getimagesize($watermark_img);
$watermark_width 	= $watermark_size[0];
$watermark_height 	= $watermark_size[1];

$image_size 	= getimagesize($main_img);
$dest_x 		= $image_size[0] - $watermark_width - $padding;
$dest_y 		= $image_size[1] - $watermark_height - $padding;

// copy watermark on main image
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);

// print image to screen
header("content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);

source

Manually load (Google’s) Latest jQuery in WordPress

<?php wp_deregister_script('jquery'); ?>
<?php wp_head(); ?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

source

WordPress Get Category Slug

if(is_category()) {
$cat = get_query_var('cat');
$yourcat = get_category($cat);
echo "the slug is" . $yourcat->slug;
}

source

SPL_autoload example script

<?php

set_include_path(realpath('/path/to/lib') . PATH_SEPARATOR . get_include_path());

function autoload($className)
{
require($className . '.php');
}

spl_autoload_register('autoload');

$Database = new Database();

?>

source

Date Based Category Archives for WordPress

function extend_date_archives_flush_rewrite_rules(){
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_action('init', 'extend_date_archives_flush_rewrite_rules');

function extend_date_archives_add_rewrite_rules($wp_rewrite){
$rules = array();
$structures = array(
$wp_rewrite->get_category_permastruct() . $wp_rewrite->get_date_permastruct(),
$wp_rewrite->get_category_permastruct() . $wp_rewrite->get_month_permastruct(),
$wp_rewrite->get_category_permastruct() . $wp_rewrite->get_year_permastruct(),
);
foreach( $structures as $s ){
$rules += $wp_rewrite->generate_rewrite_rules($s);
}
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'extend_date_archives_add_rewrite_rules');

source

List all images from Directory (and sub-directory)

<?php
//path to directory to scan. i have included a wildcard for a subdirectory
$directory = "images/*/";

//get all image files with a .jpg extension.
$images = glob("" . $directory . "*.jpg");

$imgs = '';
// create array
foreach($images as $image){ $imgs[] = "$image"; }

//shuffle array
shuffle($imgs);

//select first 20 images in randomized array
$imgs = array_slice($imgs, 0, 20);

//display images
foreach ($imgs as $img) {
echo "<img src='$img' /> ";
}
?>

source

Remove DOM Node from XML

function formatIFRAMECells1($oDom)
{
$oXpath = new DOMXpath($oDom);
$oElements = $oXpath->query("//td[@class='tditem']");

//Remove the TD element to the right of the logo cell. This xpath query finds the following TD element:
// <td bgcolor="#FFFFFF" height="2" class="tditem">
//
// Only remove it if it's also followed by:
//<div align="left"><img src="http://www.mobiles4everyone.com/images/horizontallineplain.jpg" width="100%" height="1"></div>

if (!is_null($oElements))
{
foreach ($oElements as $oElement)
{
if($oElement->hasChildNodes())
{
$oChildren = $oElement->childNodes;
for($i=0;$i<$oChildren->length;$i++)
{
$oChild = $oChildren->item($i);
if($oChild->nodeName == "div")
{

if($oChild->hasChildNodes())
{
$oDivChild = $oChild->firstChild;
if($oDivChild->nodeName == "img")
{
$oElement->parentNode->removeChild($oElement);
break;
}
}
}

}
}
}
}
return $oDom;
}

source

Call Custom Field Values (WordPress)

<?php $customField = get_post_custom_values("page_blurb");
if (isset($customField[0])) {
echo $customField[0];
}
?>

source