Three Good Things

Three Good Things: Building and deploying a project with Laravel

David Nash laravel, linux, mysql, php 2 Comments

What do developers do in their spare time? Code! Three Good Things is a little app that I started over the holidays, and have been working on in my spare time. During the Christmas/New Year holiday period I read Flourish: A Visionary New Understanding of Happiness and Well-being, by Dr Martin Seligman. In it he describes an exercise called “What-Went-Well” (or “Three Blessings”). The idea is, every night you write three good things that happened in the last 24 hours, and why they happened. I thought it would be a good idea for an app, so I set to work. It was a …

Pretty print fractions as HTML entities using PHP

David Nash code, html, php 1 Comment

I used this code in a WordPress site but it’s pure HTML. I wanted to take a string, for example “3/4” and convert it to a HTML entity ¾. When there’s no HTML entity I still wanted it to look nice, using <sup> and <sub> for the numerator and denominator respectively. I couldn’t find anything on the web that I could copy and paste so I wrote it myself. Here’s what I came up with:

It’s pretty straight forward. First we check that the string is of the form X/Y. We put the X and Y into an array, …

How to create a select element option (drop-down) menu in the WordPress Theme Customizer

David Nash css, php, wordpress 2 Comments

I’m working on a project that requires the same basic theme for two companies that are both under the same group. The only real difference is the logo and theme colours. Instead of creating two separate themes and having to maintain them separately I used WordPress “customize_register” action in the theme’s functions.php. The official docs are here and very long and confusing. WordPress provides some basic types like a text field, image upload and colour picker, but no select menu: Here’s the code I’m using to create a select element with two options (in the theme’s functions.php file): …

Why I love anonymous functions in PHP 5.3+ for WordPress functions.php

David Nash php, wordpress Leave a Comment

From PHP 5.3, we can now use anonymous functions. If you write jQuery javascript, you probably use these all the time, even if you’re not aware of it:

Here we’re calling the ready function and passing it an anonymous function. We could also do:

(Let’s ignore the “$” for now). It’s the same with PHP. We don’t have to name functions – they can be anonymous. Why do I love it for WordPress? Adding actions and filters always seemed so clunky. We write a do_my_thing() function and then call add_action(), passing the hook and the function name. The …

WordPress: show sticky posts, then show all other posts in one loop

David Nash php, wordpress 1 Comment

This is something that’s driven me a little insane for a while. WordPress posts can be marked “sticky”, which is awesome. But if you’re not using it on the home page it just doesn’t work. In the past I’ve given up and resorted to doing two “The Loop”s, one with just stickies and one without. But there’s an easier way. In your themes functions.php, do this:

There’s nothing else to chage – the filter just steps in and takes care of it! Based on this answer, which I only needed to minimally modify to get working. This uses …

Using the pure white light of PHP to pierce the dark miasma of .NET forms

David Nash php Leave a Comment

Coming from a solid open source/PHP background, any interaction I have with .NET usually leaves me wondering just how those guys at Microsoft were able to stay that high for that long. Maybe they’d made a wish on a cursed monkey hand where they wanted the web to be stateful. Or made a pact with the devil. One of those “always backfires” kind of deals, like when you wish for hot babes and they’re literally one million degrees centigrade. I recently created an application that would interact with a .NET search form. I looked into the face of insanity and …

Use curl and PHP Simple HTML DOM Parser to inject WordPress into another page

David Nash css, html, php, wordpress 3 Comments

I was recently asked to create a WordPress theme that would run on its own server but be integrated into a larger e-commerce site that was running in a separate CMS. The headers and footers for the site change frequently and the WordPress theme needed to insert itself between the two. The basic outline to tackle this problem is: Create a template in the CMS with an empty content area, and set up eg /blog so that instead of using the CMS it  loads content from the WordPress server. Create a WordPress theme that uses the built-in curl library to read …

WordPress Theme Development Quick Start

David Nash css, html, php, wordpress Leave a Comment

I’ve created plenty of WordPress themes based on designs from clients. This is the process I usually follow. 1. Create a static HTML and CSS template. I create index.html and style.css. 2. Install WordPress 3. In the WordPress folder wp-content/themes/ I create a folder with the name of my new theme. 4. Copy my static HTML to the new theme folder. 5. Modify style.css, add the WordPress stylesheet header: /* Theme Name: THEME_NAME Theme URI: YOUR_WEBSITE Description: THEME_DESC Version: 0.1 Author: YOUR_NAME */ 6. Rename index.html to index.php. Open index.php and replace the reference to style.css with < ?php bloginfo( ...

WordPress: Limit Archives to Single Category

David Nash mysql, php, wordpress 9 Comments

Late yesterday afternoon a client asked if I could look at a business’s WordPress installation. They had posts in several categories but only wanted to show the “Latest News” posts in the archives. WordPress is designed around blog posts, whereas I find that many business or company sites are designed around pages (eg About Us, Contact Us) – and don’t use WordPress’ powerful blogging tools on the front page. My site is an example – the content on the home page doesn’t change that much. This means that many of the solutions are also geared around blog-post design. While searching …

Hostmonster 500 Internal Server Error (SHTML Wrapper)

David Nash linux, php 3 Comments

A client using Hostmonster was doing some hair-pulling over what appeared to be a .htaccess problem while installing a Symphony site I developed. We thought it might be related to the fact it was running on a subdomain. The server would give a blank page, and on the HTML source would show something like “500 SHTML wrapper”. It turns out this happens when group write permissions are set on the script. Via the Hostmonster Cpanel File Manager, you can right-click the file, click permissions and uncheck “write” and the script should run. The way I worked this out was: first, …