Developers have spent countless hours working on a plugin that seems to be built just for you, just for this moment. If only it wouldn’t…. This is a problem nearly every developer faces at some point during a project. Knowing how to safely customize functionality is key to building products that will stand the test of time (or major updates.)
This talk will cover the following topics:
Hooks
Filters
Overwriting Functions
Modifying Returned content
Requesting a change to a plugin
This talk will define and provide a use case for each of the possible pathways listed above. This presentation is for beginning to intermediate developers and though I will use as little code as possible, given the nature of this talk I would imagine each item will require 1 code example. The slides will be available ahead of time on slideshare and the code examples will be available on GitHub.
2. Defining Your Problem
Make sure you’re solving a real problem
https://marcgratch.com | mgratch
WordCamp Boston 2017
3. Make sure you’re solving a real problem
• Is this behavior self-inflicted or inherent to the
plugin?
• Does this (will this) behavior bother the client? (Or is
it just me?)
• Will this impact launch?
• Can it be fixed with a bit of CSS?
https://marcgratch.com | mgratch
WordCamp Boston 2017
4. Be Aware: Dangerous Territory
Extending another plugin can get messy
https://marcgratch.com | mgratch
WordCamp Boston 2017
5. Some “dangers” of Extending/Integrating with
a 3rd Party Plugin
•Is the plugin regularly maintained?
•Are the plugin updates (typically) backwards
compatible?
•Did you ensure your code is maintainable?
https://marcgratch.com | mgratch
WordCamp Boston 2017
7. Find a Good Place to Start:
Defining your Goal
• Making a Change on the Frontend:
• Determine what file manages where (or near) your final code will need to output (or
need to modify.)
• Look at the source code (in the browser)
• Find unique strings to search for. (HTML classes or IDs are usually a good place to
start.)
• Search the plugins code base for this unique string.
https://marcgratch.com | mgratch
WordCamp Boston 2017
8. Find a Good Place to Start:
Defining your Goal
• Making a Change on the Backend:
• READ THE DOCS
• Try to identify the function(s)handling the data you wish to change.
• Search All Files in the Plugin for `apply_filters` or `do_action`
• USE BREAK POINTS / XDEBUG / STEP THROUGH
https://marcgratch.com | mgratch
WordCamp Boston 2017
12. Overriding Functions: Pluggable Functions
if ( ! function_exists( ‘some_function’ ) ) {
// do something different
}
• You can only do this once
• Only works as long as the pluggable function exists
https://marcgratch.com | mgratch
WordCamp Boston 2017
13. Pluggable “Template” Files
• When the original file is updated… yours will
remain. Old & Outdated.
• Multiple implementations are problematic.
• Only works as long as the original file exists/used
or no longer performs its intended function.
https://marcgratch.com | mgratch
WordCamp Boston 2017
14. Modifying Returned Content
• DO NOT MODIFY HTML WITH str_replace()
• Browsers are lenient – not all HTML is written the same.
• Modifying return HTML requires
DOMDocument Class
• Make sure the `class_exists(‘DOMDocument’)`
https://marcgratch.com | mgratch
WordCamp Boston 2017
15. If All Else Fails: Write a Patch
Then ask the plugin developers to implement it…
https://marcgratch.com | mgratch
WordCamp Boston 2017
16. Asking for a Change to the Core Plugin:
•Be Polite
•Identify if it’s an Edge Case
•Explain the Reason for your Patch
•Offer to fix another bug if you can…
https://marcgratch.com | mgratch
WordCamp Boston 2017
17. Asking for a Change to the Core Plugin:
…the other reason I was reaching out was in regards to submitting patches. I
realize it is likely an edge case, but I am hopeful that you might be willing to
consider implementing the attached patch. It simply adds the ability to add custom
fields (or content) to the bottom of the `getImageTab` output. I have tested it and
written further unit tests for it. The patch appears to work as expected.
Furthermore, I am familiar with your code base and I am happy to help with any
other bugs or roadmap features as I continue to work with your plugin…
https://marcgratch.com | mgratch
WordCamp Boston 2017
18. Asking for a Change to the Core Plugin:
First let me say I have been using your plugin since 2012 and I am always happy
to support good plugins and their authors.
I am hopeful you are willing to consider implementing the attached patch. It
allows form submission via AJAX. I am building a single page app and
honestly your plugin does everything I need except post back is killing the
experience. I have tested it on a vanilla WP install and it seems to be working.
If you run unit tests I am happy to write a few for this new functionality as
well…
https://marcgratch.com | mgratch
WordCamp Boston 2017