THE PENDING DRAFT

Improving Code Quality

July 9, 2015

If you’re building things with WordPress, it’s important to deliver quality code. Especially if it’s going to be released to the public or used by a client. There’s a good post on the WPMUDEV Blog covering many aspects from HTML/CSS, JavaScript or PHP to the WordPress Coding Standards or Accessibility.

It’s a great starting point if you are unsure how to improve your code but also a good reminder for experienced developers.

Stop Cowboy Coding: 10 Tips for Improving the Quality of Your WordPress Themes and Plugins

WordPress Plugin – Airplane Mode

June 27, 2015

WordPress makes a whole bunch of connections when you are using the Dashboard. For example to fetch external files like fonts or to display the latest news and so forth. While this is convenient in most situations, it can completely slow you down or lead to error messages when you are on a slow/unstable connection or don’t have a connection at all, as we had this week. The solution to this is called Airplane Mode, a WordPress Plugin by Andrew Norcross which disables all external calls from WordPress.

Control loading of external files when developing locally. WP loads certain external files (fonts, Gravatar, etc.) and makes external HTTP calls. This isn’t usually an issue, unless you’re working in an evironment without a web connection. This plugin removes/unhooks those actions to reduce load time and avoid errors due to missing files.

GitHub – Airplane Mode

Sanitize Text Field with Multiple Lines in WordPress

June 26, 2015

I recently ran into a problem where we had a text field in a metabox, that was meant to be able to hold multiple paragraphs of text, but still be properly sanitized.

The text sanitization (sanitize_text_field) removes all tags, including line-breaks, so i had to find a workaround. This is the best solution i found:

$pdr_multiline_text = implode( "\n", array_map( 'sanitize_text_field', explode( "\n", $_POST['pdr_text_field'] ) ) );

Basically, what it does is breaking up the given string into smaller chunks on line-breaks (Implode on "\n"), then sanitize those smaller strings using sanizite_text_field() and then put them back together (explode on "\n").

Stackoverflow – How to sanitize multi-line text from a textarea without losing line breaks?

Xdebug with WordPress

June 24, 2015

Proper debugging and unit testing are both very high on my list of things i need to get my head around and implement into my development workflow as soon as possible. Luckily, there are people like Jonathan Christopher who wrote this great introduction on how to use Xdebug for WordPress Development.

He goes into great detail on how to install and configure Xdebug with MAMP (Pro) and use it with either a separate application or right inside an IDE.

Monday by Noon – Improving Your Process: WordPress Development Using Xdebug

Pippin Williamson on Backwards Compatibility

May 8, 2015

Pippin Williamson with a great presentation at LoopConf about Backwards Compatibility and why it’s such an important thing to think about when you develop plugins. He covers many examples of what could happen if you break things and how to avoid those issues.

Backwards compatibility is a cornerstone of WordPress core development philosophy. It is, unfortunately, not something nearly enough plugin or theme developers take seriously. When a plugin or theme project gains 10s or 100s of thousands of users, backwards compatibility can be crucial to the overall health of the project.

Ensuring backwards compatibility is more of a mental mindset than anything. Developers have to mentally make the commitment and say to themselves “I will NOT break installs during upgrades”.

As we are building our own plugin at the moment, Backwards Compatibility is something i think about a lot lately, because as soon as we have it released, some things will be impossible or much harder to change so we better try to get it right the first time.

A Commitment to Backwards Compatibility

Rewriting URLs in WordPress

May 5, 2015

The Rewrite Engine of WordPress is one of the parts of WordPress that can be confusing at first and that i personally never liked to customize at the beginning. Basically just because it normally worked the way it should and i didn’t want to break anything by adding custom rules. But custom rewrite rules can be a very powerful thing, especially if you’re building custom solutions for clients or a web app on top of WordPress. This post covers the basics and various code examples of how to implement your custom rewrite logic in WordPress.

Rewriting URLs in WordPress

Yoda Conditions

April 25, 2015

Have you ever heard of Yoda Conditions? Maybe you saw some code written like this:

if ( true == $the_force ) {
    $victorious = you_will( $be );
}

I never really got why it could be useful to write a condition in that order until i found this part of the WordPress’s PHP Coding Standards:

In the above example, if you omit an equals sign (admit it, it happens even to the most seasoned of us), you’ll get a parse error, because you can’t assign to a constant like true. If the statement were the other way around ( $the_force = true ), the assignment would be perfectly valid, returning 1, causing the if statement to evaluate to true, and you could be chasing that bug for a while.

Now that makes a whole lot of sense, so make sure to write your conditions in that order from now on, even if it sounds strange at first. Or, as it is appropriately stated in the Coding Standards:

A little bizarre, it is, to read. Get used to it, you will.

PHP Coding Standards – Yoda Conditions