Blog

Tag Archives: PHP

Smart 404 for WordPress

Save visitors to your WordPress site from unhelpful 404 errors!

When a page cannot be found, Smart 404 will use the current URL to attempt to find a matching page, and redirect to it automatically. Smart 404 also supplies template tags which provide a list of suggestions, for use on a 404.php template page if a matching post can’t be immediately discovered.

Instead of quickly giving up when a visitor reaches a page that doesn’t exist, make an effort to guess what they were after in the first place. This plugin will perform a search of your posts, tags and categories, using keywords from the requested URL. If there’s a match, redirect to that page instead of showing the error. If there’s more than one match, the 404 template can use some template tags to provide a list of suggestions to the visitor.

This plugin is also useful if you have recently changed your permalink structure: With minimal or no adjustment, old permalinks will still work. Read More »

Also tagged , , | 214 Comments

Taking the 404 further

Update: I have now written a WordPress plugin that does all of this for you. Please use this plugin instead.

I’ve just changed my permalink structure for my blog to something a bit prettier. In the process, I realised that some previously-working permalinks weren’t operating any more, despite having a plugin set up to maintain old permalinks.

WordPress is fairly good at figuring out what viewers are requesting when a post can’t be found immediately – for example, if you’re using a permalink structure with an ID number in it, and the requested ID is incorrect, WordPress seems to be able to redirect to the correct address.

However, it’s not 100%, as I recently realised.

Consequently, a few pages were heading to the 404 page, which isn’t ideal. I changed my template’s 404 page to do a search for what the viewer was really after, and redirect them there. If it can’t find an exact match, it’ll perform a search with keywords extracted from the URL. If it finds a single result, it’ll redirect, otherwise it’ll put up a few results as suggestions on the 404 page.

It also works as a nice search shortcut. Try it: http://atastypixel.com/blog/wordpress 404 redirect

Read More »

Also tagged , | Leave a comment

Adding Twinkle to Twitter posting lists

Twinkle from Tapulous is an iPhone Twitter application with a twist – it uses location information, to introduce local social networking, and supports photos, a great extension to the standard Twitter recipe.

Twinkle handles photographs by uploading them to Tapulous’ server, then appending a ‘snipurl’ to the Twitter message which points to a page displaying the image. That works fairly well, but wouldn’t it be nice to actually see images inline, when viewing Twitter posts?

Well, now you can, if you have a self-hosted Twitter post list. See my Twitter feed on the right for a demo (although there may not be a post there with an image).

Read More »

Also tagged , , | 1 Comment

Smart redirects

After migrating my blog over to WordPress, files have moved around a bit. Sites that linked to files on my site (including Google Images, as a prominent example) now link into the void, which is a bit of a pain. As a partial solution, I’ve added some .htaccess rules to help route visitors to the right place:

Redirect 301 /feeds/index.rss2 http://atastypixel.com/blog/feed
Redirect 301 /feeds/atom.xml http://atastypixel.com/blog/feed

However, that doesn’t solve all problems – after I reorganised my uploads, files are located at a very different path to their original home. So, I added a short script which does a search for requested files if they can’t be found, then redirects visitors to the right file.

Here is the script, and an accompanying .htaccess file needed to hook into it:

redirection.zip

To use it, put the script and the htaccess file into a folder on your site where you want it to take effect (for me, it went into ‘wp-content/uploads’). Then, modify the htaccess file – just set the RewriteBase field to the directory you’re in – and rename it to ‘.htaccess’.

Also tagged , | 1 Comment

Re-organising/assembling uploads for WordPress

Migrating from Serendipity, I’ve had a few hiccups in data migration. It doesn’t help that I’m extremely OCD when it comes to data, so everything’s gotta be perfect.

I had a problem with the way my uploads (the resources linked into the blog) were organised from the old Serendipity days – all in one big ol’ folder – and wanted to re-arrange them the way WordPress does it, which is to arrange them into folders by year and month.

This is also useful if you’re changing your domain, or splitting up one blog into two. The entries link to the old domain/blog, and thus the alternative is to do keep the old site around or muck around with clever redirects.

My way’s neater.

So, I put together a script which goes through all posts, pulls out links to uploaded files, and moves/copies them into a better arrangement, fixing the link in the process. If it can’t find the files, it’ll use a ‘find’ command to attempt to locate them.

In case it’s useful to others, the script is here: reorganise-wp-media.php.zip

It goes in the WordPress webroot; put it there, then edit it to apply your settings. Give it a ‘dry run’ test, make sure all is well, then go for it. Beware, although I used it and it worked okay, I offer no guarantee that it won’t mangle your data, delete random files or eat your dog.

Also tagged , , , | Leave a comment

Serendipity (S9Y) importer for WordPress

Update 2: Jon reports that he’s improved on Carsten’s 1.3 version, and has released Version 1.4!

Update: Carsten Dobschat has continued the fine tradition and improved the importer further, adding support for extended posts, and tags.  Grab S9Y importer version 1.3 over at his site.

In the process of merging this site from Serendipity over to WordPress, I came across an importer which lets me migrate the data across. Unfortunately it was a bit buggy, not properly assigning categories and timing out when processing the post data.

I made some improvements, and here’s the new version, if anyone else finds it useful:

Version 1.2 of Serendity importer for WordPress (patch)

Also tagged , , | 11 Comments

Replacing the existing Leopard PHP installation with a more complete version

The good folks at entropy.ch have packaged up a pre-compiled version of PHP to replace OS X 10.5 Leopard’s barebones installation. The entropy.ch build contains most of the good stuff that the Apple folks in their infinite wisdom didn’t see fit to include – entropy-phpinfo.pdf

See the instructions at Entropy’s forum, or read below.

sudo mv /usr/local/php5 ~/Desktop/php5.old curl -O http://www2.entropy.ch/download/php5-5.2.5-6-beta.tar.gz tar -xzf php5-*-beta.tar.gz sudo mv php5 /usr/local/ sudo ln -sf /usr/local/php5/entropy-php.conf /etc/apache2/other/+entropy-php.conf sudo apachectl restart

Also tagged | 2 Comments

PHP debugging

php-debug.jpgManaging PHP debugging can be tricky as, by default, errors and test ‘echo’ statements can be lost along with all the other output. This problem is compounded when using AJAX, and errors can turn up in XML streams, causing all sorts of problems.

A better solution is to redirect all of that output into an external file, and use a tool like tail to monitor the log.

For printing debug statements, a debug function that shows where each statement came from (file and line number), and is capable of printing complex objects is useful too.

The following is the debugging code that’s included in SiteComponents.

if ( defined("DEBUG_ENABLED") )
{
   ini_set("error_log", $_SERVER["DOCUMENT_ROOT"]."/debug.log");
   ini_set("error_reporting", E_ALL);
   ini_set("display_errors", false);
   ini_set("log_errors", true);
}

/** * Define debugging routine */ function SCDEBUG($content="") { if ( !defined("DEBUG_ENABLED") ) return;

if ( !array_key_exists("__SCDEBUG_fd", $GLOBALS) )
{
   $GLOBALS["__SCDEBUG_fd"] = fopen($_SERVER["DOCUMENT_ROOT"]."/debug.log", "a");
    if ( !$GLOBALS["__SCDEBUG_fd"] ) return;
}

$bt = debug_backtrace();

if ( is_array($content) || is_object($content) )
{
   $content = print_r($content, true);
}

fwrite($GLOBALS["__SCDEBUG_fd"], 
      ($bt[0]["file"] ? (substr($bt[0]["file"], 
            strlen($_SERVER["DOCUMENT_ROOT"])+1).":".$bt[0]["line"]." (") : "").
        $bt[1]["function"]."()".
      ($bt[0]["file"]?")":"").
        ": ".$content."\n");

}

Also tagged | Leave a comment