WordPress: Upload code hack

Something that annoys me a little about WordPress — that has changed between v.1.2 and the latest v.1.5 — is the code it automatically generates for uploaded image files.

When you Upload a file WordPress automatically generates the XHTML code for you, so that you can simply copy and paste it into a post or page, for example:

<img src='https://www.garethjmsaunders.co.uk/wp-content/image.jpg' alt='Description of image here' />

But you will notice that it has surrounded the URL and alt description with single quotation marks (') rather than double ("). I'm a fan of double-quotation marks here. So I've made a simple hack to the upload code:

How to hack upload.php

Here's how to change the code in your WordPress 1.5 installation to ensure that it will always give you double-quotation marks (") in the auto-generated XHTML code:

  1. The code for uploading files is contained in the file wp-admin/upload.php within your WordPress installation.
  2. Open the file in your text editor of choice (I prefer 1st Page 2000) and scroll down to about line 202
  3. There you will see the following lines of code:

    $piece_of_code = "<img src='" . get_settings('fileupload_url') ."/$img1_name' alt='$imgdesc' />";
    else
    $piece_of_code = "<a href='". get_settings('fileupload_url') . "/$img1_name' title='$imgdesc'>$imgdesc</a>";

  4. Simply replace these lines with the following:

    $piece_of_code = "<img src=\"" . get_settings('fileupload_url') ."/$img1_name\" alt=\"$imgdesc\" />";
    else
    $piece_of_code = "<a href=\"". get_settings('fileupload_url') . "/$img1_name\" title=\"$imgdesc\">$imgdesc</a>";

  5. You will notice the subtle alteration of escaped quotation marks (\") replacing the previous single quotation marks ('). The backslash before the quote tells PHP to regard the quotation mark as a text character rather than as part of the syntax of the code.
  6. Now save this file, and upload it (no pun intended) to your WordPress installation.
  7. er…
  8. that's it!

Published by

Gareth Saunders

I’m Gareth J M Saunders, 46 years old, 6′ 4″, father of 3 boys (including twins). Latterly, web architect and agile project manager at the University of St Andrews and warden at Agnes Blackadder Hall. Currently on sabbatical. I am a priest in the Scottish Episcopal Church, and I sing with the NYCGB alumni choir.

One thought on “WordPress: Upload code hack”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.