I’m not sure if I phrased this question correctly…
My wife and I have one blog, family.com in this example, that we both contribute to.
I would like family.com to show all blog posts.
me.family.com would show only posts I author and her.family.com would show only posts she authors. later, we will add kid1.family.com and so on…
further, sometimes she/I/we do projects that grow beyond one post. project1.com would show only all posts in the ‘project1’ category. However, unlike authors, only specific categories would have their own domain names.
wp-subdomains-revisited plus domain-mapping-system used together seem closest to what I am looking for but they’re outdated and I’m sure I couldn’t replicate them myself.
Edit 1: I found this wonderful answer by mikeschinkel: Multiple Domain Names – One WP Install (non-Multisite) – Default Each Domain name to Category Archive
Edit 2: I got mikeschinkel’s code working on my site. Great! Now, I’ve been trying to modify it to work with subdomains as well. That has not gone so well – only a white page loads when my modifications are in place.
Edit 3: The site is loading again with working domains and subdomains! How exciting!
Next task is to have all links use their respective domain/subdomain. For example, a post holiday written by me and in category project1 should appear as family.com/holiday, me.family.com/holiday or project1.com/holiday depending on which sub/domain it’s linked from. Any suggestions would be wildly appreciated!!!
(also, it will have to work with WPML -> me.family.com/holiday, when translated, should become her.family.com/休日/ … frightening!)
Edit 4: Just to clarify, functionally this should be no different than viewing an individual author or category archive, the only difference being the archive will have it’s own sub/domain.
Also, my TLD is .is… and my domain is my family name. That means my URL is halfway to being a sentence -> _adam.rabbit.is/…
The goal is to have a url specific to whatever relationship:
adam.rabbit.is/gardening
wife.rabbit.is/coding
rabbit.is/travelling
Edit 5: Updated code – mostly working now. I’m reluctant to mark it as solved because I haven’t removed the base category or added separate header/title for each page… but as is this code works and solves the main question of the post. I hope someone else finds it useful 🙂
Edit 6: I found a nice plugin called wp-no-base-permalink which removes the category base. I updated the code to work with this plugin. Now if I type adam.rabbit.is/uncategorized it shows all uncategorized posts by me – exactly what I was looking for!
answer below…
wp-config.php
if ( is_alt_domain( $_SERVER['SERVER_NAME'] ) ) {
$domain = str_replace( 'www.', '', $_SERVER['SERVER_NAME'] );
define( 'WP_SITEURL', 'http://www.' . $domain );
define( 'WP_HOME', 'http://www.' . $domain );
} else if (is_sub_domain( $_SERVER['HTTP_HOST'] ) ) {
$domain = "{$_SERVER['HTTP_HOST']}";
define( 'WP_SITEURL', 'http://' . $domain );
define( 'WP_HOME', 'http://' . $domain );
} else if (! (is_main_domain( $_SERVER['SERVER_NAME']) ) ) {
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location:http://family.com", true, 301 );
exit;
}
function is_main_domain( $domain ) {
$domain = $_SERVER['HTTP_HOST'] ;
return strcmp( $domain, my_main_domain() ) == 0 ;
}
function is_sub_domain ( $domain ) {
$domain = str_replace( my_main_domain(), '', $_SERVER['HTTP_HOST'] );
return in_array( substr( $domain, 0, -1), sub_domains() );
}
function is_alt_domain( $domain ) {
$domain = str_replace( 'www.', '', $_SERVER['SERVER_NAME'] );
$domain = substr( $domain, 0, strrpos( $domain, '.') );
return array_key_exists( $domain, alt_domains() );
}
function sub_domains() {
return array(
'me',
'her',
'kid1',
);
}
function alt_domains() {
return array(
'project1' => 'com',
'project2' => 'com',
'project3' => 'is',
);
}
function my_main_domain() {
return 'family.com';
}
functions.php
// this seems to be important
update_option('siteurl','http://' . $_SERVER["HTTP_HOST"]);
update_option('home','http://' . $_SERVER["HTTP_HOST"]);
add_action( 'category_link', 'yoursite_category_link', 11, 2 );
function yoursite_category_link( $category_link, $category_id ) {
$parts = explode( "https://wordpress.stackexchange.com/", $category_link );
// if %category% in http://%domain%/%category%/
// is in alt_domains() update the category_link
if ( array_key_exists("{$parts[3]}", alt_domains() ) ) {
$tld = alt_domains()[$parts[3]];
$category_link = "http://www.{$parts[3]}.{$tld}/"; // Strip 'category/%category%/'
}
return $category_link;
}
add_action( 'author_link', 'yoursite_author_link', 11, 2 );
function yoursite_author_link( $author_link, $author_id ) {
$parts = explode( "https://wordpress.stackexchange.com/", $author_link );
// if %author% in http://domain/author/%author%/
// is in sub_domains() update the author_link
if ( "{$parts[3]}" == "author" && in_array("{$parts[4]}", sub_domains() ) ) {
$author_link = "http://{$parts[4]}.family.com/"; // Strip 'author/%author%/'
} else {
$author_link = "http://family.com/author/{$parts[4]}"; // Strip 'author/%author%/'
}
return $author_link;
}
add_action( 'init', 'yoursite_init' );
function yoursite_init() {
// Remove the canonical redirect to the category page
// if %category% in http://%category%.%ext%/ is a blogger category
if ( is_alt_domain( $_SERVER['SERVER_NAME'] ) || is_sub_domain( $_SERVER['HTTP_HOST'] ) ) {
$parts = explode( "https://wordpress.stackexchange.com/", strtolower( $_SERVER['REQUEST_URI'] ) );
if ( (count($parts) > 1) && ( array_key_exists("{$parts[1]}",alt_domains() ) || ( "author" == $parts[1] && in_array("{$parts[2]}", sub_domains() ) ) ) ) {
remove_filter( 'template_redirect', 'redirect_canonical' );
}
}
}
add_action( 'template_redirect', 'yoursite_template_redirect' );
function yoursite_template_redirect() {
// Redirect any http://%domain%.%ext%/%category%/ to http://%category%.%ext%/
// and any http://%domain%.%ext%/author/%author%/ to http://%author%.%domain%.%ext%
$parts = explode( "https://wordpress.stackexchange.com/", strtolower( $_SERVER['REQUEST_URI'] ) );
if ( array_key_exists( "{$parts[1]}", alt_domains()) ) {
$tld = alt_domains()[$parts[1]];
wp_redirect( "http://www.{$parts[1]}.{$tld}/", 301 );
exit;
} else if ( 'author' == $parts[1] && in_array("{$parts[2]}", sub_domains() ) ) {
wp_redirect( "http://{$parts[2]}.famliy.com/", 301 );
exit;
}
}
add_action( 'request', 'yoursite_request' );
function yoursite_request($query_vars) {
// If %category% in http://%category%.%ext%/ is a blogger category set the
// 'category_name' query var to tell WordPress to display the category page.
if ( is_alt_domain( $_SERVER['SERVER_NAME']) || is_sub_domain( $_SERVER['HTTP_HOST'] ) ) {
$category_domain = str_replace( 'www.', '', $_SERVER['SERVER_NAME'] );
$category_domain = substr( $category_domain, 0, strrpos( $category_domain, '.' ) );
$author_name = substr( $_SERVER['HTTP_HOST'], 0, strpos( $_SERVER['HTTP_HOST'], '.' ) );
$parts = explode( "https://wordpress.stackexchange.com/", $_SERVER['REQUEST_URI'] );
$category_name = "{$parts[1]}";
if ( array_key_exists($category_domain, alt_domains()) ) {
if ( strlen( $category_name ) < 1 ) {
$query_vars = array( 'pagename' => $category_domain.'-page' );
} else if ( term_exists("{$category_name}", 'category') ) {
$query_vars = array(
'category__and' => array(
get_category_by_slug($category_domain)->term_id,
get_category_by_slug($category_name)->term_id
)
);
}
} else if ( in_array( $author_name, sub_domains() ) ) {
if ( strlen( $category_name ) < 1 ) {
$query_vars = array( 'pagename' => $author_name.'-page' );
} else if (term_exists("{$category_name}", 'category') ) {
$query_vars = array(
'category_name' => $category_name,
'author_name' => $author_name
);
}
}
}
return $query_vars;
}
And, of course, you also need to point your subdomains (or *) on your nameserver to your wordpress install. You also will need to add a Virtual Host (Nginx) for any other domains you are using… and you will need to set up CORS.
As a side note, this answer used in conjuction with my next answer/question – How to have a static category/author page? – should make my intentions of having one multi-faced site a little more clear 🙂