Posted (Updated ) in Database, PHP

I recently came across a tutorial on sitting Redis infront of WordPress allowing for insanely fast page generation. I gave it a try and it really works, in fact I’m now using it on this very site! The best part however is the fact that the script requires absolutely no modification to your existing WordPress site save for 1 line of htaccess. Truly amazing.

Below I’ll detail my slightly modified version of Jim’s script along with some metrics.

 

Firstly, What is Redis and what will it do for me?

The Redis website describes Redis as

… an open source, BSD licensed, advanced key-value store. It is often referred to as a data structure server since keys can contain stringshasheslistssets and sorted sets.

What does this mean? Essentially it’s Memcached but more useful. Redis stores key-value pairs in memory and spits them out when requested. Unlike Memcached it has built in persistence but what’s most important to us is that it’s fast – very fast.

We’ll be using Redis to speed up our site by loading cached pages from it directly without even booting up WordPress. This will save a large amount of page generation time and get out site infront of our users’ eyeballs faster.

 

Exactly how much faster are we talking?

In my very unscientific tests, loading www.flynsarmy.com a bunch of times resulted in the following:

Before (Secs) After (Secs)
1.556
0.468
0.494
0.498
0.492
0.514
0.499
0.511
0.499
0.02001
0.00896
0.00883
0.00959
0.01472
0.00916
0.00915
0.00756
0.01989

As you can see from the table above this equates to a 20x to 50x speed increase and that was WITH W3 Total Cache installed! Results of course may vary but I think you get the picture.

Read More »

Posted (Updated ) in PHP

I have a project coming up involving editing WordPress posts from the front end of the site. There are a bunch of plugins that let you do this the coolest of which seems to be Front-end Editor but I wanted to come up with my own solution. Luckily it turned out to be surprisingly quick and painless!

For this tutorial I’ll be editing the cafe custom post type from my last post.

Read More »

Posted (Updated ) in PHP

This information is pretty readily available on the internet but I figured I’d make my own post for safe keeping. I’ll be adding a custom post type (Cafes) with 3 custom fields (Website, Address and Phone) and a custom taxonomy (Countries).

The following code all goes in your functions.php file.

 

Custom Post Types

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
 * Add Cafe post type
 */
function create_cafe_post_type() {
	register_post_type( 'cafes',
		array(
			'labels' => array(
				'name' => 'Cafes',
				'singular_name' => 'Cafe',
				'add_new' => 'Add New',
				'add_new_item' => 'Add New Cafe',
				'edit_item' => 'Edit Cafe',
				'new_item' => 'New Cafe',
				'view_item' => 'View Cafe',
				'search_items' => 'Search Cafes',
				'not_found' =>  'Nothing Found',
				'not_found_in_trash' => 'Nothing found in the Trash',
				'parent_item_colon' => ''
			),
			'public' => true,
			'publicly_queryable' => true,
			'show_ui' => true,
			'query_var' => true,
			//'menu_icon' => get_stylesheet_directory_uri() . '/yourimage.png',
			'rewrite' => true,
			'capability_type' => 'post',
			'hierarchical' => false,
			'menu_position' => null,
			'supports' => array('title','editor','thumbnail')
		)
	);
}
add_action( 'init', 'create_cafe_post_type' );

 

Custom Taxonomies

Taxonomies are basically tags for your post types. Add the following below the register_post_type call above:

1
2
3
4
5
6
7
//Cafe countries taxonomy
register_taxonomy("countries", array("cafes"), array(
	"hierarchical" => false,
	"label" => "Countries",
	"singular_label" => "Country",
	"rewrite" => true
));

 

Custom Fields

This is the hardest of the lot. It’s not really hard though, just alot of HTML to make the meta boxes. You can put any HTML you like in meta boxes, I just like to keep mine looking like standard WP fields:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
/**
 * Add cafe custom fields
 */
function add_cafe_meta_boxes() {
	add_meta_box("cafe_contact_meta", "Contact Details", "add_contact_details_cafe_meta_box", "cafes", "normal", "low");
}
function add_contact_details_cafe_meta_box()
{
	global $post;
	$custom = get_post_custom( $post->ID );
 
	?>
	<style>.width99 {width:99%;}</style>
	<p>
		<label>Address:</label><br />
		<textarea rows="5" name="address" class="width99"><?= @$custom["address"][0] ?></textarea>
	</p>
	<p>
		<label>Website:</label><br />
		<input type="text" name="website" value="<?= @$custom["website"][0] ?>" class="width99" />
	</p>
	<p>
		<label>Phone:</label><br />
		<input type="text" name="phone" value="<?= @$custom["phone"][0] ?>" class="width99" />
	</p>
	<?php
}
/**
 * Save custom field data when creating/updating posts
 */
function save_cafe_custom_fields(){
  global $post;
 
  if ( $post )
  {
    update_post_meta($post->ID, "address", @$_POST["address"]);
    update_post_meta($post->ID, "website", @$_POST["website"]);
    update_post_meta($post->ID, "phone", @$_POST["phone"]);
  }
}
add_action( 'admin_init', 'add_cafe_meta_boxes' );
add_action( 'save_post', 'save_cafe_custom_fields' );

 

Custom Templates

You may want your new post type to look different from standard posts. This can be done using custom templates. The WordPress codex cover custom post type templates nicely but I’ll quickly go over what I did for completeness. I’m using twentytwelve theme in my demo but this should apply for most themes.

  1. Duplicate single.php into single-<post type>.php
  2. Replace
    <?php get_template_part( 'content', get_post_format() ); ?>

    with

    <?php get_template_part( 'content', '<post type>' ); ?>
  3. Duplicate content.php into content-<post type>.php and modify however you like

 

Conclusion

And we’re done! Always remember to check the WordPress documentation for the various functions involved if you want to customize. Lots of useful information in there.

Read More »

Posted (Updated ) in PHP

This morning I was loading WPMU in a custom script and all get_option() lookups were returning the details for the main blog despite being in a switch_to_blog() block. After a bit of debugging it turns out this was due to get_option() caching its values from the main blog and never expiring its cache when switching. There’s a pretty easy action you can use to fix this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
 
$_SERVER['HTTP_HOST'] = 'yourdomain.com';
 
ini_set('display_errors', true);
 
/**
 * BEGIN LOAD WORDPRESS
 */
function find_wordpress_base_path() {
	$dir = dirname(__FILE__);
	do {
		//it is possible to check for other files here
		if( file_exists($dir."/wp-config.php") ) {
			return $dir;
		}
	} while( $dir = realpath("$dir/..") );
	return null;
}
 
define( 'BASE_PATH', find_wordpress_base_path()."/" );
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;
require(BASE_PATH . 'wp-load.php');
 
/**
 * END LOAD WORDPRESS
 */

Below is a script in its entirety for loading WPMU, switching to blog 31 and grabbing its name:

1
2
3
4
5
6
7
8
function switch_to_blog_cache_clear( $blog_id, $prev_blog_id = 0 ) {
    if ( $blog_id === $prev_blog_id )
        return;
 
	wp_cache_delete( 'notoptions', 'options' );
	wp_cache_delete( 'alloptions', 'options' );
}
add_action( 'switch_blog', 'switch_to_blog_cache_clear', 10, 2 );

Read More »

Posted (Updated ) in Linux

The below is a collaboration of useful information I’ve found while attempting to build and maintain a RAID5 array consisting of 4 HDDs. This tutorial is a work in progress and I’m learning everything as I go. I’ve left relevant links in each section for more information and if you spot anything that could be done better or that I’m missing let me know in the comments below!

Read More »

Posted in Uncategorized

SublimeCodeIntel is perhaps the most useful and heavily installed package for Sublime Text but it’s also very difficult to get working. The most common issue people run into is the dreaded endless

Info: processing `PHP’: Please wait…

This can be fixed by navigating to the SublimeCodeIntel package directory and running build.sh. For build.sh to compile we first need to install two things: pcre and XCode Command Line Tools. Annoyingly this is around 1.6GB of files!

 

Installing XCode Command Line Tools

Head over here and click View in Mac App Store. Hit install and watch your progress in the Purchases tab.

Once installed open it up and go to XCode – Preferences – Downloads – Components and install Command Line Tools.

 

Installing pcre

This little script is required by the scintilla package of SublimeCodeIntel. First install MacPorts then run:

sudo port install autoconf
sudo port install pcre
sudo cp /opt/local/include/pcre.h /usr/include/

You’re done! Navigate to the SublimeCodeIntel package directory and run sh src/build.sh. If all goes well at the end of compilation you’ll see a done! message. Restart Sublime Text and you’re good to go.

Read More »

Posted (Updated ) in Linux

So I don’t forget, here’s a tutorial on installing the RocketRaid 2760a drivers and management utilities on Ubuntu 12.10. I’m using 64-bit but 32-bit should be the same – just substitute where appropriate.

Installing the Driver

The installation disc comes with an out of date version of the driver and kernel module. The way the installation works, we need to download and compile the kernel module then download the driver installer, drop the module into place and run the installer. Make sure you have appropriate compiler tools installed – to be safe just run

sudo apt-get install ubuntu-dev-tools

Download and extract the ones with kernel 3.x support from the HighPoint website:

mkdir ~/driver
cd ~/driver
# driver
wget http://www.highpoint-tech.com/BIOS_Driver/RR276x/linux/Ubuntu/rr276x-ubuntu-11.10-x86_64-v1.1.12.0502.tgz
tar -xvzf rr276x-ubuntu-11.10-x86_64-v1.1.12.0502.tgz
# kernel module
mkdir module
cd module
wget http://www.highpoint-tech.com/BIOS_Driver/RR276x/linux/RR276x-Linux-Src-v1.1-120424-1734.tar.gz
tar -xvzf  RR276x-Linux-Src-v1.1-120424-1734.tar.gz

Build and compress the kernel module for our driver installer:

cd rr276x-linux-src-v1.1/product/rr276x/linux/
make
# Ignore the warning about not being able to find him_rr2760x.o...doesn't seem to matter
gzip rr276x.ko
mv rr276x.ko.gz ~/driver/boot/rr276x$(uname -r)$(uname -m).ko.gz

And finally install the driver

cd ~/driver
sudo bash preinst.sh

This step succeeded! Now you can press ALT+F1 to switch back to the installation screen!

sudo bash install.sh

Update initrd file /boot/initrd.img-3.5.0-17-generic for 3.5.0-17-generic
Please reboot the system to use the new driver module.

sudo shutdown -r now

That should be everything! You can now test with

cat /proc/scsi/rr276x/*

 

Installing the RAID Management Software

Annoyingly the RAID management console is difficult to install to say the least. The GUI deb packages error when trying to install and HighPoint don’t even provide a deb for the command line version and to top it off the version of the command line utility on the driver CD is newer than the version on their site! For this reason I’ve provided the newer command line RPMs here.

Let’s get this thing installed.

Web version:

mkdir ~/driver/utility
mkdir ~/driver/utility/console
mkdir ~/driver/utility/web
cd ~/driver/utility/web
echo "rr276x" | sudo tee -a /etc/hptcfg > /dev/null
wget http://www.highpoint-tech.com/BIOS_Driver/GUI/linux/WebGui/WebGUI-Linux-v2.1-120419.tgz
tar -xzvf WebGUI-Linux-v2.1-120419.tgz
sudo apt-get -y install alien
sudo alien -d hptsvr-https-2.1-12.0419.$(uname -m).rpm
sudo dpkg -i hptsvr-https_2.1-13.0419_amd64.deb

 

Command Line version:

cd ~/driver/utility/console
wget https://www.flynsarmy.com/wp-content/uploads/2012/11/LinuxRaidUtilityConsole.tar.gz
sudo alien -d hptraidconf-3.5-1.$(uname -m).rpm
sudo alien -d hptsvr-3.13-7.$(uname -m).rpm
sudo dpkg -i hptraidconf_3.5-2_amd64.deb

Run the web server:

sudo hptsvr

You should now be able to connect to it from your browser by navigating to http://localhost:7402 with username RAID and password hpt.

 

Further Reading

HOWTO: Get GUI hptsvr & hptraid 3.13 working on Ubuntu
HighPoint driver download page for RocketRaid 2760a

Read More »

Posted in Linux

The below is a quick guide to creating partitions on a newly purchased, unformatted disk. For this guide I’ll be formatting a new WD 2TB Black.

 

Find the disk you want to partition

# lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 55.9G 0 disk
├─sda1 8:1 0 53.6G 0 part /
├─sda2 8:2 0 1K 0 part
└─sda5 8:5 0 2.3G 0 part [SWAP]
sdb 8:16 0 1.8T 0 disk
sdc 8:32 0 1.8T 0 disk

Partitions appear as subitems. Notice sdb and sdc have no partitions – those are the disks I want to format.

 

Create the partition

# sudo fdisk /dev/sdb

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xd3e43840.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won’t be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help):

Below I’m using the default values to create one large partition for the whole disk.

n

Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended

Select (default p):
Using default response p
Partition number (1-4, default 1):
Using default value 1
First sector (2048-3907029167, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-3907029167, default 3907029167):
Using default value 3907029167

Command (m for help):

All done? Write!

w

The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

That’s all for partitioning! Take a gander at your fancy new partitions:

# lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 55.9G 0 disk
├─sda1 8:1 0 53.6G 0 part /
├─sda2 8:2 0 1K 0 part
└─sda5 8:5 0 2.3G 0 part [SWAP]
sdb 8:16 0 1.8T 0 disk
└─sdb1 8:17 0 1.8T 0 part
sdc 8:32 0 1.8T 0 disk
└─sdc1 8:33 0 1.8T 0 part

 

Format to NTFS

I’m choosing a quick format. You may choose instead to remove the -f argument for a proper one instead.

# sudo mkntfs -f /dev/sdb1

Cluster size has been automatically set to 4096 bytes.
Creating NTFS volume structures.
mkntfs completed successfully. Have a nice day.

 

Optional: Name the drives

Because I’m running NTFS I can give the drives labels. Make sure you have ntfsprogs installed then enter:

# sudo ntfslabel -f /dev/sdb1 YourLabel

 

More Information

For more information on partitioning check out Chris Wakefield’s post here.
For a short and sweet howto on  formatting as NTFS check here.
Information on NTFS labels here.

Read More »

Posted (Updated ) in Uncategorized
Removed 'Clear Scrollback' shortcut

Removed ‘Clear Scrollback’ shortcut

This would have to be far and away the most annoying ‘feature’ of Pidgin.

In just about every web browser, Ctrl+L is used to highlight the URL bar allowing easy navigation via the keyboard. In pidgin it’s used to clear the scrollback of the active window. As a web developer I’m always hitting Ctrl+L in the browser however every now and then I won’t take notice to which window is active on my desktop at the time and use it while Pidgin is active, clearing out the conversation I’m having with my contact at the time. Frustrating!

There was a bug post here about it but as usual the devs don’t particularly care and instead just pointed to their FAQ page. For convenience here’s how you remove the shortcut:

Open ~/.purple/accels and change

; (gtk_accel_path "<PurpleMain>/Tools/Preferences" "<Primary>p")

to

(gtk_accel_path "<main>/Conversation/Clear Scrollback" "")

Remember to remove the semicolon at the start of the line!

 

Read More »

Posted (Updated ) in Uncategorized

One week from this post I will be publishing an update for my Banner Slideshows module for LemonStand which upgrades it to use the newly released Nivoslider 3. This will provide a few nice benefits however the implementation isn’t completely backwards compatible so your frontend code may need updating. Details inside.

What do I need to do?

Simply press the Update slideshow button on your slideshow page in the Administration:

Hit the update button indicated above

Hit the update button indicated above

Then remove your existing slideshow code on your sites frontend and follow the instructions on the marketplace page (to be updated when the module update is released) to add the new version.

How does this benefit me?

The biggest benefit is its new responsive design which resizes with your browser – making it easier to build sites that scale from a desktop monitor all the way down to a mobile phone. I’ve whipped up a live demo to see this feature in action. Open it up and resize your browser window. Notice the slideshow resizes with the window.

Also new in this version of the module is full HTML support for slide descriptions:

HTML support for slide descriptions

HTML support for slide descriptions

Read More »