Yet another programmer blogging about code

Archive for the ‘Snippets’ Category

Sort WooCommerce products first in search results

Thursday, December 18th, 2014

When shoppers search a WordPress website, you want them to find products first before listing any articles and pages. And it’s surprisingly easy to achieve, with a simple filter hook. (more…)

Set the tab size on GitHub, Gist, and Bitbucket

Wednesday, September 3rd, 2014

I indent with tabs. There, I said it. Viewing my code (and that of other tab indenters) on GitHub, Gist, or Bitbucket can be annoying because the default tab size in the browser is equivalent to 8 spaces. Modern browsers let you change that through CSS, and here’s some bookmarklets that do just that. (more…)

Move Gravity Forms field labels above input fields

Saturday, July 19th, 2014

Gravity Forms has some nice compound fields to make it easy to accept things like names, addresses, and credit card details. One annoying thing it does, though, is put the labels for the input fields below them instead of above them. Here’s how to move Gravity Forms field labels above input fields where most people would expect them to be. (more…)

Load Gravity Forms scripts for custom field

Monday, June 30th, 2014

Gravity Forms is a very easy to use yet flexible tool for building forms in WordPress. It uses quite a bit of JavaScript and custom CSS, so it’s careful to only load its scripts and stylesheets when needed. But if you put a form into a custom field, e.g via Advanced Custom Fields, how will Gravity Forms know it needs to load them? (more…)

Remove register link from WordPress wp-login.php

Tuesday, April 29th, 2014

I recently had to enable user registrations on a WordPress multisite, so that shops on that site could allow customers to register. I don’t want users to register any other way, only through specific applications on specific subsites. Enabling user registrations adds a “register” link to the wp-login.php script page. That invites trouble! (more…)

Remove SKU from WooCommerce front end

Friday, April 25th, 2014

If you need to have SKUs on products in WooCommerce, but don’t want to show them on the front end, you can’t just untick an option in the WooCommerce settings: you can either have and show SKUs or not have them at all. So here’s a quick snippet that lets you have them, but remove them from the front end. (more…)

Remove the WordPress post date filter on custom post type admin pages

Sunday, March 2nd, 2014

WordPress custom post types can be very useful for storing all sorts of different types of data in WordPress — and I should really write a post about that some time. But the date a post was published, i.e. its post_date, isn’t important for many custom post types. So why have a drop-down list of dates to filter your custom posts types by if you don’t need it? (more…)

Stop CloudFlare Rocketscript breaking WordPress plugin scripts

Friday, February 28th, 2014

CloudFlare offers a service called Rocketscript, which compresses, concatenates, and defers any JavaScript on your web pages. On some websites, it can make a significant difference to the apparent page load time, because it waits until the web browser has loaded the page before it loads and executes scripts. Of course, that sometimes means it breaks things! Fortunately, CloudFlare offers a way to avoid that, and it’s possible to use it on your WordPress plugin scripts. (more…)

sessionStorage with expiry time

Saturday, November 16th, 2013

Session storage is a very handy tool for caching content fragments retrieved via AJAX. Once we’ve pulled the content once, and stuffed it into session storage, we can access it again quickly without the overhead of a round trip to the server. But what if we want to limit the age of that content, so that it expires before it gets too stale? (more…)

WooCommerce quantity fields without spinners

Tuesday, November 12th, 2013

WooCommerce uses HTML5 number fields for shop quantities, because they restrict the characters you can enter, and Safari on iPad/iPhone conveniently shows the number keyboard. Webkit and Opera/Presto add spinners (up/down arrows) to HTML5 number fields. WooCommerce also adds +/- buttons surrounding qty fields, because IE and Firefox don’t add spinners. WooCommerce then uses CSS to hide the spinners on Webkit: (more…)