The Zen of WP Development is being one with the code, and creating compelling web experiences with proper uses of the core API. It involves:
- Focusing on the question at hand, and ignoring distractions.
- Distilling complex situations into simpler parts.
- Pulling from deep knowledge of the codebase to understand how APIs interact.
- Striving for elegant solutions always.
Today, I was invited to present my perspective at WordCamp San Francisco. These are my slides. My notes are posted below them.
Be intimately aware of your environment
Know thy codebase
Key to being able to manipulate your environment is knowing the rules by which the environment operates.
“Why is the sky blue?” you might ask. “Well, defraction. The atoms in the sky scatter blue light more than any other.”
Similarly, you might ask, “why is my custom URI redirecting to a page when I’ve explicitly registered the rewrite rule?” and discover a canonical redirect conflict you need to disable.
If you don’t already, you’ll eventually know core inside and out. Here are a few places you can get started:
- wp-includes/formatting.php – Some of your functions for sanitizing input and formatting output, including esc_*(), sanitize_*(), and capital_P_dangit()
- wp-includes/post.php – Post manipulation, including register_post_type(), get_posts(), etc.
- wp-includes/rewrite.php – See how WordPress translates request URIs
- wp-includes/query.php – Reference for modifying the Query
- wp-includes/pluggable.php – Functions you can override in your theme or plugin
Master the tools of the trade
Tools are another key way to gather situational knowledge. Gather more information to make a decision.
The Developer plugin was released to help bring more consistency across developer environments. VIP works with dozens of developers every day. We want to make sure they’re writing clean code as effectively as they can. In addition to installing suggested plugins, it does some basic environment checking (e.g. is WP_DEBUG set to true). The plugin is on Github and we welcome your pull requests.
Some of the plugins it installs are:
- Debug Bar Cron – Inspect cron entries
- User Switching – Easily switch between users to test roles and capabilities
- Rewrite Rule Inspector – See how a request URI is interpreted by WordPress
Best practices are healthy ingredients for your code
Prefix all the things! Avoid fatal collisions in global namespace. It doesn’t happen often, but it never needs to happen.
Also, don’t declare functions within functions. If the parent function is called twice, you’ll see a fatal error when the second function.
Commit messages are one of your greatest opportunities to make your mark in history. When you are gone, your commit messages will live on.
Write them wisely. Recounting what you’ve changed is a good sanity check. More important is to explain why you’re making the change.
Why does the duplicate code exist in the common plugins directory? Is it there mistakenly, or is code being migrated? What was the background context for its being?
When writing a commit message, you’re writing for two audiences:
- Your colleagues, so they can learn from the personal growth you experienced in preparing your changeset
- Your future self, so you can put yourself back in your shoes and remember why you made the change you did
It’s also tremendously helpful to link to context if there is any, Trac ticket or otherwise.
Focus on choosing the proper hooks and APIs
Run through the slides in this particular section as they describe it plenty.
Code is a conversation.
Read others’ code;
have them read yours.