19
- August
2018
Posted By : Code Buddy
Comments Off on 10 Awesome Tips for Coding with WordPress
10 Awesome Tips for Coding with WordPress

WordPress is the world’s most popular software platform. More websites are created on on the platform than almost all other systems combined. One of the most useful features of WordPress is the simplicity of adding your own functionality to your site with PHP and JavaScript.

If you can imagine it, there is probably a plugin that can do it. If there isn’t a plugin for it, why not create your own? Here are some awesome WordPress tips every coder must know.

Have a Staging Server

No matter what kind of development you’re doing, you should use a staging server. A staging server is just a server that you use to test what you’re doing before it goes live. You can set “Discourage Search Engines” in the admin settings, and sites like Google will ignore your staging server.

Plugins vs Themes

There is one major technical difference between plugins and themes in WordPress. In any single page load request, there can only be one theme, but there can be many plugins.

Themes also contain the “template” views for things like the header and footer. The reason for this is because you want the site to have a certain “look”.

In general, most themes display a single header across every page in the site. This is by design, you want the visitor to know they are, in fact, on your site.

Plugins can be run with any theme. So in WordPress, themes control what a site looks like. Plugins control what a site does.

When you create new code, put functionality in a plugin. Put design, and design controls, in a theme.

Backup Your Site

It is amazing how high a percentage of WordPress sites aren’t backed up! Don’t be a statistic, use a backup plugin.

Enqueue Your JavaScript

JavaScript files need to be localized and enqueued properly. There are, in fact, several ways of doing this. “Localized” means two things, passing strings through the i18n functions, and passing variables to the JS file.

Enqueueing your script tells WordPress that your particular script needs to be loaded with the current page load. WordPress takes cares of the actual linking to the browser.

When you enqueue your scripts properly, you will generally speed up the delivery of the site.

i18n

i18n functions, or internationalization and localization functions, are used to translate strings. If your code says “Hello World!” in English, it will output “Hola Mundo” in Spanish, to a person viewing your site on a Spanish language browser.

It is important that you wrap all your strings in the __() and _e() functions. Even if you don’t put translations of all your strings in now, someone else might add translations in another language sometime in the future. By properly encoding your strings in the i18n functions, you leave open the possibility of someone translating them later on.

Very Little Code Goes in the “functions.php” File

It is a sign of an inexperienced coder that you are putting code in the functions.php file. The only thing that belongs there, is functionality specifically related to the theme.

In other words, would this functionality be inherently worthless if the admin wanted to change themes? Probably not! Usually, there would actually be some worth to your code, even if the user wanted to change themes. If this is true, it belongs in a plugin!

If your code is put in a plugin, the admin can change themes at will, and your code will still be there.

Namespace Your Code

WordPress is loaded in the “global” namespace. That means that your code will be in the same memory space as all the other plugins and themes that the admin has activated. Who knows how many that is, or what those plugins and themes do?

There are tens of thousands of plugins in the .org repository and millions more in the wild. If you have a function or class called “email”, or “first_name”, you’re looking for trouble! This will create conflicts and break sites.

Luckily, you can use PHP namespacing to prefix your code. Your namespace should be the same as your plugin or theme’s WordPress slug.

Test Your Code

As a coder, you should begin to learn Test Driven Development and Behavior Driven Development. You should adopt a test first strategy when you write code.

Testing first creates all kinds of benefits. You get a “single source of truth” and “highly documented code”. The tests become the documentation.

WordPress code uses PHPUnit. Thier tests are designed for core, and won’t really help your custom plugin or theme code. You should create your own tests.

An excellent library from Codeception uses all the tools form the PHPUnit toolkit as well as having a custom WordPress Selenium driver.

Use The Codex

The Codex contains a description with examples of nearly every WordPress function and situation. When you encounter a problem, type your problem into your favorite search engine. If the Codex is included in any of the top search results, that is a good place to start.

Use Git

In 2005, Linus Torvalds was just sitting around. He had already created Linux. Linux is the operating system that most WordPress sites run on, as well as the basis of the Android OS and many other platforms.

Having nothing else to do, he created Git, which has since become the worlds most popular versioning control system.

WordPress core and the .org repos use SubVersion, which is like Git from the stone age. You should use Git during development, and only use svn to publish to the .org repos.

Github.com is a popular site owned by Microsoft. Many programmers use Github because it is free. Github.com and “git” the version control system, are two separate things.

Nothing wrong with using github.com, just be aware you can also create your own git servers for free on any machine.

Get more WordPress Tips!

WordPress is simple to use. But it becomes super complex once you open it up under the hood. There are endless possibilities and options, and your imagination is the only limit.

For more website and WordPress tips, check out our blog.