There will be times when you need to remove a callback from a 3rd party plugin or application. Your project may require a completely different implementation for part of the plugin. This edge case will happen in your career. Okay, then how do you remove an object’s method callback within a 3rd party plugin? Imagine that you have a project that is using WooCommerce and you need to build a completely different implementation of sending out an email when a new customer note occurs. How can you remove the built-in callback so that you can overwrite it with your code? […]
Let’s talk about how to make a static method a callback in PHP. You will need this in order to understand the syntax and how it works when registering a static to a WordPress hook event.
Now that you know how to make an object’s method a callback, let’s apply it to registering a method to a WordPress event. You’ll look in WordPress Core where it is using call_user_func_array(). Therefore, the same syntax rules and approach applies for you register it to a specific event. You’ll see how the object’s method is registered and added to the WordPress Event Registry, which is stored in $wp_filter. You’ll see how the key for it includes the object’s ID plus the method name. You’ll see the array is used as $the_[‘function’].
Functions and methods can be dynamically called in PHP as a callback. In this episode, let’s talk about callables and callbacks, to get you prepared for how WordPress events actually work.
In the last episode, you saw how to dynamically call a procedural function using call_user_func_array( ‘fully/qualified/function_name’, array( ‘args to pass’ ) );. How do you call a method on an object? Let me show you in this episode. You’ll register the methods from within and outside of an object.
Before you begin, make sure that you have the code from the Introduction to PHP Object-Oriented Programming (OOP) for WordPress lab. If you do not have that code, no problem, as you can get it from this GitHub repository. Let’s talk about the WordPress event-driven system. Then you’ll look at the code in WordPress Core to use how it’s using the PHP construct call_user_func_array in order to call each of the pre-registered callbacks when the event is fired. It’s important to have a basic understanding of hooks, which are events and why they exist. In this episode, you’ll get a […]
Registering and unregistering WordPress hook callbacks is different with PHP objects. In this hands-on lab, you’ll learn the syntax, why it’s different, how to register and unregister, as well as strategies to work with other plugins.
Let’s review what you learned in this lab. Then I’ll give you some resources to continue learning about OOP. Congratulations for completing this lab! Other PHP Resources A collection of other awesome educational resources. Want to really level up in PHP? Make sure you also join PHP Nomad. Leading PHP developers share their insights with you each month. Their libraries are filled with PHP awesomeness. If you are a member, they have a deal waiting for you….3 months for FREE! Object-Oriented Bootcamp in PHP – Laracasts Jeffrey Way at Laracasts has an awesome Object-Oriented Bootcamp. If you get a chance, […]
Here are some practical examples of WordPress plugins that are well architected and built to clean coding standards: WordPress Custom Menu Separator by Tom McFarlin Single Post Meta Manager by Tom McFarlin Settings Page – Better Implementation by Alain Schlesser Tag Swapper by Tonya Fulcrum by Tonya Laravel’s Illuminate codebase is a beautifully crafted example of OOP and clean, quality coding. It’s more advanced, but well worth a tour through the code to see how wonderful OOP can be. Symfony is another well-crafted and architected codebase. Like Laravel, it’s both OOP and clean coding.
There are times when you need have some constant value, such as a version. PHP gives the means to declare a constant on the class blueprint using const VERSION. What is a constant? It’s a special variable that you declare and set just once. It’s not changeable. In this episode, you’ll learn how to declare, what it is, why you want, and when to use it. You’ll see how to use it within the class and application. You’ll learn about the difference between $this and self::.