WordPress Tutorial: How to Create a WordPress Plugin

Step 2 – Isolate Your Plugin From Other Plugins

WordPress Plugin Isolation
WordPress Plugin Isolation

Steps to Create a WordPress Plugin

  1. Name Your WordPress Plugin
  2. Isolate Your Plugin From Other Plugins
  3. Organize Your Plugin Files
  4. Define Directory Paths
  5. Load Plugin Files
  6. Directory Structure
  7. Plugin Activation, Plugin Deactivation, and Plugin Uninstallation
  8. Essential Plugin Files
  9. Security: Protect Plugin Files
  10. Plugin Hierarchy

If you currently aren’t practicing using Namespaces, now’s the time to start. If you’re unaware of what a Namespace is, please educate yourself on PHP Namespaces. It really doesn’t take that long to learn, and it’s totally worth it. FAQ: things you need to know about namespaces

In the past, before Namespaces were relevant, WordPress developers used to prefix: function names, class names, variables, etc. with a unique identifier and/or even a conditional check.

Example of a non-prefixed function name:

Example of a prefixed function name using mbe_ as a prefix:

Example of using a conditional check to avoid conflicts:

Using prefixes and/or conditional checks can become tedious over time. It’s not practical, and can eventually pose a problem at some point.

Namespaces greatly reduce the possibility of potential conflicts. They allow you to skip using conditional checks ~90+% of the time, and there’s no need to: prefix function names, class names or variable names. Namespaces isolate your code from all other code (A/K/A the global scope). This allows you to freely name your stuff whatever you want (within the scope of your namespace).

Define a Namespace

Namespaces are so simple to use. All you need to do is specify one line of code at the top of every file in your plugin. I like to try to keep the naming convention similar to my plugin name.

Define your namespace near the top of your index.php file:

Namespaces are case-sensitive, so make sure you’re consistent with your naming convention throughout your plugin files.

Accessing Namespaced Functionality

You should be able to access any function from the global scope as you always have previously, however sometimes, you need to determine the scope of the desired function.

I know I just got done saying there’s no need for prefixes anymore, that’s not entirely true. You might need to prefix your function calls with a backwards slash and/or include the namespace along with your function call. I like to think of namespaces as a “path”. You will notice I try to organize my namespaces similarly to the way I organize my directory and file structure.

For example, if you wanted to use WP_Query in your namespaced plugin, Previously you would have just done WP_Query( array( '' => '' ) ); but now you would need to do: \WP_Query( array( '' => '' ) ); Notice the backwards slash immediately before the class name? That’s because this class name is a part of the global namespace, which is outside of your namespace.

I haven’t quite figured out when it’s necessary to use a backwards slash and when it isn’t. So far, I’ve come to the conclusion that it’s necessary for class names. There have been some functions I’ve had to do this with as well. wp_head(); and wp_footer(); for example, I’ve had to write like: \wp_head(); and \wp_footer(); while inside a namespace.

Ideally though, most functions you should be able to use just as normal, without the need of a backwards slash prefixing the function call. While $this->statement == true; it’s slightly hypocritical and $this->statement !== true;. (I tried to be punny there with a little code humor.)

Let’s say you’ve created a function called get_settings(); within the namespace of your plugin. Now, when you make a call to get_settings();, PHP will attempt to use the function defined in your plugin’s namespace. That’s fine and dandy, but what if you wanted to use the core WordPress function named get_settings();? You would then need to prefix the function call with a backwards slash, like this: \get_settings(); and if you wanted to use your plugin’s function you would just call it as normal like: get_settings();, since it’s defined within your namespace.

Please just read the documentation PHP Namespaces and FAQ: things you need to know about namespaces. It’s explained much better there.

Leave a Reply