Last active
December 8, 2021 01:30
-
-
Save afragen/dcc77953ec95cdc1d06a2ed4aea67bf8 to your computer and use it in GitHub Desktop.
Simple plugin to fix the plugins_url() and get_template_directory_uri() paths when WP installed in a subdirectory and wp-content not in ABSPATH
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
| add_filter( | |
| 'subdirectory_fix_plugins_url', | |
| function() { | |
| return [ 'wpforms-lite', 'query-monitor' ]; | |
| } | |
| ); | |
| add_filter( | |
| 'subdirectory_fix_template', | |
| function() { | |
| return 'twentytwentyone'; | |
| } | |
| ); |
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: Subdirectory Plugin/Theme Directory URI Fix | |
| * Plugin URI: https://gist.github.com/afragen/dcc77953ec95cdc1d06a2ed4aea67bf8 | |
| * Description: This plugin is used to fix plugins_url() and get_template_directory_uri() for WP subdirectory installation. Add plugin or theme slugs via filters in the plugin. | |
| * Version: 0.1 | |
| * Author: Andy Fragen | |
| * License: MIT | |
| * Requires at least: 5.2 | |
| * Requires PHP: 7.1 | |
| * Gist Plugin URI: https://gist.github.com/afragen/dcc77953ec95cdc1d06a2ed4aea67bf8 | |
| * | |
| * @package Fragen\Subdirectory\Fix | |
| * @see https://wordpress.org/support/article/giving-wordpress-its-own-directory/ | |
| */ | |
| namespace Fragen\Subdirectory; | |
| /** | |
| * Class Fix | |
| */ | |
| class Fix { | |
| /** | |
| * Site URL. | |
| * | |
| * @var string | |
| */ | |
| protected static $site_url; | |
| /** | |
| * Home URL. | |
| * | |
| * @var string | |
| */ | |
| protected static $home_url; | |
| /** | |
| * Array of slugs to test. | |
| * | |
| * @var array | |
| */ | |
| protected static $plugins = [ 'wp-dismiss-notice', 'debug-quick-look', 'query-monitor' ]; | |
| /** | |
| * Initialize variables and load hooks. | |
| * | |
| * @return void | |
| */ | |
| public function init() { | |
| static::$site_url = get_option( 'siteurl' ); | |
| static::$home_url = get_option( 'home' ); | |
| add_filter( 'plugins_url', [ $this, 'plugins_url' ], 10, 3 ); | |
| add_filter( 'template_directory_uri', [ $this, 'template_directory_uri' ], 10, 3 ); | |
| } | |
| /** | |
| * Update plugins_url() for possible subdirectory install with wp-content outside of ABSPATH. | |
| * | |
| * Must have correctly set WordPress Address (URL) and Site Address (URL) in Settings. | |
| * Must have correctly set WP_CONTENT_DIR in wp-config.php | |
| * | |
| * @param [type] $url The complete URL to the plugins directory including scheme and path. | |
| * @param [type] $path Path relative to the URL to the plugins directory. Blank string | |
| * if no path is specified. | |
| * @param [type] $plugin The plugin file path to be relative to. Blank string if no plugin | |
| * is specified. | |
| * | |
| * @return string | |
| */ | |
| public function plugins_url( $url, $path, $plugin ) { | |
| $slug = basename( dirname( $plugin ) ); | |
| if ( static::$site_url !== static::$home_url ) { | |
| /** | |
| * Filter the supplied plugin slugs. | |
| * | |
| * Accept a string or array as return value to be converted to an array. | |
| * | |
| * @param array Array of supplied plugin slugs. | |
| */ | |
| $plugin_slugs = (array) apply_filters( 'subdirectory_fix_plugins_url', static::$plugins ); | |
| $plugin_slugs = array_unique( array_merge( static::$plugins, $plugin_slugs ) ); | |
| foreach ( $plugin_slugs as $plugin_slug ) { | |
| if ( $plugin_slug === $slug ) { | |
| $url = str_replace( static::$site_url, static::$home_url, $url ); | |
| break; | |
| } | |
| } | |
| } | |
| return $url; | |
| } | |
| /** | |
| * Update get_template_directory_uri() for possible subdirectory install with wp-content | |
| * outside of ABSPATH. | |
| * | |
| * Must have correctly set WordPress Address (URL) and Site Address (URL) in Settings. | |
| * Must have correctly set WP_CONTENT_DIR in wp-config.php | |
| * | |
| * @param string $template_dir_uri The URI of the current theme directory. | |
| * @param string $template Directory name of the current theme. | |
| * @param string $theme_root_uri The themes root URI. | |
| * | |
| * @return string | |
| */ | |
| public function template_directory_uri( $template_dir_uri, $template, $theme_root_uri ) { | |
| if ( static::$site_url !== static::$home_url ) { | |
| /** | |
| * Filter to add theme slug. | |
| * | |
| * @param string Default empty string. | |
| */ | |
| if ( apply_filters( 'subdirectory_fix_template', '' ) === $template ) { | |
| $template_dir_uri = str_replace( static::$site_url, static::$home_url, $template_dir_uri ); | |
| } | |
| } | |
| return $template_dir_uri; | |
| } | |
| } | |
| ( new Fix() )->init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment