Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialFinn Terdal
15,997 PointsModify a Plugin's PHP Class in WordPress
I have a WordPress website that relies heavily on a plugin. We're using a child theme, as necessary to appease the WordPress gods, and are diligent to keep all changes in the child theme folder. Modifying the source code of a plugin directly is a big taboo, since updating the plugin will cause the site to break.
That being said, we do need to change the functionality of the plugin from time to time. We can just override the relevant function in theme-child/functions.php and all is well. Whenever the plugin is updated, the new, customized function gets called instead.
But what if we want to modify AN ENTIRE CLASS?! We've got a PHP class whose methods we need to modify, but those methods are themselves called from inside the plugin. Just to make sure it all works, we did try modifying the plugin on a test site, and our site works great. Now its a matter of taking those changes, and moving them to the child theme. Any suggestions?
(I did find a discussion about this on Stack Overflow, but the solution requires altering the way these methods are called by using a class decorator, and that would still require us to modify code in the plugin. http://stackoverflow.com/questions/3011910/how-to-add-a-method-to-an-existing-class-in-php)