Outputting JSON with Zend Framework

Today I was looking for a quick solution to outputting a response in JSON format from within one of my existing Zend_Controller actions that could be both requested and parsed using one of the jQuery Ajax methods.

The easiest method I could find was to use something similar to the following which first detects whether or not an XHTTP request has taken place, then outputs the response in plain text format. Continue reading “Outputting JSON with Zend Framework”

WordPress exclude categories from homepage

If you use posts within WordPress to generate content, such as Frequently Asked Questions or Help Guides, it’s quite often the case that you don’t want these to appear on the posts page of your blog.

Instead of having to re-create the whole query, you can append a comma separated list of category ID’s  that you want to exclude to the end of the original query and fetch the new data set to be used. Continue reading “WordPress exclude categories from homepage”

Extending the WordPress mod_rewrite rules

If you have written a WordPress Plug-in, or a highly customised page, and you want to add a custom rewrite rule then this is article is definitely the one for you.

We all know that WordPress is a powerful and pretty impressive blogging platform, and contains many useful features. The problem is finding the features you need / want. Most of the time they are available – somewhere. Continue reading “Extending the WordPress mod_rewrite rules”

Using WP-Syntax and the visual editor

Many of the blog posts I write tend to be about web development related topics and will often include a number of code snippets. My current choice of syntax highlighter is WP-Syntax, which supports a wide range of popular languages and has the ability to also include line numbers.

The problem with using this plug-in is that the WordPress WYSIWYG Editor (TinyMCE), will remove any tags and attributes that it believes to be invalid according to it’s configuration. As two of the attributes used by this plug-in are custom attributes (escaped/line) they are removed, causing some unexpected output. Continue reading “Using WP-Syntax and the visual editor”

WP – Enable read more links for pages

When listing pages using the_loop(), the latest version of WordPress (2.8.4) does not allow for a “Read more…” link to be appended to a list of pages using the_content() along with the <!–more–> quicktag.

Although it wasn’t immediately obvious to me, read more links can be enabled by over-riding the global $more variable and setting it to false, before looping through the result set, for example: Continue reading “WP – Enable read more links for pages”

WP – Add class name to first paragraph in blog post

When re-designing my blog I wanted to display the first paragraph of each blog post in bold text. A CSS3 selector could have been used, but wouldn’t work in older browsers such as IE6.

Instead I decided to write a very simple function to add a class name to the first paragraph by filtering the content, then setting the styles accordingly in my theme CSS file. Continue reading “WP – Add class name to first paragraph in blog post”

Simple PHP Sparklines

Whilst designing a new layout for one of my current projects, I wanted to be able to provide the end user with the ability to quickly scan over a series of statistics.

The main page should act as overview of everything that is going on, therefore I wanted to avoid overcrowding it with lots of large graphs and instead simply show the basic trends.

A few months back I came across an implementation of Sparklines – data-intense, design-simple, word-sized graphics – which are often used to demonstrate stock market activity in a simple visual graphic.

I was just going to use a pre-made script from the many already available, but instead decided to knock one up myself and created a small PHP Class to generate and output a sparkline image using the GD Library.

To create a sparkline I can now simply include the class and then initiate it in a variation of the following:

$sparkline = new Sparkline();
$sparkline->data = Array(3,38,2,8, ... ,38,13,4,23);
$sparkline->output(); // Display image

This PNG image is generated and output to the browser:

Example Sparkline

A number of other setup options are available to customise the output further:

$sparkline->bg = '#000000'; // Background colour
$sparkline->width = 150;
$sparkline->height = 50;
$sparkline->line = '#336699'; // Line colour
$sparkline->points = '#333333'; // Start & end points
$sparkline->thickness = 2; // Line thickness
$sparkline->scale = 5; // Anti-aliasing scale size
$sparkline->output('saved/test.png', true); // Save to location and display

Customised Sparkline

All colours must be provided in hexadecimal format and if you are attempting to save the image the destination location must be writeable by Apache.

The Sparkline PHP Class source code is available for anyone to use as they may like, although I must confess it hasn’t been fully tested as yet!