Created
April 17, 2025 05:54
-
-
Save tayyebi/d9b017d06b3623737f5f7993a19d738e to your computer and use it in GitHub Desktop.
mu-plugins
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: WP Admin Access Control | |
Description: Restricts access to wp-admin based on environment and custom header or referrer. | |
*/ | |
function check_wp_admin_access() { | |
// Check if the current request is for wp-admin and the environment is production | |
if ((is_admin() || $GLOBALS['pagenow'] === 'wp-login.php') && getenv('WP_ENV') === 'production') { | |
// Retrieve the HTACCESS_TOKEN from the environment variable | |
$htaccess_token = getenv('HTACCESS_TOKEN'); | |
// Get the custom header value (if set) | |
$x_htaccess_header = $_SERVER['HTTP_X_HTACCESS'] ?? ''; | |
// Define allowed referrers as an array of patterns | |
$allowed_referers = [ | |
'/contact-us/', | |
'/forms/', | |
]; | |
// Get the HTTP Referer (if set) | |
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; | |
// Validate the custom header | |
$is_valid_header = ($x_htaccess_header === $htaccess_token); | |
// Validate the referrer against the allowed patterns | |
$is_valid_referer = false; | |
foreach ($allowed_referers as $pattern) { | |
if (strpos($referer, $pattern) !== false) { | |
$is_valid_referer = true; | |
break; | |
} | |
} | |
// Allow access if either the header is valid OR the referrer is valid | |
if (!$is_valid_header && !$is_valid_referer) { | |
$error_message = '<div style="text-align:center; font-family:monospace; font-size:16px;">' | |
. '<strong>WARNING:</strong> This is a PRIVATE COMPUTER SYSTEM administration area.<br>' | |
. 'Unauthorized access is strictly prohibited by law.<br>' | |
. 'Log off IMMEDIATELY or your actions will be logged and reported to the authorities.<br>' | |
. '<br>' | |
. '<em>You have been warned.</em>' | |
. '</div>'; | |
wp_die( | |
$error_message, | |
'403 Forbidden - Security Breach', | |
['response' => 403, 'back_link' => true] | |
); | |
} | |
} | |
} | |
// Hook into the 'init' action to enforce access control | |
add_action('init', 'check_wp_admin_access'); |
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: Dynamic URL | |
Description: Maps multiple domains to the main site. | |
Version: 1.7 | |
Author: Mohammad R. Tayyebi | |
*/ | |
if (defined('WP_CLI') && WP_CLI) | |
return; | |
define('FORCE_SSL_ADMIN', false); | |
define('FORCE_SSL_LOGIN', false); | |
if ((isset($_ENV["HTTPS"]) && ("on" == $_ENV["HTTPS"])) | |
|| (isset($_SERVER["HTTP_X_FORWARDED_SSL"]) && (strpos($_SERVER["HTTP_X_FORWARDED_SSL"], "1") !== false)) | |
|| (isset($_SERVER["HTTP_X_FORWARDED_SSL"]) && (strpos($_SERVER["HTTP_X_FORWARDED_SSL"], "on") !== false)) | |
|| (isset($_SERVER["HTTP_CF_VISITOR"]) && (strpos($_SERVER["HTTP_CF_VISITOR"], "https") !== false)) | |
|| (isset($_SERVER["HTTP_CLOUDFRONT_FORWARDED_PROTO"]) && (strpos($_SERVER["HTTP_CLOUDFRONT_FORWARDED_PROTO"], "https") !== false)) | |
|| (isset($_SERVER["HTTP_X_FORWARDED_PROTO"]) && (strpos($_SERVER["HTTP_X_FORWARDED_PROTO"], "https") !== false)) | |
|| (isset($_SERVER["HTTP_X_PROTO"]) && (strpos($_SERVER["HTTP_X_PROTO"], "SSL") !== false)) | |
) { | |
$_SERVER["HTTPS"] = "on"; | |
$_SERVER['SERVER_PORT'] = 443; | |
} | |
if (!empty($_SERVER['HTTP_X_FORWARDED_HOST'])) { | |
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST']; | |
} | |
$http_host = $_SERVER['HTTP_HOST']; | |
$scheme = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http'; | |
$site_url = $scheme . '://' . $http_host; | |
add_action('init', function() use ($site_url, $http_host) { | |
define('WP_HOME', $site_url); | |
define('WP_SITEURL', $site_url); | |
}); | |
// Filter to set the site URL option | |
add_filter('option_siteurl', function($value) use ($site_url) { | |
return $site_url; | |
}); | |
// Filter to set the home URL option | |
add_filter('option_home', function($value) use ($site_url) { | |
return $site_url; | |
}); | |
// Filter to adjust attachment URLs to use dynamic URL | |
add_filter('wp_get_attachment_url', function($url, $post_id) use ($site_url) { | |
return $site_url . wp_parse_url($url, PHP_URL_PATH); | |
}, 10, 2); | |
// Filter to adjust image source URLs to use dynamic URL | |
add_filter('wp_get_attachment_image_src', function($image, $attachment_id, $size, $icon) use ($site_url) { | |
if ($image) { | |
$image[0] = $site_url . wp_parse_url($image[0], PHP_URL_PATH); | |
} | |
return $image; | |
}, 10, 4); | |
// Filter to ensure JavaScript and CSS assets use dynamic URL | |
add_filter('style_loader_src', function($src, $handle) use ($site_url) { | |
return $site_url . wp_parse_url($src, PHP_URL_PATH); | |
}, 10, 2); | |
// Filter to ensure JavaScript assets use dynamic URL | |
add_filter('script_loader_src', function($src, $handle) use ($site_url) { | |
return $site_url . wp_parse_url($src, PHP_URL_PATH); | |
}, 10, 2); | |
// Filter to adjust stylesheet directory URI to use dynamic URL | |
add_filter('stylesheet_directory_uri', function($uri) use ($site_url) { | |
return $site_url . wp_parse_url($uri, PHP_URL_PATH); | |
}); | |
// Filter to adjust template directory URI to use dynamic URL | |
add_filter('template_directory_uri', function($uri) use ($site_url) { | |
return $site_url . wp_parse_url($uri, PHP_URL_PATH); | |
}); | |
// Filter to adjust plugins URL to use dynamic URL | |
add_filter('plugins_url', function($url, $path, $plugin) use ($site_url) { | |
return $site_url . wp_parse_url($url, PHP_URL_PATH); | |
}, 10, 3); | |
// Filter to adjust includes URL to use dynamic URL | |
add_filter('includes_url', function($url, $path) use ($site_url) { | |
return $site_url . wp_parse_url($url, PHP_URL_PATH); | |
}, 10, 2); | |
//// Filter to adjust admin URL to use dynamic URL | |
//add_filter('admin_url', function($url, $path, $blog_id) use ($site_url) { | |
// return $site_url . wp_parse_url($url, PHP_URL_PATH); | |
//}, 10, 3); | |
// Filter to adjust network site URL to use dynamic URL | |
add_filter('network_site_url', function($url, $path, $scheme) use ($site_url) { | |
return $site_url . $path; | |
}, 10, 3); | |
// Filter to ensure Elementor URLs use the dynamic URL | |
add_filter('elementor/utils/get_placeholder_image_src', function($url) use ($site_url) { | |
return $site_url . wp_parse_url($url, PHP_URL_PATH); | |
}); | |
// Filter to ensure Elementor file URLs use the dynamic URL | |
add_filter('elementor/files/file_url', function($url) use ($site_url) { | |
return $site_url . wp_parse_url($url, PHP_URL_PATH); | |
}); | |
// Hook into Elementor to replace URLs dynamically | |
add_action('elementor/frontend/after_register_scripts', function() use ($site_url) { | |
add_filter('elementor/frontend/get_settings', function($settings) use ($site_url) { | |
foreach ($settings as $key => $value) { | |
if (is_string($value) && strpos($value, home_url()) !== false) { | |
$settings[$key] = str_replace(home_url(), $site_url, $value); | |
} | |
} | |
return $settings; | |
}); | |
}); | |
// Inline script to update Swiper images with dynamic URLs | |
add_action('wp_enqueue_scripts', function() use ($site_url) { | |
wp_add_inline_script('swiper', " | |
document.addEventListener('DOMContentLoaded', function () { | |
var swiperImages = document.querySelectorAll('.swiper-slide img'); | |
swiperImages.forEach(function(img) { | |
var src = img.getAttribute('src'); | |
if (src) { | |
var newSrc = '$site_url' + new URL(src).pathname; | |
img.setAttribute('src', newSrc); | |
} | |
}); | |
}); | |
"); | |
}); | |
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: Noindex Nofollow for Specific Domains | |
* Description: Adds meta nofollow noindex tags and headers for specific domains. | |
* Author: MohammadReza Tayyebi | |
* Version: 1.1 | |
*/ | |
// List of whitelisted domains | |
$whitelistedDomains = ['example.com']; | |
// List of URLs to apply noindex nofollow | |
$blacklistedUrls = ['/knowledge-base', '/kb']; | |
// Function to check if current URL should be noindex nofollow | |
function shouldNofollowNoindex() { | |
global $whitelistedDomains, $blacklistedUrls; | |
$hostname = parse_url($_SERVER['HTTP_HOST'], PHP_URL_HOST) ?: $_SERVER['HTTP_HOST']; | |
foreach ($blacklistedUrls as $url) { | |
if (strpos($_SERVER['REQUEST_URI'], $url) !== false || !in_array($hostname, $whitelistedDomains)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
// Hook to add meta tags in the head section | |
function addNofollowNoindexMetaTag() { | |
if (shouldNofollowNoindex()) { | |
// Remove existing robots meta tags to avoid conflicts | |
remove_action('wp_head', 'wp_robots', 1); | |
echo '<meta name="robots" content="noindex, nofollow">' . "\n"; | |
} | |
} | |
add_action('wp_head', 'addNofollowNoindexMetaTag', 1); | |
// Hook to add noindex nofollow headers | |
function addNofollowNoindexHeaders() { | |
if (shouldNofollowNoindex()) { | |
header('X-Robots-Tag: noindex, nofollow', true); | |
} | |
} | |
add_action('send_headers', 'addNofollowNoindexHeaders'); | |
// Hook into Yoast SEO to modify meta robots | |
function yoastSeoNofollowNoindex($robots) { | |
if (shouldNofollowNoindex()) { | |
$robots['index'] = 'noindex'; | |
$robots['follow'] = 'nofollow'; | |
} | |
return $robots; | |
} | |
add_filter('wpseo_robots', 'yoastSeoNofollowNoindex'); | |
// Hook into Rank Math SEO to modify meta robots | |
function rankMathNofollowNoindex($robots) { | |
if (shouldNofollowNoindex()) { | |
$robots['index'] = 'noindex'; | |
$robots['follow'] = 'nofollow'; | |
} | |
return $robots; | |
} | |
add_filter('rank_math/frontend/robots', 'rankMathNofollowNoindex'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment