Development

Wanted (source)code plugin for Wordpress

The past days I’ve been posting stories with a few code samples – Perl, PHP, Javasscript and/or HTML. While Wordpress is fantastic to a number of things, handling sourcecode examples in the posts, reallly isn’t one of them.

I’ve been playing with a number of plugins, but none of them really works for me. What I’m looking for is this:

  • It must be able to handle HTML, Javasscript, PHP and Perl sourcecode.
  • The code samples should work with the visual editor (preferably entered in this, but ”code mode” is quite alright) – Visual post editing shouldn’t break the code example.
  • The plugin should be alive (maintained and tested with recent wordpress releases).

I would have thought it would have been easy to find a plugin that covered the above requirements, but so far I’ve had very little luck – any suggestions?

Changing a form submit url with javascript

Sometimes you might need to change the address a form submits to, it’s quite easy with javascript. One example could be if you - like me - from time to time is hit hard by comment-spammers. Changing the submit URL on the form with javascript, makes it at least somewhat harder for “automated brainless robots” to kill your site in a spam attack.

The recipie below is a very simple example, but so far it seems to work quite well:

<script type="text/javascript">
function doSubmit() {
  var target1 = 'hello';
  var target2 = 'world';
  var target3 = '.cgi';

  var supertarget = target0 + target1 + target2 + target3;
  var theForm=document.getElementById("theForm");
  theForm.action = supertarget;
  theForm.submit();
}
</script>

<form id="myForm" method="post" action="some\_fake\_address.php">
<input type="text" name="dummy field" value="">
<input type="button" onclick="javascript:doSubmit();">
</form>

Bulk resizing images with Perl

Suppose you’ve just filled you digital camera with an endless stream of photos. You want to place them online at your website, but placing 5+ megapixel files online, well…probably a bad idea. Let’s resize them to a propper size - and why not use Perl and ImageMagick for the job.  Not a problem, here’s a complete example on how to resize all images in a directory . Make sure you have ImageMagick installed.

Syntax checking PHP on the commandline

I’m sure most people only thing of PHP as a Weblanguage due to be called through a browser. It has however since version 4.3.0 also been possible to use PHP on the commandline - as you do with Perl, Shell scripts and likewise. If you’re using Linux (or an other Unix-like operating system - including Mac OSX) you probably have a few small programs available which can make it a breeze to check if the syntax in all you PHP scripts is correct.

Converting between image formats with Perl

Changing files from one format to another is quite easy with a little bit of ImageMagick . In the example below a JPG image (test.jpg) is converted into a GIF-image (test.gif). To output in a different (ImageMagick supported ) format, just change the “image->Set” line.

#!/usr/bin/perl -w use strict; use Image::Magick;
my $image = Image::Magick->new();

# To explicitly set image format use this instead: 
# my $image = Image::Magick->new(magick=> 'JPEG');
my $x = $image->Read('test.jpg'); 
$x = $image->Set(magick => 'GIF'); $x = $image->Write('test.gif');
exit();