Category: WordPress

  • WordPress and Composer TLDR

    There’s a few questions I’m commonly asked about using composer + WordPress. Here they are in a super short form FAQ: To install a composer project, run: composer install To update a composer project, run: composer update Install WordPress in a folder composer require johnpbloch/wordpress @stable or in your composer.json add: { “require” : { “johnpbloch/wordpress”: “@stable”…

  • Root Composition in WordPress Plugins

    When I build a WordPress plugin, I use several methods to structure things that help me greatly, all of which stem from root composition. I’m going to expand on what that is and how I use it. Every plugin has a root file that contains the plugin header comment, and gets loaded by WordPress. This…

  • Escaping the Unsecure

    Part of my job at WordPress VIP is review. It takes up a lot of my time, and it’s critical work. Before code reaches our servers it needs to be vetted and peer reviewed to vouch that it’s fast, safe, and secure. Every developer makes mistakes ( even VIP developers ), so we peer review…

  • ___ without a plugin

    How do I do ___ without a plugin? The answer is to remove the comment at the top of the plugin, and it’s no longer a plugin.

  • Automatticians For The People

    We felt there was a major deficiency in the worldwide supply of Automatticians, and came up with a solution.

  • Admin Colour Schemes

    I’ve built two Admin Colour schemes for WordPress. Both are based initially off of the themergency theme generator, but with extensive changes. Notably the split colours of the admin menu and the admin bar. I’ve also accounted for form highlights, selection, checkboxes, and the progress bar on videos. The first is called Tangerine: The second…

  • Theme Composition vs Frameworks

    For a long time, theme frameworks have been the primary alternative to building things from scratch in WordPress, and to a lesser extent, starter themes. There is a better way. Starter Themes & Frameworks Starter themes work on the paradigm of adding and replacing. You’re given a theme that represents the ‘halfway’ mark and add…

  • WordPress Bash Magic

    I’ve been doing a lot of bash scripting with WordPress, especially with WP CLI. Here are some snippets I found useful. All my scripts assume a standard wp-config.php with DEFINE() statements and no unusual logic structures. A rogue constant with the name DB_NAME may throw things off without some modification. Getting the Database table prefix If you…

  • Calling Constructors

    Take a look at this code, found inside the Jetpack Post Views plugin: function Jetpack_Post_Views() { $this->__construct(); } Here an object is calling its own constructor. But what’s wrong with it? The General rule for calling __construct: The only occasion it is okay to call a constructor method directly, is in a child class that…

  • Use of mysql_connect in WordPress

  • &$this

    Take a look at this code, found inside a PHP class or object: add_action( &$this, ‘functionname’ ); This looks familiar, and can be found in many themes and plugins. But what’s wrong with it? It’s PHP4 code in a PHP5 world. General PHP rule for add_action: When creating a callable, never use &$this, use $this instead…

  • Code For The People

    I’m now working for Code For The People! As a WordPress developer, I’ll be working on a lot of new projects as I did at Interconnect/IT, but without an office to visit everyday. I’ll be swapping The Spectator and University of Nottingham Blogs, for Stephen Fry and The Rolling Stones. This does mean that Interconnect/IT are…

  • ComposerPress

    Curious about Composer but already have a WordPress site? Not to worry, I have you covered. Presenting Composerpress: Composerpress is a plugin that analyses your WordPress install, and generates a composer.json file you can then use retro-actively. This file acts as an inventory of dependencies, plugins, etc that are needed to set up a site, and…

  • add_action considered harmful

    I’m going to argue that add_action is actually a more elaborate version of goto hidden in  plain sight, an anti-pattern, that it encourages unstructured programming, and obscures dependencies between code by providing the illusion of structured code, and encouraging spaghetti architecture. The Dangers of Goto Firstly what is a goto statement? goto (GOTO, GO TO or other case combinations,…

  • Creating Objects on Hooks/Actions

    Lets say we had a class that set up some important things. These things needed to be added on the admin_init hook, and they were done in our classes constructor. How would we create our object so they get set up on the admin_init hook? View the code on Gist. Some people may try to…

  • WP_Query::get_posts()

    Every call to retrieve posts goes through WP_Query, and the bulk of the logic is in the method get_posts. Here’s some potentially useful information about the current state of what is arguably the most important function in WordPress. WP_Query::get_posts is 890 lines long, and has a cyclomatic complexity of 256. A good cyclomatic complexity is ~2-6,…

  • Alternative Post Loops

    We all know the standard post loop in every template, but what if we did something slightly different? Lets begin with the standard main loop we all know: View the code on Gist. There’s a lot of boilerplate there, what if we could reduce it down? Lets try using a feature of PHP 5 called…

  • Content Without Posts

    I’m going to demonstrate how to have a URL with content in WordPress that’s neither a post, page, or listing. Its contents can be anything you’d like! To do this we need to take the following steps: Add a custom query variable Add a rewrite rule that uses the new query variable On the template_redirect…

  • WordCamp Lancaster 2013

    I’m at WordCamp Lancaster, and I’ve given two talks. Here are my slides, and a selection of photos taken! A big shout out to: Chris Witham for lending me a VGA adaptor Jenny Wong for organising the massive Pizza order Marko Heijnen for another VGA adaptor Code Deodorant How to determine code quality by scientific…

  • Getting From URL to Template

    So you load a page on a WordPress site. How did it know to load the page template? How did it know it was a page and not a category listing? Is it hardcoded somewhere? To begin with lets list out what happens, then we’ll explain each step in basic terms: .htaccess rules pass the URL…