Applying patches to 3rd party WordPress plugins

I recently stumbled upon a bug in a third party plugin. I opened an issue on the wordpress.org support forum. I didn’t have much confidence in the author to fix the bug, as someone else mentioned the bug 1.5 years before. I needed a way to modify a 3rd party plugin’s codebase without manually having to apply the changes after each update/deploy.

I decided to fork the plugin repo and apply the changes myself. I opened a PR, but still had no confidence in the author to merge the PR anytime soon. I was searching for a way to automatically apply the changes to the plugin when deploying this website. I did some searching and found this composer package. To be able to apply the patch, we need a patch file obviously.

You can add .patch to the pull request’s url, and Github will automatically serve you the patch (https://github.com/hijiriworld/intuitive-custom-post-order/pull/46.patch).

We can now install the composer-patches package

composer require cweagans/composer-patches

We can then add the patch in our composer file:

...
"extra": {
  ...
  "patches": {
    "wpackagist-plugin/intuitive-custom-post-order": {
      "Fix for multisite": "https://github.com/hijiriworld/intuitive-custom-post-order/pull/46.patch"
    }
  }
},
...

Running composer install or composer update will now automatically apply the patch.

Edit:

You can also apply patches from a local patch file (in case the github repo doesn’t exist). An example for https://wordpress.org/support/topic/trying-to-access-array-offset-on-value-of-type-null-mime-types-php22/:

cp www/app/plugins/svg-support/functions/mime-types.php www/app/plugins/svg-support/functions/mime-types-fix.php

# apply changes in www/app/plugins/svg-support/functions/mime-types-fix.php

diff -Naur www/app/plugins/svg-support/functions/mime-types.php www/app/plugins/svg-support/functions/mime-types-fix.php > patches/svg-support-mime-types.txt
rm www/app/plugins/svg-support/functions/mime-types-fix.php

Patchfile:

--- www/app/plugins/svg-support/functions/mime-types.php	2022-11-17 22:10:34.000000000 +0100
+++ www/app/plugins/svg-support/functions/mime-types-fix.php	2022-12-12 10:07:25.000000000 +0100
@@ -19,7 +19,7 @@
 	$allowed_roles_array = array();
 	$is_role_allowed = array();
 	
-	$allowed_roles_array = (array) $bodhi_svgs_options['restrict'];
+	$allowed_roles_array = $allowed_roles_array = isset($bodhi_svgs_options['restrict']) ? (array)$bodhi_svgs_options['restrict'] : [];
 	
 	$user = wp_get_current_user();
  
...
"extra": {
  ...
  "patches": {
        "wpackagist-plugin/svg-support": {
        "Fix fatal error on PHP 8": "patches/svg-support-mime-types.txt"
        }
    }
},
...

Geef een reactie

Je e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *

Overtuigd?

Neem dan contact op voor een offerte of vrijblijvend gesprek.
Stuur een e-mail naar [email protected] of maak gebruik van het contactformulier.