Skip to content

Commit

Permalink
re-enable mu-plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
batonac committed Sep 12, 2024
1 parent 6ff4607 commit dc46039
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
8 changes: 4 additions & 4 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ if [ ! -f /var/www/html/wp-includes/version.php ]; then
install_wordpress
fi

# # Always copy the custom mu-plugins
# echo "Copying custom mu-plugins"
# cp -r /mu-plugins /var/www/html/wp-content/mu-plugins
# chmod 755 /var/www/html/wp-content/mu-plugins
# Always copy the custom mu-plugins
echo "Copying custom mu-plugins"
cp -r /mu-plugins /var/www/html/wp-content/mu-plugins
chmod 755 /var/www/html/wp-content/mu-plugins

# if PROC_TYPE=worker, then run cron jobs on the minute via wp-cli
if [ "$PROC_TYPE" = "worker" ]; then
Expand Down
64 changes: 64 additions & 0 deletions mu-plugins/loopback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Plugin Name: Internal URL Rewrite
* Description: Rewrites internal URLs for proper loopback and REST API functionality behind a reverse proxy
* Version: 1.0
* Author: Avunu LLC
* Author URI: https://avu.nu
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: internal-url-rewrite
*
* This plugin rewrites internal URLs to ensure proper functionality
* of WordPress when running behind a reverse proxy or in a containerized
* environment. It handles loopback requests, REST API calls, and other
* internal communications.
*/

// Ensure this file is being included by WordPress
if (!defined('ABSPATH')) {
exit;
}

// Define allowed hosts for internal requests
define('WP_ACCESSIBLE_HOSTS', 'localhost,127.0.0.1');

// Function to rewrite URLs for internal requests
function rewrite_internal_url($url) {
if ((defined('DOING_AJAX') && DOING_AJAX) ||
(defined('REST_REQUEST') && REST_REQUEST) ||
wp_doing_cron()) {

$site_url = parse_url(get_site_url());
$url_parts = parse_url($url);

// Only rewrite if the host matches the site's host
if (isset($url_parts['host']) && $url_parts['host'] === $site_url['host']) {
$url = set_url_scheme($url, 'http');
$url = str_replace($url_parts['host'], 'localhost', $url);
}
}
return $url;
}

// Apply URL rewriting to various WordPress URL functions
add_filter('site_url', 'rewrite_internal_url', 10, 1);
add_filter('home_url', 'rewrite_internal_url', 10, 1);
add_filter('admin_url', 'rewrite_internal_url', 10, 1);
add_filter('includes_url', 'rewrite_internal_url', 10, 1);
add_filter('content_url', 'rewrite_internal_url', 10, 1);
add_filter('plugins_url', 'rewrite_internal_url', 10, 1);
add_filter('wp_get_attachment_url', 'rewrite_internal_url', 10, 1);

// Force WordPress to use HTTP for loopback requests
add_filter('http_request_args', function($args, $url) {
if (strpos($url, 'localhost') !== false || strpos($url, '127.0.0.1') !== false) {
$args['sslverify'] = false;
$args['curl'][CURLOPT_SSL_VERIFYPEER] = false;
$args['curl'][CURLOPT_SSL_VERIFYHOST] = false;
}
return $args;
}, 10, 2);

// Optionally, set the site URL for the REST API
add_filter('rest_url', 'rewrite_internal_url', 10, 1);
4 changes: 4 additions & 0 deletions wordpress.nix
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ pkgs.dockerTools.buildLayeredImage {
cp ${./docker-entrypoint.sh} docker-entrypoint.sh
chmod +x docker-entrypoint.sh
# copy must-use plugins
mkdir mu-plugins
cp ${./mu-plugins/loopback.php} mu-plugins/
# Symlink CA certificates
ln -s ${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt etc/ssl/certs/ca-certificates.crt
'';
Expand Down

0 comments on commit dc46039

Please sign in to comment.