Adding Semantic Reviews / Rich Snippets to your WordPress Site


Screenshot of JSON code in a web page.

This is a real "scratch my own itch" post. I want to add Schema.org semantic metadata to the book reviews I write on my blog. This will enable "rich snippets" in search engines. There are loads of WordPress plugins which do this. But where's the fun in that?! So here's how I quickly built it into my open source blog theme. Screen options First, let's add some screen options to the WordPress editor screen. This is what it will look like when done: This is how to add a custom metabox to…

Continue reading →

Remove the first few lines from a string in PHP


Binary code displayed on a screen.

Problem: I have a multiline string - not a file - and I want to remove the first few lines from it and keep the rest. I found this surprisingly unintuitive. So these are notes in case I want to do it again. Feel free to suggest a better way! Example Given this input: $str = "Once upon\nA Time\nLived a big\nDragon\nCalled Cuthbert."; I want the output to be: "Dragon\nCalled Cuthbert." That is, remove the first X lines in a string. Exploding kittens Using explode()…

Continue reading →

Quick and Dirty Self-Hosted Alexa Skills (2019)


I hate creating Alexa skills. What should be a 3-click process inevitably ends up requiring trips to multiple websites, to set up weird parameters, and reading outdated tutorials for obsolete libraries. So this is how to create a self-hosted Skill, using PHP. It runs on your own server and doesn't require any interaction. The Skill At a basic level, all your website has to do is spit out a piece of JSON for Alexa to read out. // Set the correct header for JSON data header('Content-Type:…

Continue reading →

Import Images From A Migrated WordPress


The Logo for WordPress.

Here's how to solve a common WordPress problem. I want to re-import all my blog's images into the media library. I've moved my blog to a new host - but kept the same domain name. I started with a new WordPress install, the uploads folder still has all my images, but WordPress can't see them. None of the plugins I found worked with huge amounts of images spread across multiple directories. One Line Fix This uses the WP-CLI program. Install it on your server, then run: ./wp-cli.phar media…

Continue reading →

Counting Invisible Strings


The PHP logo.

When is a string not a string? When it's a series of control characters! Not a particularly funny riddle, but one I've been wrestling with recently. Imagine we want to write a program which displays a Twitter user's name. Not their @ handle, but their "real" name. For example, instead of @POTUS, display "President Obama". Easy, right? Not quite. What happens when a user is named "️"? Normally, we'd just say if (null == $name) { ...Do Stuff... } Ah! But that's not an empty string, i…

Continue reading →

A UTF-8 Aware substr_replace (for use in App.net)


The PHP logo.

So, I stayed up bashing my head against a brick wall all last night! PHP's string functions aren't (yet) UTF-8 aware. This is a replacement for subtr_replace which should work on UTF-8 Strings: function utf8_substr_replace($original, $replacement, $position, $length) { $startString = mb_substr($original, 0, $position, "UTF-8"); $endString = mb_substr($original, $position + $length, mb_strlen($original), "UTF-8"); $out = $startString . $replacement . $endString; return $out; …

Continue reading →

Introducing a NEW QR Generator


A QR code.

When people ask me which QR generator to use, I usually suggest Google Charts. However, recently I've become dissatisfied with its limitations, so I've decided to write and release my own QR encoder. I'm still looking for a catchy name for it (suggestions welcomed) - so for now it's called "QR Generator PHP". It's available on GitHub or you can use it directly. So, how does it compare to Google Charts? FeatureNew QR EncoderGoogle Charts Image FormatsPNG, JPG, GIFPNG Maximum Image…

Continue reading →

Calibre PHP Patches


Two quick patches which should be in the next version of Calibre PHP. Adding File Size This shows the sizes of the eBook files. Screenshot shows a demonstration using the free "Hacking The BBC" eBook. (more…) …

Continue reading →

Installing Calibre PHP


(These are mostly notes to myself!) I love Calibre, it's the perfect eBook management tool. It comes with a built in WWW server so you can easily access your library on the go. The only problem is that this really only works if you have a single machine dedicated to Calibre. For various reasons, I don't have a single machine. I have a desktop, laptop, and server. The Calibre Library is just a database with a set of files and folders - so all three machines sync via DropBox. As long as I…

Continue reading →

Displaying Twitter Photos via Entities


Twitter has announced that it will soon open up a native photo sharing service. Rather than using an external service like Embed.ly to retrieve thumbnails, all the data is embedded within Twitter Entities. So, if you request a status using "include_entities=true", you will be able to grab the image and display the thumbnail using the following code. function twitter_get_media($status) { if($status->entities->media) { $url = $status->entities->media[0]->media_url_https; …

Continue reading →

HOWTO: Twitpic and OAuth


Logo of TwitPic.

I am no longer confused! Here is a quick tutorial in how to post images to Twitpic and Twitter when using OAuth. I'm indebted to Steve Corona of Twitpic, for his help with this. You can see the full code on Dabr's Google Code page. First of all, you'll need to have enabled OAuth for your Twitter client. I use Abraham's excellent OAuth libraries for PHP. This tutorial assumes you already have OAuth working. I'll attempt to explain what I'm doing as I go along - but the code should be…

Continue reading →

Twitpic OAuth - I'm Stuck


Logo of TwitPic.

Twitpic has implemented an OAuth API. No more having to hand out passwords to all and sundy. Only I'm too much of a dunderhead to get it working. Perhaps it's a combination of heatstroke or this rotten head-cold, but I just can't see what I'm doing wrong. Any help much appreciated. The easy bit. It's easy to post the data to Twitpic $media_data = array( 'media' => '@'.$_FILES['media']['tmp_name'], 'message' => html_entity_decode($_POST['message']), 'key'=>'123465789132465' );…

Continue reading →