Articles
-
Embedding PHP in Content
Would you like to embed and run some PHP code in a blog post? Need to include a PHP file in a sidebar widget? Recently discovered this curious plugin? Don’t, you’re about to make an awful mistake. Lets walk through why it’s bad, and what you should be doing instead. Why Might Somebody Embed PHP? Perhaps you…
-
Scaling the WP Importer
Small scale imports are handy, but when sites get large you can run into problems. Normally people fall back to using SQL dumps, but doing this sidesteps hooks, and filters that could be important. Here are some tips on how to make WordPress imports and exports using WXR files easier. The First Major Problem When loading…
-
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…
-
Gitbooks & GitHub
I started writing a book on gitbook.io recently, and chose to use GitHub to host it. I ran into some stumbling blocks as I started, mainly because things weren’t documented well enough, so I’m going to explain how to begin. Setting Up First, create an empty repository on GitHub to store your book. You’ll then…
-
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…