There are times when you can’t get a hold of the object in order to remove a method callback from that object. The author wrote it such that s/he did not expose the object for you. What do you do? How can you get that object in order to do your work and unregister its callback? Here are strategies in order: Talk to the author and see s/he will add a filter or action to pass the object to you. If it’s open source, submit a pull request to add the filter/action code. Evaluate if you have to use this […]
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.
Wrap it Up
Let’s review what you learned in this lab about registering and unregistering callbacks for both static and object methods. You looked into WordPress Core and saw that it is using call_user_func_array to call each of the registered callbacks for a given event hook. You learned strategies for working with 3rd party code including WooCommerce. If the WordPress Event registrations confused you, go take this lab. Congratulations for completing this lab!
Design Strategy
When you are registering and unregistering callbacks within an object, you typically do this task as part of the object’s initialization process. Instead of putting all of the init tasks into the constructor, break it out. Put the callback registration into a separate method called init_events().
3rd Party Static Callbacks
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 […]
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? […]
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 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 […]