Fruitfulcode / Fruitful

Free WordPress theme
https://fruitful.fruitfulcode.com
111 stars 68 forks source link

Easier Child Theme Development #5

Closed nathanph closed 10 years ago

nathanph commented 10 years ago

Surrounded function definitions with code that checks to see if the function is already defined. This change allows for easier child theme development. It is a recommended practice for theme developers.

Note: this change was done using regular expressions; only basic testing was done to ensure problems didn't exist. I tested these changes using my child theme.

Expressions: Find: ^function (?'functionName'[^[\s(]])(?'functionCode'.*?)^} Replace: \nif( !function_exists('$+{functionName}') ) {\nfunction $+{functionName} $+{functionCode}}}

nathanph commented 10 years ago

Why you should merge my pull request!

=== Scenario ===
AJ (Awesome John) loves the Fruitful theme, but wants to make a slight modification so that the theme will better suit his needs. AJ decides he's going to add an additional social icon to the theme.

Being the wise developer that AJ is, he decides to make a child theme; this way if the Fruitful theme updates the changes will cascade into his child theme and he will not lose any code he has written.

AJ begins by finding all instances of the Fruitful theme that pertain to social icons:

AJ creates instances of each of these files and functions within his child theme and modifies them as necessary. Once finished AJ uploads his changes to his web server and refreshes his browser. AJ is immediately hit with a flurry of errors because the function names he is using are already defined within the parent theme (Fruitful).

In an effort to fix this AJ renames each of his functions to something new:

However, now AJ must go through and modify any functions that call one of the three functions that he has renamed. Furthermore, AJ must now also rename any other functions he modifies. This process needs to be done recursively until all instances of functions or hooks that indirectly or directly call one of the original three functions has been replaced.

These functions include:

At this point AJ has been forced to radically modify his child theme because having duplicately named functions causes PHP to throw errors. The most graceful fix for this would be if the parent theme had each of its function definitions wrapped in function_exists checks; this would make the parent theme's functions "pluggable."

TLDR; pluggable functions are awesome!