Skip to content

Instantly share code, notes, and snippets.

@pritdeveloper
Last active June 27, 2025 14:51
Show Gist options
  • Save pritdeveloper/db1331c01d5cfbd895775e297a15389e to your computer and use it in GitHub Desktop.
Save pritdeveloper/db1331c01d5cfbd895775e297a15389e to your computer and use it in GitHub Desktop.
mu-plugin for wordpress
<?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')) {
// Only check debug condition if host does NOT contain any of these
$excluded_hosts = ['localhost', '.test', '.dev', 'staging.', '.staging', '.local', '.acer', 'singhs.work'];
$should_check_debug = true;
foreach ($excluded_hosts as $excluded) {
if (strpos($_SERVER['HTTP_HOST'] ?? '', $excluded) !== false) {
$should_check_debug = false;
break;
}
}
if (!($should_check_debug && (!defined('WP_DEBUG') || !WP_DEBUG))) {
if (!($should_check_debug && defined('WP_DEBUG_DISPLAY') && !WP_DEBUG_DISPLAY)) {
$_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
/**
* This is used to change all the URLs to use the original domain url
* instead of the cloned server url for all the attachments
*/
function mu_singh_check_url($url)
{
$new_domain = '';
$old_domain = '';
if (!function_exists('curl_init') || !$new_domain || !$old_domain) return $url;
$wp_upload_dir = wp_upload_dir();
$urls_file = '.mu_singh_urls__';
$urls_file_path = untrailingslashit($wp_upload_dir['basedir']) . "/{$urls_file}";
// Ensure the file exists
if (!file_exists($urls_file_path)) {
@touch($urls_file_path);
}
$f_contents = file_get_contents($urls_file_path);
$urls = maybe_unserialize($f_contents);
if (!is_array($urls)) {
$urls = array();
}
$md5 = md5($url);
if (isset($urls[$md5])) return $urls[$md5];
// Perform cURL HEAD request
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// If not 200, switch to old domain
if ($code !== 200) {
$url = str_replace(untrailingslashit($new_domain), untrailingslashit($old_domain), $url);
}
$urls[$md5] = $url;
// Save updated cache
file_put_contents($urls_file_path, maybe_serialize($urls));
return $url;
}
add_filter('wp_get_attachment_url', function ($url) {
return mu_singh_check_url($url);
});
// Modify each srcset entry
add_filter('wp_calculate_image_srcset', function ($sources) {
foreach ($sources as $width => &$source) {
$source['url'] = mu_singh_check_url($source['url']);
}
return $sources;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment