nooshu - Matt Hobbs' Web Development Blog

Kneeling on the shoulders of giants

WordPress: Are you sure you want to do this?

While writing a plug-in for WordPress recently I came across a very strange error message:

Are you sure you want to do this?

Now my initial reaction was “Well yes, I do want to do this!”. Unfortunately that wasn’t an option. It just told me to try again… same message… ad nauseam. What I was actually trying to do was pass some form information from the plug-in dashboard panel to the plug-in tables in the database.

After searching the web for a while and not having much luck I decided to ‘view source’ on the Quickpress widget which was doing a similar function. I noticed these two hidden inputs:

1
2
<input type="hidden" id="_wpnonce" name="_wpnonce" value="[random code here]" />
<input type="hidden" name="_wp_http_referer" value="/wp-admin/" />

After a brief search in google about ‘Cryptographic nonce‘ it occurred to me that’s what was missing. A vital security feature that WordPress uses to validate that the form information came from the current site rather than an external source. Very clever, but quite frustrating if you don’t know about it.

Adding the following to the form code fixed the issue.

1
2
3
4
5
$content = '<form name="formname" method="post" action="'.$url.'">';
if (function_exists('wp_nonce_field')){
    $content .= wp_nonce_field('hidden_input_name_here');
}
$content .= '...';

Simple when you know about it! The ‘wp_nonce_field‘ function is documented in the WordPress codex.

Update: Just to make it a bit clearer I added the code to the plug-in file that was generating my form. So for example:

1
2
3
4
5
6
7
8
<form name="our_form" method="post" action="http://oururl.com/action">
    <?php
        if (function_exists('wp_nonce_field')){
            $content .= wp_nonce_field('hidden_input_name_here');
        }
    ?>
    <!-- Other relevent code for the generated form -->
</form>

The hidden inputs are inserted into the form allowing WordPress to validate where the request came from.

IE6 and the Abbreviation tag

While testing a WordPress theme in various browsers (IE) I noticed a strange issue that was occurring in IE6, but fine in IE7+. After a little head scratching I realised IE6 doesn’t recognise the abbreviation(abbr) tag. I’d never noticed before as I very rarely use the tag.

As a quick fix you could edit your theme:

1
2
3
4
5
6
7
<!-- From -->
<abbr>Abbreviation here</abbr>
               
<!-- To -->
<abbr>
    <span>Abbreviation here</span>
</abbr>

Then apply any styles to the inner span rather than the abbr. Or if hard coding a span isn’t your thing you could always use a little bit of jQuery(1.2+) goodness to add the spans for you:

1
2
3
4
5
jQuery(function($){
    $("abbr").each(function(){
        $(this).wrapInner('<span></span>');
    });
});

The effect on page render time will be minimal since we are only grabbing a single tag with no complex selectors.

Processing & Aptana make a good couple

I’ve been learning Processing on and off for three or four months now since i started reading ‘Learning Processing‘ by Daniel Shiffman. A great book that makes learning easy and fun. One thing that frustrated me from the start was Processing’s own IDE (Integrated Development Environment).

Processing GUI on Windows Vista

The default Processing GUI (Windows)

Now there’s nothing wrong with PDE, it’s just quite limited. Having come from Aptana (and before that Eclipse) it’s missing quite a few features I’m used too. The main two being code complete and line numbers (Your current line number is displayed in the bottom left, but that’s it). Lucky it is possible to use either Aptana or Eclipse (since Aptana is a modivied version of Eclipse) to program Processing.

The Processing website has a great step by step tutorial on how to do this ‘Processing in Eclipse‘.

Using Aptana to program Processing

Aptana and Processing in harmony

Once complete you have all the features of the Eclipse IDE, including auto complete and line numbers (yay!). It will be a shame to not have all the code examples from PDE, but it’s always possible to copy and paste where needed.

Full Frontal 2009 in Brighton

6 speakers at presenting at full frontal 2009

Full Frontal 2009 - The line up

This post is slightly late as the conference was on the 20th November 2009. But better late than never i guess!

The day was very wet and rainy but that didn’t take anything away from a brilliant conference. Some great speakers there: Simon Willison, Christian Heilmann and Peter-Paul Koch(ppk) to name a few.

The two highlights for me were the very entertaining talk on ‘Optimising where it hurts’ by Jake Archibold and a great introduction to node.js by Simon Willison. It was quite a technical talk and after a long day of listening to various people present it did make my brain hurt a little. I’ve added node.js to the ‘TODO’ list of things to look at… eventually.

Hopefully there will be another Full Frontal in 2010, if there is I’ll be going again.

jQuery, WordPress and your functions.php

It’s always interesting running ySlow on a website you are working on, getting that ‘warm fuzzy feeling’ when you finally get that ‘A’. I noticed while using it on nooshu that jQuery was being included twice. Version 1.4 by me in the footer.php and version 1.3.2 being included by a plug-in (or WordPress itself). Not Good. The user gets an extra 40Kb download and an added HTTP request.

A quick way to fix this is to add this to your functions.php file (located in the theme directory). The admin section will still be able to use it’s own version of jQuery due to the if statement.

1
2
3
if(!is_admin()){
    wp_deregister_script('jquery');
}

If you don’t have a functions.php file you can create a new one and paste that code inside. While in there you may also want to include this:

1
remove_action('wp_head', 'wp_generator');

That will remove your current WordPress version from your ‘head’ tag in your templates. Not a big issue but could be useful information to a malicious individual.

For more information on both functions in the WordPress codex see remove_action and wp_deregister_script

Currently on page 13 of 14« First1011121314