Category Archives: Geek

Pixel Density vs CSS

Pixel Density. This post is part sharing my research over the past few weeks, and part continuing it by requesting your feedback in the comments below.

Years ago everyone got excited for Retina and it’s high density pixels. Now every smart phone has it’s own configuration all the up to a Sony with 806ppi (a PC monitor usually has 72px).

The question is – how do we accommodate this so a phone can look awesome and a PC can still load fast and efficiently.

Vectors

The real answer, it seems, is to use vectors wherever possible. SVG support is here in all recent versions of all major browsers. Perfect for icons and fancy elements.

HTTP2 Push means we don’t even need to sprite them, but you still can if you wish.

Fonts are vectors, CSS is all vectors. Looking good.

Bitmaps

For bitmaps – photos and graphics – we have srcset so we can tell the browser which sizes of the image are available, and let the device pick the best size (based on pixel density, network speed, user preference – amazing!). Srcset works everywhere except IE (but srcset falls back to the old src way anyway, so no difference).

The PX Value in CSS

The big question is what do we do with “px” values in CSS – which are increasingly meaningless. They could just become an arbitrary CSS value which devices calculate against their Pixel-Ratio and that may well work forever.

Another possibility is do we use Viewport values for everything – one viewport value is 1% of the width or height of the viewport. It’s an exciting new value!

And guess what? Viewport Units are already widely supported!

Conclusion

In short: The situation is pretty great  🙂 The PX value is probably going to stick around for a bit (unless I’ve totally missed something) but devices know that and they’re happy to accommodate you for now.

Other Questions?

If you have other comments, or questions, leave them in the comments below.

 

Categories:

What Minimum Screen Size for Responsive Sites?

Until recently I’ve been testing responsive sites down to 320px out of habit but without really knowing whether it’s worth the effort. After all, Bootstrap 3 only has media queries down to 768px and Bootstrap 4 will be 34em (roughly/often 544px).

So should we bother optimising responsive websites for 320px, 480px, 768px or bring back the 1990s with Best Viewed in 800×600

I had a play with some stat from statcounter.com to find out.

My Process

  1. Show Screen Resolutions for the 12 months up to June 2016.
  2. Filters it for data for the UK (since most of my sites are mostly visited by people in the UK)
  3. Delete all screen resolutions wider than 1000px.
  4. Combine the matching widths – ignoring heights.
  5. Plot them on a graph

The Graph

The graph has two lines – one for individual market share and one for accumulative market share (i.e. how many people have 800px or smaller).

The aim was to find where the browser width jumps to a notable proportion.

responsive-webdesign-optimisation-width

The Results

The results are fascinating! 2.5% of visits come from a browser less than 720px wide but 15% have a screen bigger than that – and nearly double that have at least 800px.

The case for a minimum 720px Width

What to optimise for depends on your market – if you’re happy with 2.5% of your visitors getting frustrated that your site does not work – then don’t bother with less than 720px.

The case for a minimum 320px Wide Responsive Site

Optimising for 320px instead of 360px will only help 0.12% (one person in every thousand) use your site better – for a small business website getting 75 hits a day that’s one person every fortnight who’ll have a less than optimal time on your site.

Finding out who (or what, if they’re bots) are using very narrow screens would also be interesting. Are these bots, browser bugs or perhaps people just making a window smaller to get it out the way for a while? If they’re not your target audience, you can forget them and spend your time making the wider-screen experience excellent.

Raw Data

If you want to have a play yourself, here’s the data formatted as CSV:

Screen Width,Share %,Accumulative Share %
320px,0.121538461,0.121538461
360px,0.576923077,0.698461538
412px,0.049230769,0.747692307
480px,0.011538462,0.759230769
534px,0.244615385,1.003846154
540px,0.01,1.013846154
600px,0.947692308,1.961538462
601px,0.293846154,2.255384616
640px,0.063846154,2.31923077
720px,0.022307692,2.341538462
768px,12.76846154,15.11
800px,13.14846154,28.25846154
819px,0.019230769,28.27769231
854px,0.266153846,28.54384616
900px,0.016923077,28.56076923
911px,0.056153846,28.61692308
962px,0.011538462,28.62846154

Feedback

I’m not statistician and other interpretations of these results might be useful. If you have any feedback or other thoughts – let me know in the comments below.

Tags:
Categories:

Download MyDefrag-v4.3.1.exe

One of my favourite defragmentation tools MyDefrag (formerly known as JKDefrag) has long ago stopped development and recently the website has also gone down. However the software is still fully functional and as useful as the day the bread was baked.

MyDefrag was maintained for years as an excellent, free defragger. It’s essentially feature complete and just kept on ticking, but after a problem with the mydefrag.com hosting Jereon decided to let the site close.

In his final post on the former official MyDefrag forum he said he hopes that free software download sites keep serving it up for download – I’ve not found it terribly easy to find so I’m hosting my own mirror here for my own benefit and yours.

Download MyDefrag

MyDefrag Download Verification

You can pop the download URL into VirusTotal.com to do an online-virus scan for some reassurance: https://www.virustotal.com/en/

  • EXE: http://patabugen.co.uk/wp-content/uploads/MyDefrag-v4.3.1.exe
  • ZIP: http://patabugen.co.uk/wp-content/uploads/MyDefrag-v4.3.1.zip

Unfortunately I have no other way to prove that the exe is not the original and untouched download from the original site (I made the zip myself) – since there isn’t a checksum listed and available via archive.org. You’ll just have to take my word for it I’m afraid. I’m hosting a MyDefrag download because I love the software, use it frequently (and recommend it in my guide to cleaning your own computer)

Thanks Jereon!

This feels like an appropriate place to put a sincere thank you to Jereon for his years of time and effort to make MyDefrag! The donate button has died with the website, if you didn’t have a chance to donate before and find a way for people to donate now let me know in the comments so we can keep the money-sharing-love alive.

Tags:
Categories:

WordPress Command Line Script Boilerplate

Recently I had a client who wanted me to import their posts from BlogSpot on to the shiny new self hosted WordPress I’d built for them.

The official importer is OK, but it didn’t do everything we wanted. Specifically not all images had featured images, and those which did often had a copy of the image in the body as well as the featured image. What I needed was a script to go through all the posts, find an image, sideload it so it’s no longer hosted on Blog Spot and set it as the featured image then remove any other images from the post using a Composer package.

I’m not going to share all those tasks here, but below you’ll find a boilerplate script which should give you everything you need to run WordPress admin operations on a selection of posts. Getting access to the Admin functions is a little more complicated than not since it includes all the plugins/admin side things and their baggage.

This is a CLI script designed to be run from the command line, but you could probably adapt it to run through Apache if you had reason to do so.

If you saved this as ‘update_everything.php’ in a ‘scripts’ folder which lived next to your ‘public’ folder (which contains WordPress) you’d run `php scripts/update_everything.php` to use. No output from the script indicates success (unless you modify it to output something).

<?php
// DO NOT RUN THIS SCRIPT without a modifying it, backing up and testing it first.

// This script assumes it's saved in a folder called `script`s and you have
// the following directory layout:

// Soil plugin breaks on CLI, so hack these into place for it. You don't need
// these if you only have well written plugins installed.
$_SERVER['SERVER_NAME'] = '';
$_SERVER['SERVER_PORT'] = '80';

// Give us all the errors!
if (!defined('WP_DEBUG')) {
 define('WP_DEBUG', true);
}
// Include these so we can access all the wordpress bits.
// Change this path to point to wp-load.php, after that we can use ABSPATH
include_once __DIR__.'/../public/wp-load.php';

// These specific includes may vary depending on which bits of admin you need
// you could load admin.php to load everything, but then you'll need to work
// around every not-well-written plugin (see below).
// Look up the function you want in the WordPress docs and it will tell you which
// file it's located in - add that file here. If you then get a "fu
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');

// This SEO Recdirect plugin doesn't play well when you're updating but not
// including every field. You may need to add/remove from these remove_action
// calls or disable other bits of plugin if they're causing trouble.
remove_action( 'save_post', 'seo_redirect_save_current_slug' );
remove_action( 'save_post', 'seo_redirect_save_postdata' );

// Find the post types with a regular query, I wanted everything.
$query = new WP_Query([
	'post_type' => 'post',
	'nopaging' => true,
]);

// This sample script will replace all post content with "Hello, World!";
while ($query->have_posts()) {
	// We are now in the famous WordPress loop and can use functions like get_the_ID() to refer
	// to the post in the current iteration
	$query->the_post();

	// Get the content
	$the_content = get_the_content();

	// Do something with it - you want to change this bit, obviously
	$the_content = "Hello, World!";

	// Put together an array with the post details for updating
	$newPost = [
		// Include ID to tell WP to update this post
		'ID' => get_the_ID(),
		// Set any other fields we want to change
		'post_content' => $the_content,
		// One of the plugins I had installed got confused if I didn't include
		// this in the update too
		'post_type' => 'post',
	];

	// Perform the update
	$error = wp_update_post( $newPost, true );

	// Check if we got an error
	if (is_wp_error($error)) {
		// Output any errors to the console
		print_r($error);
	}
}
// Output a new line to clear the console in case anything was output above
echo "\n";

Let me know if this boilerplate was useful and what you’ve done with it in the comments below. Any tips for improving it are also welcome!

Tags:
Category:

HTML Basics

This post intends to be a very basic introduction to HTML for people who may need to edit some code but don’t need or want to learn the everything required to build a website. If you have a WordPress site and want to make some tweaks in HTML, this is the place for you.

Terminology

Websites are built with three computer languages: HTML, CSS and JavaScript. Many other languages exist for outputting these but they all run on the server instead of in your browser and are beyond the scope of this post.

HTML is used to define different bits and content of a web page. CSS then defines how the web browser should make the HTML look (colours, layouts, fonts, sizes etc). Many websites use JavaScript to make things more interactive too.

  • HTML stands for Hyper Text Markup Language
  • CSS stands for Cascading Style Sheets
  • JavaScript is not the same as Java.

Anatomy of an HTML Tag

HTML tags are bits of code which wrap the text of the web page, they have different meanings and the CSS causes them to be displayed in different ways. HTML tags look like one of these. Pay attention to spaces and quotation marks.

  1. Self Closing: <tag_name>
  2. Simple: <tag_name>Contents<value>
  3. Regular with Attributes: <tagname attributes=”value”>Contents</tag>

Self Closing HTML Tag

Many tags don’t have any contents, they just tell the browser to do something. A classic example is a line break, if you have a paragraph (see below) and you want to force a new line somehere stick in the br tag:

 <p>The contents of the paragraph goes here<br> this text will be on a new line</p>

Simple HTML Tag

A simple HTML tag is the P tag, it defines a paragraph and you wrap it around the contents.

 <p>The contents of the paragraph goes here</p>

HTML Tag with Attributes

Another tag is the one which creates links, they’re called anchors and the tag name is a, to define where the link should point you must set the “href” attribute. Do this by adding a space after the tag name, putting the attribute name (href) followed by a space and the value for that attribute. You normally want to surround the attribute value with quotes – either single ” or double ‘ are fine.

 <a href="http://www.google.com">Click here to visit Google</a>

Simple HTML tag with CSS Class

CSS works by applying styles to HTML tags which match various types of attribute. A common one is the class. If your web developer has written code to make some links (Anchors) look like buttons you probably just need to add the right class. For example:

 <a class="button" href="http://www.google.com">Click this button to visit Google</a>

Note that you (or someone) will need to have written some CSS to make that class=”button” do anything, check with your designer/developer to find out what your options are for styling things with classes.

Tags:
Categories:

Strip Images from All WordPress Posts

After importing a blog from Blogger to self-hosted WordPress recently I discovered that all the inline images remain hosted on Blogger.

There were various hacks for moving them to my local server and updating the posts and suchlike – but in my case all the posts had a single image, and the importer had managed to set that image as the featured image. So all I really wanted to do was to strip the image and leave my theme to display the featured image.

I’ve got a scripts folder in my project root for just such occasions, here’s the script I used to remove all images from all WordPress posts:

<?php
$wordpressBasePath = __DIR__.'/../public/wp-load.php';
include_once $wordpressBasePath;

$query = new WP_Query([
	'post_type' => 'post',
	'nopaging' => true,
]);

// I figured I'd also use this opportunity to make sure the post content adheres to the new blog restrictions too
// plus kses is a handy way to strip the image tags.
$allowed_tags = wp_kses_allowed_html( 'post' );
unset($allowed_tags['img']);

while ($query->have_posts()) {
	$query->the_post();
	// Everyone loves an update, especially big websites
	echo "\r".($query->current_post + 1).' of '.$query->found_posts.'. '.round((($query->current_post + 1) / $query->found_posts) * 100).'%';
	$newPost = [
		'ID' => get_the_ID(),
		'post_content' => wp_kses(get_the_content(), $allowed_tags),
	];
	$error = wp_update_post($newPost, true);
	if (is_wp_error($error)) {
		print_r($errors);
	}
}
echo "\n";

I’d like to also make this remove any empty anchor tags which get left behind (most of the images were wrapped in one) but without removing other anchors. If you have a reliable method for doing this, let me know in the comments!

Tags:
Category:

Writing Good Documentation

Writing documentation is tough, knowing what your target audience will need to be told without writing so much they won’t bother reading it isn’t easy.

Documentation writing is much like teaching  – you need a level of patience and empathy.

Patience to spend the time to understand the needs of your learners (whether they’re 11 year olds learning to code, developers learning to use your software or the end user of your product) and to spend the time and effort writing it, checking it and updating it.

Empathy to be able to put yourselves in their shoes and understand what they want and need, and how they would find it best to consume it.

Types of Documentation

Documentation tends to take two forms:

  • Inline documentation – such as labels on forms or those little question marks you can click for more details or comments in code.
  • External documentation – where you have to know where to go to look it up. Ideally the inline documentation is brief and links to a more detailed explanation in the external documentation.

Bad Documentation

A common mistake new developers make when writing code is to write comments (because they’ve been told they must write lots) which just say what the code already says. For example:

// Open the file
$file = open_file("some_file")

The same is often done in user interfaces, this classic example from the otherwise excellent WooCommerces Bookings extension shows the developers writing the easy documentation, but leaving the more complicated bits until later – and eventually shipping without it.

Here they’ve added an explanation to the already intuitive boolean field labelled “Bookable” but made no effort to exaplin the could-be-anything field “First block starts at…”

write-good-documentation

Good Documentation

Good documentation:

  • is consistent – whatever you’re looking up is in the same format as other similar things
  • has examples – an example is the equivalent of a picture in the expression “A picture is worth 1,000 words”
  • is open to contributions – If I find something lacking in some documentation I’m still going to figure out how to solve the problem, to allow me to feed that back into the documentation for future users is invaluable.
  • has a “see also” or “related” section, so if the bit you’re reading about isn’t quite right you can explore to the correct place.

I think that Stripe, Apache and PHP all have excellent external documentations. PHP’s default php.ini is a good example of inline documentation.

If you have more opinions, or wish to praise or insult documentations you can comment below!

Tags:
Category:

Firefox devtools.responsiveUI.presets Settings for Bootstrap 3

The ResponsiveUI tool in Firefox makes testing CSS Media Queries super easy, it comes with some presets which I’m sure mean something to somebody but since I work mostly in Bootstrap I wanted to have some easy to access Bootstrap presets in there.

Setting devtools.responsiveUI.presets

  1. In a new window or tab type about:config into the address bar and hit Enter.
  2. Heed the warning about dragons
  3. In the Search box type “responsiveUI”
  4. Double click on the preference named devtools.responsiveUI.presets and then copy & paste the code below into it.
  5. Disable Responsive UI View, refresh the page, then re-enable the responsive view for the settings to take effect.

devtools.responsiveUI.presets Bootstrap Settings

Double click to select all:

[{"key": "bootstrap-xs-max","name": "Bootstrap: Extra Small Max (xs)","width": 767,"height": 1000},{"key": "bootstrap-sm-min","name": "Bootstrap: Small Min (sm)","width": 768,"height": 1000},{"key": "bootstrap-sm-max","name": "Bootstrap: Small Max (sm)","width": 991,"height": 1000},{"key": "bootstrap-md-min","name": "Bootstrap: Medium Min (md)","width": 992,"height": 1000},{"key": "bootstrap-md-max","name": "Bootstrap: Medium Max (md)","width": 1199,"height": 1000},{"key": "bootstrap-lg-min","name": "Bootstrap: Large Min (lg)","width": 1200,"height": 1000}]

Using the Bootstrap Settings

There’s a setting for the smallest and largest Bootstrap Grid breakpoints, and each one between has two – a Min and a Max – to show you the two extremes for each size band.

More devtools.responsiveUI.presets Options

Tags:
Categories:

Full Width Inline x-editable Elements

I’ve been using x-editable inline components for a project and wanted them rather than being the default tiny width they normally are, to take the full wonderful responsive width of their Bootstrap parents.

The CSS

/* Make inline editables take the full width of their parents */
.editable-container.editable-inline,
.editable-container.editable-inline .control-group.form-group,
.editable-container.editable-inline .control-group.form-group .editable-input,
.editable-container.editable-inline .control-group.form-group .editable-input textarea,
.editable-container.editable-inline .control-group.form-group .editable-input select,
.editable-container.editable-inline .control-group.form-group .editable-input input:not([type=radio]):not([type=checkbox]):not([type=submit])
{
    width: 100%;
}

This just gives 100% width to most of the containers between the editable-container and the element itself, it also excludes radio and checkbox inputs since they don’t usually want their widths playing with.

Don’t forget to include this after your xeditable CSS files.

Before x-editable full-width inline CSS

Before applying the full-width CSS

After x-editable full-width inline CSS

After applying the full-width CSS

After applying the full-width CSS

Nice and tidy, eh?

If I’ve missed anything, or you find something that this breaks – let me know in the comments!

Tags:
Categories:

How To Backup your Data

Last Updated: June 2017

People often don’t backup their computer, usually because they “don’t know how”. So now and then I’ve taken to sharing some advice with my social networks (Anyone on my Facebook who’s not backed probably doesn’t sign in!).

If you want to keep your photos, documents, invoices, scans, videos and all the other stuff you accumulate safe (on either Windows, Mac or Linux), consider yourself informed:

Why Backup?

Sooner or later, your computer will stop working. Often it’s something like the screen or some other part and your data is recoverable, but the Hard Drive is also likely to die. When it does, chances of you getting your photos, documents etc back are slim. Hard Drives usually last anywhere between 1 and 10 years, and will often die without warning.

What is a backup?

A backup is a physically separate second copy of your files, if you only have one copy of a photo it is not backed up – likewise if you have two copies on the same device it’s also not backed up. I recommend you have three copies, because then when one storage device stops working you still have a backup until you can replace it (otherwise you’re left without a backup in the mean time).

Types of Backup

On Site Backups

There are two kinds of backup, On Site and Off Site. Sounds businessy but it’s just as relevant at home. On Site backups are great because they’re fast to backup to and restore from, but if you’ve lost your data to theft or a house fire chances are you’ve lost the on-site backup also.

  • USB Stick – Small and portable, great if you’re travelling.
  • External Hard Drive – You get more space for less money.
  • FreeFileSync is useful for automating these backups.

Off Site Backups

Offsite Backups are somewhere else, probably on the Internet/The Cloud (The Cloud is a marketing term for The Internet, don’t get confused between the two)! Chances are you’ll use one of the many services available for this very purpose, both of those listed below are fully automatic and fast.

Some of these are referral links. My opinion of any service is unaffected by the results of any referral options. – but where available I’ve included them because usually we both get a bonus for using one.

Dropbox.com

Pros:
  • Free for up to 2GB (although you can earn more by inviting friends and other things)
  • Install it on more than one machine and you get an on-site backup too (since it syncs your files)
Cons:
  • Gets quite pricey if you want more than 2GB
  • You have to store your files in your Dropbox folder, which means a slight change to the way you work

SquirrelSave.co.uk

Pros:
  • Fixed price however much data you want (Currently £5.05/m)
  • You tell it which files to backup (rather than needing to move your files)
  • Keeps every copy of everything
  • Encrypted before being uploaded
Cons:
  • You need one account per machine
  • Does not support network drives

Mozy.co.uk

Pros:
  • Very easy to use
  • Also backs up to an external drive
  • Seems to be very reliable
  • Lots of restore options
  • Keeps backups for 60 days
  • Has a business package, so you can use the same at home and work
Cons:
  • Gets quite expensive after 125GB
  • The interface is quite sluggish

OneDrive (and Microsoft Office 365)

Pros:
  • If you’re buying Office 365, you get 1TB of OneDrive storage included
  • Online editing
  • Integrates well with Windows and Office
Cons:
  • If you edit the same file on multiple devices it’s not always 100% clear whether you’re editing the latest version of a file.
  • Microsoft may monitor your files for (potentially) illegal files and may take action (source). Other services might do this too – and just haven’t been caught.

Offsite backups always carry the risk of people snooping on your data, if you’re concerned do some research before uploading anything (but get your On Site backup sorted in the mean time!). Wuala.com prides themselves on being very secure, another popular method is to use Boxcryptor.

What I Do

Here’s what I do:

  • I have everything in Dropbox
  • I adjust my Windows Libraries to include my Dropbox folders, and exclude the default ones.
  • I use Boxcryptor for encrypting sensative details.

Other General Tips

  • I wrote a more complete comparison of Cloud Storage a few years ago. It’s probably getting out of date.
  • When you download photos from your camera, don’t remove them from the camera before they’ve reached one of your backups. I move them to a “downloaded” folder on the camera storage which I remove periodically.
  • If you have a memory card slot in your laptop, that can work as a fast on-site backup.
  • Your initial off-site backup might take while, several days even. Leave your computer on and connected as much as possible since your files won’t be backed up until it’s complete.
  • You should test your backups to check they work as you think they will, restore your files onto a spare computer or borrow a friends. You can always delete them once you know you’ll be able to do it when you need to!

If you have any other advice or suggestions, feel free to leave them in the comments.

Categories: