In WordPress, we register callbacks to events by using add_action or add_filter. In the last episode, you learned that you must specify the fully qualified name to invoke a function (i.e. run it) outside of the namespace. This applies to add_action or add_filter. In this episode, you will learn why as well as a shorthand version using the magic constant __NAMESPACE__.
Labs
Labs are hands-on coding projects that you build along with Tonya as she explains the code, concepts, and thought processes behind it. You can use the labs to further your code knowledge or to use right in your projects. Each lab ties into the Docx to ensure you have the information you need.
Each lab is designed to further your understanding and mastery of code. You learn more about how to think about its construction, quality, maintainability, programmatic and logical thought, and problem-solving. While you may be building a specific thing, Tonya presents the why of it to make it adaptable far beyond that specific implementation, thereby giving you the means to make it your own, in any context.
PHP String Building and Processing Basics
As developers, we need to build and process dynamic strings. Our code pulls in information from various sources, assembles it, and then renders it out to the browser. PHP provides a powerful function library to help us. In this hands-on lab, you will learn about the basics of PHP string building and processing.
WordPress Built-in User Metadata Basics
Out-of-the-box, WordPress gives us built-in user metadata, which captures details about each user. We use this metadata to personalize the user’s experience as well as for processing and filtering tasks, such as checking access rights, capabilities, and more. In this hands-on lab, you will learn the basics of WordPress user metadata, the database, and the profile interface in the back-end.
WordPress Term Metadata Basics
Using term metadata, we can add additional information to terms. We use this information for specific filtering, querying, grouping, and processing tasks. In the back end, we build metaboxes provide an interface for authors to input the details for the term metadata. In this hands-on lab, you will learn the basics of WordPress term metadata, metaboxes, and custom fields you build for their interface.
Wrap it Up
Whew, you did it! Congratulations for completing this lab! Let’s review what you learned as well as show you the code here on Know the Code. Remember, namespacing is only available in PHP version 5.3.0 and up. That shouldn’t be a problem for you. Why? Because 5.3 stopped being supported back in 2014. It’s old. Anything older than it can be problematic. Therefore, build your code to be at least 5.3 and up. You can check the PHP supported versions by clicking here.
Architecture – Building in Packages and Modules
In this episode, let’s stop and talk about how you architect your code. Code should be built in modules. These modules are not assembled into your plugin or theme. It’s not snippets of code. Rather, they are complete, fully-tested, ready-to-go component or feature modules. All you have to do is change the configuration and none of the codebase. That makes it a reusable module. For example, let’s say you are building a theme. In your code libraries, you have modules for: WooCommerce bbPress LearnDash MemberPress BuddyPress Customizer and more The theme you are building needs WooCommerce, bbPress, and Customizer. You […]
Real World Example: Convert from Prefixing
Next, let’s take a real world plugin and convert it from prefixing to namespacing. Let’s convert Tom McFarlin’s Easier Excerpts plugin. You can install this plugin within the WordPress back end by going to Plugins > Add New and then type in Easier Excerpts. In this episode, you will walk through the steps to remove the prefixing, add in the namespacing, and provide better function naming that starts with a verb.
Name Resolution Rules
Next, let’s talk about PHP name resolution rules. That means: how PHP figures out what code to run for the given name. If you look at the PHP Manual, they give you a techie write-up that likely will confuse you. Essentially, here’s what you need to know: How you declare the function is the starting point. If you include the fully qualified name, you are telling PHP exactly which function you want; whereas, if you leave off the namespace, then it walks through its process ordering to figure out which function to run. You can name using: Unqualified name: no […]
Practical Example: Convert from Prefixing
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 Basics
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 […]