WordPress search form snippet

David Nash wordpress Leave a Comment

This is code to use for a search form, for example in the header area of a WordPress theme. It retains the previous search query in the text field. [html] <form action="<?php echo home_url(‘/’); ?>"> <input type="text" name="s" value="<?php the_search_query(); ?/>" /> <button type="submit">Search</button> </form> [/html]

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. 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): …

How to make WordPress more like a CMS

David Nash wordpress Leave a Comment

WordPress is wonderful for its core functionality – blogging. And it’s pretty good for other stuff too, but sometimes it takes a little shoe-horning to get it working like a real CMS. I recently used the Advanced Custom Fields plugin and it’s excellent. As a developer I can set up fields that only apply to specific pages, and then use those fields in the themes I develop. My clients then have a simple, easy to use interface for entering single lines of text, or selecting images, or… basically anything you can think of. To make it really powerful, you need …

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: [js] jQuery(document).ready(function($) { //… }); [/js] Here we’re calling the ready function and passing it an anonymous function. We could also do: [js] function do_stuff() { //… } jQuery(document).ready(do_stuff); [/js] (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 …

Caffeine and Code: Not always the best combination

David Nash General Leave a Comment

I have a secret: I don’t drink caffeine at all. It’s been about a year now and I don’t think I’ll go back. I know: coders and caffeine go together like two sequential elements in an array. Every programmer I know drinks coffee, and a lot of it. I was the same. A cup in the morning and a cup just after lunch. Sometimes more. I’d always done it. I needed it, but I was never really sure if it was actually helping. Sure, it would take away the exhausted feeling for an hour, but it would come back even …

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: add_filter(‘the_posts’, function ($posts) { $stickies = array(); foreach($posts as $i => $post) { if(is_sticky($post->ID)) { $stickies[] = $post; unset($posts[$i]); } } return array_merge($stickies, $posts); }); There’s nothing else to chage – the filter just …

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 …

Website design refresh

David Nash wordpress Leave a Comment

Time flies when you’re having fun. And coding is a lot of fun, so time goes very fast. I’ve been meaning to add new clients to this site for a while, the big one being General Pants Group who I recently helped out by developing a WordPress theme. I was very excited to work for such a well known brand. I’ve also refreshed the design of this site a little. The full screen background image is now clouds, and I changed the colour scheme to match. I’ve included screenshots of some of the great projects I’ve developed on the front page, …

Disable WordPress Theme “Update Available” Notification

David Nash wordpress 20 Comments

I’ve been customising a theme that had a new version, getting the message “There is a new version of … available”. I didn’t want the end-user to update the theme, because I’d customised it so heavily. I know that any updates are going to need to be done by a WordPress theme developer. After a while of searching through code, looking for add_action and add_filter hooks, and even delving into the database, I realised I could just edit the theme’s style.css and remove the “Version:” line from the WordPress specific CSS header right at the top. And the notification (and …