Let’s put your name skills to use. In this episode, you will convert the supplied plugin from prefixing to PHP namespacing. The best way to learn this is to start where you are, i.e. in prefixing, and walk through step-by-step the process of making a plugin namespaced. You’ll change callbacks, use the magic constant __NAMESPACE__. You’ll remove the namespacing. Step-by-step. Let’s do this together.
Class structures use namespacing too, just like functions do. Within its namespace, PHP knows how to find the methods and properties of a class blueprint. When you use the class outside of the namespace, you need to make PHP know which class you mean. That means you can either: You can specify its fully qualified name such as \KnowTheCode\InsOutsPHPNamespacing\Sandbox\Post If the namespace is the root, then you can use a relative name such as $post = new Sandbox\Post(); You can import the class using the PHP keyword use. You can import and then alias the class. You can learn more […]
As you build your PHP expertise, you will likely grow into other frameworks and applications outside of WordPress. Laravel and Symfony are two awesome frameworks. Many times, you can pull in the modules and packages from their libraries even into a WordPress application. Yes, that’s how well these frameworks are built. Let’s take a quick look at the namespacing in each of these frameworks to show you how it’s used in the wild. You can also take a look at code construction and architecture too, i.e. to further your software building skills.
To help you master the concept of “fully qualified naming,” let’s take a look at what each function’s name is. You will use the magic constant __FUNCTION__. It returns the fully qualified name of the function, which includes the namespace if one exists.
You can use functions from different namespaces. But you have to be aware of how to bring in that function. Remember, the function has a fully qualified name which includes its namespace. When using that function in another namespace, you have choices: You can specify its fully qualified name such as \KnowTheCode\InsOutsPHPNamespacing\Sandbox\get_post_id() If the namespace is the root, then you can use a relative name such as Sandbox\get_post_id() You can alias the namespace and then use that alias in place of the fully qualified namespace If you are using PHP 5.6 and up, then you can import the function. You […]
The best to learn about namespace and to wrap your mind around how it’s different than prefix is to use it and see it in action. In this episode, you will add a function called get_the_ID() to your plugin. Wait a minute. That function exists in WordPress Core. Aha, you’ll see what happens when you use a namespace around the plugin’s function. You’ll see how to call WordPress Core’s function versus the one in your plugin.
What is PHP namespace? Why should you care about using it versus prefixing? What does it give you? What are the advantages? Let’s talk about it. The first thing to know is: the web server must be running at least PHP 5.3.0. This is an old version of PHP. It stopped being supported as of August 2014. It’s old. Shoot, 5.4 and 5.5 are old too and no longer supported. Check out the supported versions here on the PHP manual website. PHP namespace encapsulates your code. What does that mean? It means you are putting a container around your code. […]
In this episode, you’ll learn what you will be doing in this lab. We’ll also make sure that your sandbox is ready to go. In order to do this lab, you will need a local sandbox site spun up and ready to go. You will need to install the lab’s starter plugin from GitHub, as you will be u
In this hands-on lab, you and I will walk through the entire process of adding a new feature to an existing WordPress plugin. You’ll start with the Collapsible Content plugin and then add a FAQ feature to it. This feature will require you to plan, think, and execute building a custom post type, custom taxonomy, advanced SQL queries, custom archive, and shortcode.
Let’s discuss the plugin architecture and build the folders and main files. We’ll think about the intent of the structure and each component. We’ll talk about why you want to build your code in modular format that complies to Single Responsibility Principle. Let’s build our file structure.