Sulby Theme Slider

January 23, 2018

#CSS #JavaScript #jQuery #JSON #PHP #Shortcode API #Sulby Theme #WordPress


I built this simple slider for my standalone "Sulby" WordPress theme. Written in PHP and JavaScript with jQuery + CSS defining the layout, responsiveness etc. The code is below.

In several past projects we used a well known third-party slider. Now I wanted something more minimal, lightweight and extensible. If we want to change the look and feel then we can do that that in CSS. If we want different functionality then let’s do it in the code. It’s much simpler than trying to work out how to achieve a result via a convoluted plugin interface or restrictive settings.

The PHP creates a WordPress Shortcode. The WordPress Shortcode API can be used to create macros which can be added to a Post or a Page – or to a PHP template using the do_shortcode() function. Eg the slider shown here is added like this:

[insertSulbySlider images=1005,992,1003,1002,993 message-title="Slider Demo" message-body="stock images from my legacy portfolio at Getty Images & iStockPhoto" slider-caption="Bunhill / Getty Images" slider-caption-link="" delay=8 aspect-padding=56.25 header-bool=false]

The images are added by ID. The json_encode function sanitizes this input and then translates the data to a JSON string to be passed to a JavaScript function. The $custom_aspect variable allows me to specify the aspect ratio of the slider. The TL;DR:

eg – 100 = 1:1 / 75 = 4:3 / 66.66 = 3:2 / 56.25 = 16:9

We need to be able to specify the aspect ratio every time. The slider is a div background image but there is not currently a way in CSS to set height such that it is dynamically proportional to widthcalc only works with vw and vh. We could do it in Javascript but that would be clunky (the function would be repeatedly called as the user resized a browser window). Instead we set width to 100% and height to 0 (in the CSS). Then use padding-top as a percentage. The method is described here at

The $header_bool variable flags a specific exception – if the slider is at the top of the page then it wants to be approaching full height relative to the device or browser window – 79vh set in the JavaScript. This particularly matters on phones and the $custom_aspect variable is effectively ignored.

Each slider needs a unique #ID – because elements must be unique per page and potentially we may have more than one slider per page with different properties. That means naming them dynamically. PHP’s uniqid() is a perfectly adequate solution here.

Show Code

WordPress Customised Data Entry Interface – PT2

December 18, 2017

#AJAX #JavaScript #jQuery #JSON #PHP #WordPress

Our [ Product ] entity is as a Custom Post Type. Looking at the list of product items, we want to be able to choose which fields are shown as columns. We can simply achieve this result by combining manage_{$post_type}_posts_columns & manage_{$post_type}_posts_custom_column

simple data model
Custom [ Product ] listing – selected fields only shown here

Show Code

WordPress Customised Data Entry Interface – PT1

December 16, 2017

#AJAX #JavaScript #jQuery #JSON #PHP #Relational Models #WordPress

The client needed a customised backend for a WordPress site to facilitate data entry for a small catalogue of products (fewer than 100 items in total). There were 3 different specific types of product over multiple lines.

Clearly we need to begin by establishing a logical data model. The entity relationships look like this:

simple data model
Simple data model

[ Product ] is a Custom Post Type. I also created additional Custom Taxonomies for fields which would potentially need to be searchable using WP_Query or WP_Term_Query. This is a rather simplified version of what I successfully implemented – the client version included several additional taxonomy fields. [ Product Type ] and [ Product Line ] are logical entities. [ Product Version ] is arguably just a grouping of [ Product ] fields. We don’t actually need to worry about that distinction here – because of the way in which the underlying database structure is abstracted by the WordPress API.

Product - as a Custom Post Type
Product – as a Custom Post Type – with additional Custom Taxonomies
client products plugin
Client products plugin

In this case it made sense to separate the product catalogue functionality by coding it as a WordPress plugin. It comprises two files – the PHP and the JavaScript. With WordPress we can customise the backend user interface (the Dashboard) using custom meta boxes. The user needs to be able to enter the data for each field. As shown in the example here which is demonstrated with dummy data for an imaginary product. There is a good description of the metabox api at Theme Foundation’s WordPress Meta Boxes: a Comprehensive Developer’s Guide.

Custom Data Entry Interface
Custom data entry interface for [ Product Item ] fields
Custom Data Entry Interface
Custom data entry interface for [Product Item] fields

Some of those fields are just text boxes – the user can type in anything (though obviously that data needs to be sanitized before being written to the db). Sometimes the user will be selecting an image (or a PDF) from the Media Library (or uploading a new media item). Other times the user may selected from a predefined list (with radio buttons). Or the user may choose an item from a list where the option also exists to add a new item to the list.

When the user has completed or finished editing the fields for a product – then they publish, the same as any other post. But if a user is adding a new field item – for example a new [ Product Line ] then we want them to be able to do that without the need to reload the page in full. We use AJAX to update the database and the page. There is a good tutorial here at – about how to implement and secure wp-ajax. Even with this being on the admin side, rather than user facing, it is important that the callback is properly secured. The following PHP shows how the metabox for [ Product Version ] is implemented:

taxonomy options
Taxonomy options

Show Code

Here is the corresponding JavaScript (jQuery). Any taxonomy which has an add button will use the same function. The client version has several additional similarly coded taxonomies which I am not including in this simplified example.

Show Code

This is how the AJAX side of that is implemented back in the PHP:

Show Code

Finally saving [ Product Version ] – called on post:

Show Code

Show All Posts