Display WooCommerce category description and custom category titles

WooCommerce category description and custom title

David Nash woocommerce, wordpress 0 Comments

The WooCommerce category description is a good way to increase usability and improve SEO on your site. We can also show a custom category title which is a little more descriptive. To do this, it’s good practise to start with something easy, get that working, and then build from there. Show the WooCommerce category description In the child theme, if it doesn’t already exist, create a woocommerce/ directory. Copy the template file from wp-content/plugins/woocommerce/templates/archive-product.php  to wp-content/themes/[your-child-theme]/woocommerce/archive-product.php. Now we can edit that without changing any plugin files directly (which could get replaced on the next update). In WooCommerce 2.0.0+, towards the top of …

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

David Nash css, php, wordpress 1 Comment

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. https://codex.wordpress.org/Theme_Customization_API WordPress provides some basic types like a text field, image upload and colour picker, but no select menu: https://codex.wordpress.org/Class_Reference/WP_Customize_Manager/add_control 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 0 Comments

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 http://wordpress.stackexchange.com/a/87788, 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 0 Comments

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 …

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 …

Using PHP variables that contain hyphens

David Nash php 2 Comments

This took me a little while to work out. I’m using PHP5’s SimpleXML to parse XML into a PHP object. The XML has entries like <HELLO-THERE>. But you can’t use $xml->HELLO-THERE because it reads the hyphen as a minus. Instead, use $xml->{“HELLO-THERE”} Easy!

Dynamic Font/Image Replacement in Silverstripe

David Nash silverstripe, What I'm Working On Today 15 Comments

I’ve recently started creating sites with SilverStripe CMS, and I’m loving it. My client wants nicely rendered non-standard font titles that fade in and out, without using javascript. Here’s how I’m going to accomplish it: In Silverstripe’s mysite/code/Page.php I overload the onBeforeWrite() call in my Page class. This intercepts the data before it is written to the database. I can get the title of the updated page from $this->Title I will then use PHP’s GD (graphics) library to create a PNG with a transparent background. I’ll use imagettftext() to load a font from a TTF file and write it to …

Unix Time in MySQL and bash

David Nash bash, mysql 0 Comments

A quick one today. I was working on a mysql database that used unix timestamp produced by PHP’s time() function. I needed to be able to quickly convert this time to a human-readable format. In bash, date -d @timestamp is a quick way to convert. In a terminal shell eg: # date -d @1224992980 Sun Oct 26 14:49:40 EST 2008 In a MySQL client, you could also use select date(from_unixtime(column_name)) from table_name; Or if you want a little more flexibility in the output, for example outputting 27/02/09, you could do: select date_format(from_unixtime(column_name), ‘%d/%m/%y’) from table_name; This post is one of …

vim: Quickly assign POST variables in PHP

David Nash vim 0 Comments

You’ve got a web form with lots of fields, and you want to POST them to a PHP script. Open vim, and list the INPUT tag’s NAME attributes, one per line. <?php firstname lastname address1 address2 state postcode ?> Now with some search-and-replace magic we can save ourself a lot of boring typing. Hit ‘escape’ to get out of insert mode, type a colon (“:”) and copy-paste this: %s/^\(\S\+\)/\$\1 = \$_POST\[\’\1\’\]; I won’t bother explaining it unless someone asks. But what you should end up with is this: <?php $firstname = $_POST[‘firstname’]; $lastname = $_POST[‘lastname’]; $address1 = $_POST[‘address1’]; $address2 = …