PHP 101. Basic stuff: get value from URL querystring with PHP

As I’m still an idiot when it comes to media queries and mobile devices and can pass this stuff on to other folks – but needed to give myself and the interested parties a visual representation of how different sized graphics will be delivered to different mobile devices… I was after a simple ‘switch’ that would allow a single wedge of HTML to render according to a variable.

The example here goes along the lines of where the width has a few values it accepts or will default to a preset size…

This variable can then get passed around in the PHP file to set image dimaensions CSS etc…

function getUrlStringValue($urlStringName, $returnIfNotSet) {    if(isset($_GET[$urlStringName]) && $_GET[$urlStringName] != "")      return $_GET[$urlStringName];    else      return $returnIfNotSet;  }
$width = getUrlStringValue("width", "480");

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head><title></title><meta name="viewport" content="width=<?php echo $width ;?>" />

No Comments

JavaScript redirect based on referrer

Basic stuff (as often the case not immediately findable). You want a simple script to redirect to a different page if te referrer is such and such. In the following example the two referral page options ( foo.html and bar.html ) are separated with the double pipe (the ‘or’ bit)…

<script type="text/javascript">
if (document.referrer == ("" || "")) 
      location.href = "foobar.html"; 


No Comments

CSS 3 pseudo-classes and attribute selectors in Internet Explorer

Thanks to my mate Tony I’ve been introduced to this rather delicious (and very small) jQuery plugin that adds support for, among other things the :nth-child pseudo class. If this had been around a year ago ‘d have avoided having to craft this php counting system for a custom WordPress loop.

What’s the link already?
Here you go Jack: Selctivizr

Shout out to our kid Tony and good luck to him and his latest baby – an online platform for Illustrators: the artfuls


No Comments

Getting a page ID in WordPress

Q. Want to get something in your theme to work with data from a particular page or post – but don’t know for sure what that post’s ID is?

A. Try this in your functions.php file:

$my_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = 'myPost'");

…then you can then pass a variable about in your theme’s templates.


No Comments

Passing a variable about in a WordPress theme

I’m used to using php variables set in included  file being used in another included file if all are included in the same document. This doesn’t work in WordPress theme parts. It’s easy to overcome though… you just need to recall the variable first…

somewhere in functions.php

 $my_var = "xyz" 

Now i can do this somewhere in header.php

<?php global $myVar ;  ?>
<?php echo $myVar; ?>


No Comments

Multiple WordPress excerpt lengths

Q. Got a theme that needs two different excerpt lengths?

A. Found here: set the standard, longer excerpt as normal in the functions.php file and the override locally using this code:

<?php echo substr( get_the_excerpt(), 0, strrpos( substr( get_the_excerpt(), 0, 75), ' ' ) ); ?>



No Comments

Rollback a GIT commit

GIT is great, Git is good.

I mostly use git to track changes (very useful to check on what I’ve  been up to when it’s late in the night!) but occasionally I need to use it to roll back to a previous commit. This is the command I always forget, and today it took me more than one minute to find the answer (or formulate the right question) on google.

The answer, unsurprisingly on stackoverflow: in your [git tracked] working directory enter this terminal command:

git reset --hard <tag/branch/commit id>

Where  you replace the gubbins inside the angle brackets with the SHA ID of your commit. Find this easily using GitX or my potential new favourite GIT client on the Mac: Gitti.

No Comments