Last active
May 12, 2026 09:59
-
-
Save pritdeveloper/db1331c01d5cfbd895775e297a15389e to your computer and use it in GitHub Desktop.
mu-plugin for wordpress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Plugin Name: MU Singh | |
| * Description: This plugin is used to provide all helper functions and some other functionality for wordpress websites. | |
| */ | |
| /* | |
| mkdir -p wp-content/mu-plugins/mu-singh && cd wp-content/mu-plugins/mu-singh && composer require digitalnature/php-ref filp/whoops && cd ../../../ | |
| */ | |
| defined('ABSPATH') || exit; | |
| if (file_exists(__DIR__ . "/mu-singh/vendor/autoload.php")) { | |
| require_once __DIR__ . "/mu-singh/vendor/autoload.php"; | |
| // ref::config('showPrivateMembers', true); | |
| // ref::config('showIteratorContents', true); | |
| } | |
| if (class_exists('Whoops\Run') && mu_singh_is_dev_environment()) { | |
| $_whoops = new \Whoops\Run(); | |
| $_whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler); | |
| $_whoops->silenceErrorsInPaths('/.*/', E_WARNING | E_NOTICE); | |
| // $_whoops->pushHandler(new \Whoops\Handler\JsonResponseHandler()); | |
| $_whoops->register(); | |
| } | |
| // helper functions copy from https://gist.github.com/pritdeveloper/6c1b3ba4770731fc48fb6fbd329b1430/raw | |
| function mu_singh_is_dev_environment(): bool | |
| { | |
| $host = $_SERVER['HTTP_HOST'] ?? ''; | |
| $dev_hosts = [ | |
| 'localhost', | |
| '.test', | |
| '.dev', | |
| '.local', | |
| '.staging', | |
| 'staging.', | |
| 'stg.', | |
| 'dev.', | |
| ]; | |
| foreach ($dev_hosts as $pattern) { | |
| if (str_contains($host, $pattern)) { | |
| return true; | |
| } | |
| } | |
| return defined('WP_DEBUG') && WP_DEBUG; | |
| } | |
| function mu_singh_fix_upload_url(string $url): string | |
| { | |
| $staging = ''; | |
| $production = ''; | |
| if(!($staging && $production)) { | |
| return $url; | |
| } | |
| if (!str_contains($url, $staging)) { | |
| return $url; | |
| } | |
| $relative = str_replace($staging, '', $url); | |
| $local_path = WP_CONTENT_DIR . $relative; | |
| if (!file_exists($local_path)) { | |
| return str_replace($staging, $production, $url); | |
| } | |
| return $url; | |
| } | |
| add_filter('wp_get_attachment_url', function ($url) { | |
| return mu_singh_fix_upload_url($url); | |
| }); | |
| // Modify each srcset entry | |
| add_filter('wp_calculate_image_srcset', function ($sources) { | |
| foreach ($sources as $width => $source) { | |
| $sources[$width]['url'] = mu_singh_fix_upload_url($source['url']); | |
| } | |
| return $sources; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment