[wp-hackers] Desiring relative, pretty paths to plugin and theme folders (multisite-friendly)

David @ ALittleCode david at alittlecode.com
Thu Nov 8 00:52:34 UTC 2012

I'd like to be able to rewrite links to theme and plugin folders so that they appear as relative, custom paths.

Disclaimer: I'm new to the list, so forgive me if this has been asked recently. I'm also new to serious WP hacking but intend to develop my skills while shaping up a couple of multisite installations for some serious duty. I intend to manage lots of sub-sites, many of them with mapped custom domains. So I'm eager for advice as I start to dive in. That said, let me get back to what I'm after --

Throughout my multisite sites, I'd like paths such as these:

To appear as, simply:

no "http://domain.com/"
and with "wp-content/themes/" rewritten as simply /_ui/

In essence, I'm trying to accomplish some of the URL cleanup that the Roots Theme employs (http://www.rootstheme.com), but in a way that is not theme-dependent, and which is multisite friendly. (If I can do it, I'd also love to remove the name of the currently active theme from the path, if that's possible.)

While I'm at it, I'd like to customize the path by which multisite users login, changing it to something such as simply "http://domain.com/login/"

For this latter task, I've found this plugin, by Ozh, which rewrites the login path via WordPress's Rewrite API:
--> Ozh explains this use of the rewrite API here: http://planetozh.com/blog/2011/01/pretty-login-url-a-simple-rewrite-api-plugin-example/

My questions:
Is it possible to build on the approach in Ozh's plugin to rewrite paths to theme and plugin directories as well? 
Can the rewrite api make the paths appear as relative links, rather than absolute?

OR would relative paths require something like the approach of the Roots Theme? **But implemented via *plugin* rather than theme functions.**

See these lines from the roots cleanup.php file https://github.com/retlehs/roots/blob/master/lib/cleanup.php

 * Clean up output of stylesheet <link> tags
function roots_clean_style_tag($input) {
  preg_match_all("!<link rel='stylesheet'\s?(id='[^']+')?\s+href='(.*)' type='text/css' media='(.*)' />!", $input, $matches);
  // Only display media if it's print
  $media = $matches[3][0] === 'print' ? ' media="print"' : '';
  return '<link rel="stylesheet" href="' . $matches[2][0] . '"' . $media . '>' . "\n";

add_filter('style_loader_tag', 'roots_clean_style_tag');

 * Root relative URLs
 * WordPress likes to use absolute URLs on everything - let's clean that up.
 * Inspired by http://www.456bereastreet.com/archive/201010/how_to_make_wordpress_urls_root_relative/
 * You can enable/disable this feature in config.php:
 * current_theme_supports('root-relative-urls');
 * @author Scott Walkinshaw <scott.walkinshaw at gmail.com>
function roots_root_relative_url($input) {
  $output = preg_replace_callback(
      // If full URL is home_url("/") and this isn't a subdir install, return a slash for relative root
      'if (isset($matches[0]) && $matches[0] === home_url("/") && str_replace("http://", "", home_url("/", "http"))==$_SERVER["HTTP_HOST"]) { return "/";' .
      // If domain is equal to home_url("/"), then make URL relative
      '} elseif (isset($matches[0]) && strpos($matches[0], home_url("/")) !== false) { return $matches[2];' .
      // If domain is not equal to home_url("/"), do not make external link relative
      '} else { return $matches[0]; };'

  return $output;

More information about the wp-hackers mailing list