There will be projects where you need to remove a static method’s callback from a 3rd party plugin. How do you find and remove it? You should never edit their code. Why? Because when they do updates, your changes will be overwritten. Instead, you need to find where the callback is registered, i.e. with a add_action or add_filter. Then, you need the class name. Lastly, you have to figure out the timing of when the callback is registered. Why? Because you can’t unregister it until it’s been registered. Doing it too early is a timing mistake and will cause wonky […]
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.
3rd Party Object Callbacks
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? […]
Unregistering an Object’s Method Callback
Unregistering an object’s method callback is the same as registering it. Remember that you need the following: The name of the method The object
Unregistering a Static Method Callback
Unregistering a static method is the same as registering, except that you use remove_action or remove_filter. Keep the syntax the same.
Static Method Callback
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.
Registering a Static Method to a WordPress Event Hook
Let’s walk through registering a static method to a WordPress Event, such as init. You’ll do it both internally within the class blueprint and outside of it.
Registering an Object’s Method to a WordPress Event Hook
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’].
Meet the PHP Callback
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.
Object’s Method Callback
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.
Lab Introduction
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 […]