WordPress – Redirecting to old domain


January 27, 2009

After migrating a WordPress site to a new domain (renaming the root folder) I found that my new site kept forwarding to the development domain. As I was unable get into the admin screen of WordPress to change the base URL of the site I had to alter it directly in the database.

The path to the base URL of the site is:

Server: yourServer.com > Database: Database_name > Table: wp_options

In the options table you’ll find a value named ‘URL’, change this to the new domain name and you should be good to go.

Tags: ,

Form breaks WordPress


January 26, 2009

WordPress Variable Clash

Just spent the last hour trying to figure out why a simple php form was breaking WordPress. The answer was a variable name clash. I used a variable called ‘name’ in my form which seems to be linked the WP tag tags (!).

To get things working I renamed ‘name’ to ‘signUpName’.

Whilst scrabbling around for answers I did come across a good article about a security vulnerability when using the PHP_Self command, it’s well worth a read before you build your next form.

Tags: , , ,

Postcode Widget – Part 3


August 25, 2008

You can see a prototype version of the widget by clicking here.

Template engine

I attempted this built as single piece of php but it quickly became obvious that this was not the right approach. Instead I adapted a simple template engine from my faithful O’Reilly book. This allowed me to separate the logic from the markup

function FillTemplate($inName, $inValues = array()){

	$theTemplateFile = $_SERVER['DOCUMENT_ROOT'].'/templates/'.$inName;
	if($theFile = fopen($theTemplateFile, 'r')){
		$theTemplate = fread($theFile, filesize($theTemplateFile));
		fclose($theFile);
	}
	$theKeys = array_keys($inValues);
	foreach($theKeys as $theKey){
		$theTemplate = str_replace("\{$theKey}",$inValues[$theKey],$theTemplate);
	}

	return $theTemplate;
}

Map Logic

Here’s the source for the Map Logic. As an attempt to make the script secure you can see I’ve used a UK Postcode Regex from Stuart Wade to validate the form input. And also mysql_real_escape_string() on any database calls.

Any security tips would be greatly appreciated

The Map Logic uses four separate templates to handle the interaction, and the error state.

<?php
//Pin counter
connectDB();
$result = mysql_query("SELECT * FROM mapPins");
$num_rows = mysql_num_rows($result);
$bindings['PINCOUNT'] = $num_rows;

//Place Pins
$pinQuery = "SELECT Latitude,Longitude FROM mapPins";
$pinArray = mysql_query($pinQuery)or die('Query failed: '.mysql_error());
$pinCode="";
while ($row = mysql_fetch_array($pinArray, MYSQL_NUM)) {;
	$i++;
	$pinCode .="var latlng$i = new GLatLng($row[0] ,$row[1] );";
	$pinCode .="map.addOverlay(new GMarker(latlng$i));";
}
$bindings['MAPPINS'] = $pinCode;

$bindings['DESTINATION'] = $PHP_SELF;

function centerFromDB($postcode){
	//Connect to the database
	connectDB();
	//Convert the Postcode to all uppercase
	$postcode = strtoupper($postcode);

	//Break the Postcode into array
	$postcode = explode(' ', $postcode);

	//Pull out the relevant fields from our database
	$query = "SELECT Latitude,Longitude FROM postcodes WHERE Pcode= '$postcode[0]'";
	$result = mysql_query($query) or die('Query failed: '.mysql_error());
	return mysql_fetch_array($result, MYSQL_ASSOC);

}

if (isset($_POST['submitPostCode']) & $_POST['code']!="") {
	if(eregi("^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$",mysql_real_escape_string($_POST['code']))){
		$line = centerFromDB(mysql_real_escape_string($_POST['code']));

		//Grab the Postcode from the form
		$postcode = mysql_real_escape_string($_POST['code']);

		//Uppercase
		$postcode = strtoupper($postcode);

		//Break the Postcode into array
		$postcode = explode(' ', $postcode);

		//Output Results
		$bindings['INITIALCENTER'] = "var center = new GLatLng(".$line['Latitude'].", ".$line['Longitude'].");";
		$bindings['OUTCODE'] = $postcode[0];
		$bindings['SECONDHALF'] = $postcode[1];

		$template = "map02.template";
	}else{

		$template = "mapError.template";
	}
}elseif(isset($_POST['plotPoints'])){

	$outCode = mysql_real_escape_string($_GET['outCode']);
	$secondHalf = mysql_real_escape_string($_GET['secondHalf']);
	$mapLat = mysql_real_escape_string($_GET['mapLat']);
	$mapLng = mysql_real_escape_string($_GET['mapLng']);
	$today = date("Y-m-d");

	$line = centerFromDB($outCode.' '.$secondHalf);
	//connect to db
	connectDB();

	//add point to db
	mysql_query("INSERT INTO mapPins (outCode, secondHalf, Latitude, Longitude, theDate) VALUES('$outCode', '$secondHalf', '$mapLat', '$mapLng', '$today') ") or die(mysql_error()); 

	$pinQuery = "SELECT Latitude,Longitude FROM mapPins WHERE outCode='$outCode' AND secondHalf='$secondHalf'";
	$pinArray = mysql_query($pinQuery)or die('Query failed: '.mysql_error());

	$pinCode="";
	while ($row = mysql_fetch_array($pinArray, MYSQL_NUM)) {;
		$i++;
		$pinCode .="var latlng$i = new GLatLng($row[0] ,$row[1] );";
		$pinCode .="map.addOverlay(new GMarker(latlng$i));";
	}
	//new
	$bindings['CURRENTPC'] = $outCode.' '.$secondHalf;
	$bindings['MAPPINS'] = $pinCode;
	$bindings['NEWCENTER'] = "var center = new GLatLng(".$line['Latitude'].", ".$line['Longitude'].");";

	$template = "map03.template";
}else{

	$template = "map01.template";

}

echo FillTemplate($template, $bindings);
?>

Integrating script with WordPress

After developing this script I discovered that WordPress can’t run PHP in the body of a post without a plugin. Strange. So I had to put the prototype on a loose page.

Take a look at the trick I used to capture the output of a WordPress tag so I could incorporate it into Map Logic, and then my templates. In this example I captured the footer code, then bound it to a template tag.

<?php
require('./wp-blog-header.php');
ob_start();
get_footer();
$footer = ob_get_contents();
ob_end_clean();
$bindings['FOOTER'] = $footer;
?>

The SEO experiment – Part 1 groundwork


August 10, 2008

I’ll admit it, I’m very drawn towards Search Engine Optimization (SEO). The idea that you can drastically improve the visibility of your site with a few code and content tweaks certainly does appeal to my lazy bone.

For my experiment this site is currently the perfect candidate; low traffic, and scoring exactly zero in Googles Page Ranking.

However, that’s all about to change.

There are a number of techniques involved in SEO which I’ll cover as I go along.

Okay, first things first; get a Google account. Done that already? Great!

Now what’s needed here is the Webmaster Tools and Analytics. These will show how Google sees this site, help uncover any errors, and measure progress.

Before going any further decide on the url, in my case either:
‘http://matthewbyrne.co.uk’
or
‘http://www.matthewbyrne.co.uk’ – my choice

Whatever you choose, stick with it.

Setting up Google Webmaster tools

  1. Enter the URL of your site
  2. Click ‘Verify your site’
  3. Select a verification method – Here I always choose to upload an html file
  4. Make a blank text file on your desktop, and rename it to your unique file name google5b27wb292f65b6.html
  5. Upload this to your sites root directory using your favourite ftp program – I use FileZilla
  6. Hit verify

Add a sitemap to Google Webmaster tools

If you’re site is built with WordPress, download and install the excellent ‘Google XML Sitemaps Generator for WordPress‘ plug-in. This automatically generates a fresh sitemap each time you add new content taking all the donkey work out of the job. Whilst you’re on their site why not donate a couple of dollars to encourage great software?

  1. For everybody else visit XML-Sitemaps.com to build a sitemap.
  2. Enter your URL
  3. Hit ‘Generate’
  4. Download the file to your desktop
  5. Upload it to your site root with your ftp client
  6. Back in the Webmaster tools hit Sitemap > ‘Add a sitemap’
  7. Fill in the sitemap name ‘sitemap.xml’
  8. Hit ‘Add general web sitemap’ and you’re done

Now I hop over to the Google account page and select Analytics.

Set up Google Analytics

  1. Hit ‘Add new website profile’
  2. Enter your URL
  3. Hit ‘Finish’
  4. Copy the chunk of code into all your pages before the closing body tag and you’re done

I’ll take a few hours/days for Google to process this information, and longer to give you anything meaningful, so don’t hold your breath. Next, determine the Google Page Rank.