List Drafts Widget – a minor update

I was mildly surprised when I realised that it was all of nine years ago that I unleashed my List Drafts Widget WordPress plugin. It’s had a few minor tweaks since then, but as a simple thing that does a simple job, that’s all it’s needed.

But when I was checking my site for PHP 7 compatibility, I got a warning that it used some old code that was no longer supported.

The problem was in the bit of code that sets up the actual widget. Now this was standard code sating back to how widgets started to work in WordPress 2.8, and I’d copied it from an example when I rebuilt the plugin to work in what was then the approved way, just in case the old way ceased to be supported by WordPress at some point. This is the offending section:

class ListDrafts_Widget extends WP_Widget {

/**
* Widget setup.
*/

function ListDrafts_Widget()
{
/* Widget settings. */
$widget_ops = array( 'classname' => 'example', 'description' => __('A widget which displays the title of draft posts.', 'listdrafts') );

The function is a “constructor” – it creates a software thingy called a class, which does things, and look, you know I’m not an actual coder, right?

Anyway, in newer versions of PHP, this has to be done slightly differently – instead of a function with the same name as the class, you have to use a generic __construct() function instead. Or so I found out with a little googling. Anyway, the fix seemed simple enough – one little edit. So I tried it out, and everything still worked on PHP 5.6, so that was OK. And it still works on PHP 7.

I pushed the update out to the repository, so anyone who has it should be offered the new version.

Here’s the slightly edited version of the above:

class ListDrafts_Widget extends WP_Widget {

/**
* Widget setup.
*/

// Update October 2017- new constructor for PHP 7 compatibility

function __construct()
{
/* Widget settings. */
$widget_ops = array( 'classname' => 'example', 'description' => __('A widget which displays the title of draft posts.', 'listdrafts') );

All good fun, really.

Leave a Reply

Your email address will not be published. Required fields are marked *