Category Archives: PHP

Breadcrumbs for Taxonomy for a Custom Post type

This is what I came up with to display the category, or rather a custom taxonomy, of a single posttype page of use in a breadcrumb. The taxonomy I used is ‘smg-logo-brand’ and the terms slug where ‘kcrg’, ‘the-gazette’ and ‘sourcemedia.’ I compared the terms associated with the post to that in the URL to determine which term belonged in the breadcrumb.

if (!is_tax() && get_post_type() == 'smg-logo' && has_term( array( 'kcrg', 'the-gazette', 'sourcemedia', 'other' ), 'smg-logo-brand' ) ) {
			$terms = get_the_terms( get_the_ID(), 'smg-logo-brand' );
			foreach ( $terms as $term ) {
				if ( strpos( $_SERVER["REQUEST_URI"], $term->slug ) !== false) {
					$breads['terms'] = '<a href="/logos/'. $term->slug . '/">' . $term->name . '</a>';
				}
			}
		}

WP-o-Matic breaks WordPress Related Links

function adminInit() {
		auth_redirect();

		// force display of a certain section
		$this->section = ($this->setup) ? ((isset($_REQUEST['s']) && $_REQUEST['s']) ? $_REQUEST['s'] : $this->sections[0]) : 'setup';
		
//    if (isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false))
//      die('Please switch to Firefox / Safari');

		wp_enqueue_script('prototype');
		wp_enqueue_script('wpoadmin', $this->tplpath . '/admin.js', array('prototype'), $this->version);

		if ($this->section == 'list')
			wp_enqueue_script('listman');

		//if ( WPOTools::isAjax() ) {
		if ( WPOTools::isAjax() &&  $this->section != 'home' ) { // Matt Thiessen 11/8/11
			$this->admin();
			exit;
		}
	}

SlidePress Plugin Redirect Bug

When clicking on settings links for the SlidePress plugin in a multi-user WordPress site the plugin would redirect to /wp-admin/?c=1 for each domain except the parent site.

[code lang=”php”]function get_current_user_role() {
global $current_user, $wp_roles;
$role_names = $wp_roles -> get_names();
/* UPDATED 11/2/2010 9:21 AM
* by matt.thiessen@sourcemedia.net
*/
// modified [start]
reset($current_user->roles);
$first_key = key($current_user->roles);
return preg_replace( “/\|.*/”, ”, $role_names[$current_user->roles[$first_key]]);
/*old*///return preg_replace( “/\|.*/”, ”, $role_names[$current_user->roles[0]]);
// modified [end]
}[\code]

Also, make sure if you are an admin user with super user privileges you are added at a admin user to the site you are using the SlidePress plugin.

This will add proper capabilities to the database in the wp_usermeta table.
meta_key: wp_25_capabilities
meta_value: a:1:{s:13:”administrator”;s:1:”1″;}

USPS Web Tools

I was trying to add the USPS API into osCommerce. I received the following error when adding this shipping module:

RateV3 is not a valid API name for this protocol

This is because when you create an account for the Web Tools at USPS you only have access to their test server. The USPS test server can only use RateV2, so test using this code below, then request to be switched to use the production server. Then you can use RateV3 on the production server and the USPS module for osCommerce will work.

<?php

$request1 = <<< XMLREQUEST
<RateV2Request USERID="xxxxxxxxxxxxx">
<Package ID="0">
<Service>PRIORITY</Service>
<ZipOrigination>10022</ZipOrigination>
<ZipDestination>20008</ZipDestination>
<Pounds>10</Pounds>
<Ounces>5</Ounces>
<Container>Flat Rate Box</Container>
<Size>REGULAR</Size>
</Package>
</RateV2Request>
XMLREQUEST;


$request = "http://testing.shippingapis.com/ShippingAPITest.dll?API=RateV2&XML=" . rawurlencode($request1);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SLL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);


$response = curl_exec($ch);
curl_close($ch);

echo($response);

?>

php turns SQL to Excel

Thanks StarGeek for this code.

this php script will write data from php using text from an SQL table (MySQL, supply your own SQL query) to a microsoft excel (.xls) file. The Excel file is specified as such by a set of headers, defining content, using php’s header() function.

<?php
//Written by Dan Zarrella. Some additional tweaks provided by JP Honeywell
//pear excel package has support for fonts and formulas etc.. more complicated
//this is good for quick table dumps (deliverables)

include(‘DB_connection.php’);
$result = mysql_query(‘select * from excel_test’, $linkID);
$count = mysql_num_fields($result);

for ($i = 0; $i < $count; $i++){
$header .= mysql_field_name($result, $i).“t”;
}

while($row = mysql_fetch_row($result)){
$line = ;
foreach(
$row as $value){
if(!isset(
$value) || $value == “”){
$value = “t”;
}else{
# important to escape any quotes to preserve them in the data.
$value = str_replace(‘”‘, ‘””‘, $value);
# needed to encapsulate data in quotes because some data might be multi line.
# the good news is that numbers remain numbers in Excel even though quoted.
$value = ‘”‘ . $value . ‘”‘ . “t”;
}
$line .= $value;
}
$data .= trim($line).“n”;
}
# this line is needed because returns embedded in the data have “r”
# and this looks like a “box character” in Excel
$data = str_replace(“r”, “”, $data);

# Nice to let someone know that the search came up empty.
# Otherwise only the column name headers will be output to Excel.
if ($data == “”) {
$data = “nno matching records foundn”;
}

# This line will stream the file to the user rather than spray it across the screen
header(“Content-type: application/octet-stream”);

# replace excelfile.xls with whatever you want the filename to default to
header(“Content-Disposition: attachment; filename=excelfile.xls”);
header(“Pragma: no-cache”);
header(“Expires: 0”);

echo $header.“n”.$data;
?>