Minimal AJAX in WordPress

Minimal AJAX in WordPress

David Nash code, php Leave a Comment

Here’s what it takes to create minimal AJAX in WordPress. First, let’s create a button: Then functions.php include a Javascript file called do_ajax.js. It has the dependencies jquery and wp-util: Also in functions.php, we set up a function to handle the AJAX call, and add the actions: Finally, in our do_ajax.js file: And then in your console you should see “Hello!”. That string is being sent from do_ajax.js to the server, and the server simply sends it back again. That’s it – we have minimal AJAX in WordPress up and running! This post was based on this StackOverflow answer, and …

Display WooCommerce category description and custom category titles

WooCommerce category description and custom title

David Nash woocommerce, wordpress 28 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 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): …

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 …

Disable WordPress Theme “Update Available” Notification

David Nash wordpress 21 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 …

WordPress upgrade “Briefly unavailable for scheduled maintenance. Check back in a minute.”

David Nash wordpress 2 Comments

I just updated my WordPress installation from 3.2 to 3.3. The upgrade was running but then the page directed to this message: Briefly unavailable for scheduled maintenance. Check back in a minute. The front page and wp-admin both showed this unstyled message. After a quick search I found that simply deleting the .maintenance file via FTP will remove this. Perhaps it’s a bug in this version, perhaps something went wrong. But after deleting that file everything seems to be working well. And I must say I like the design tweaks in WordPress 3.3.

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 …

New WordPress theme for my site

David Nash css, wordpress 3 Comments

I’ve been meaning to update my website’s design and layout for a long time. My site is based on WordPress and the theme I’d been using previously was a little too sterile. I wanted something that was more of a portfolio site and yet could still accommodate a blog. After sporadic searches through the free WordPress themes on offer, I decided none of them quite did what I wanted, so I designed my own. I wanted to keep it minimal while making use of emerging web technologies like HTML5, CSS3; and I wanted the site to look just as good …

Duplicate WordPress site for local development and testing environment

David Nash mysql, wordpress 4 Comments

Update: This is not the best way to do this – use Search and Replace for WordPress databases instead. 1. Download all files from eg, upload to eg http://localhost/example/. 2. Export mysql database from 3. Create a local mysql database with the same user, password and database name. These can be found in wp-config.php 4. Import the database into your new local account 5. Log in to the database, and do: update wp_options set option_value=’http://localhost/example/’ where option_id=1 You’ll now be able to log in at http://localhost/example/wp-admin, using the same username and password as 5. In WordPress, click ‘Settings’ …