Nat TaylorObservations & more

Home / Blog / Announcing Two New Sections

Announcing Two New Sections

Published on

I rolled out two new sections (/webdesign and /eastboston) which may not seem like much, but is the result of a decision about where to host and how to separate things which I’ve been debating for longer than I’m willing to admit.

In addition to nattaylor.com, I was maintaining two other domains: taylorwebdesigns.com and liveeastboston.com. Despite all being hosted on the same server, maintenance and upgrades were a chore, but most detrimentally, myself, search engines and visitors alike had to keep track of 3 distinct things. For me it often caused delays as I chose the right place to put something, but more importantly, it meant that search engines had to divide rankings 3 ways.

With everything within nattaylor.com’s WordPress install there is only one theme, one upgrade path, one configuration and most importantly one site to rank.

WordPress Tweaks

These might be considered WordPress abuses, but that’s part of the beauty of WordPress!

Category Template Override

For the pages within a section, I specify the template. For posts in a section, instead of using the default hierarchy of category-{slug}.php I wanted to use the section’s template, so I added this to my functions.php

add_filter(
	'category_template',
	function ($template) {
		if ( is_category(array( 21 )  )  ) {
			$template = locate_template( 'eb.php' ); 
		}
		return $template;
	}
);

WordPress Posts Within Subdirectories

Another thing I wanted was for each “section” to have it’s (pseudo) own blog such that the URLs fall within the section’s URL (e.g. a blog post for East Boston is /eastboston/blog/2019/post-name) I accomplished that with this code.

add_filter('post_link', function ( $permalink, $post ) {
		$category = get_the_category($post->ID);
		if($category[0]->slug=='eastboston') {
			$permalink = str_replace('/blog/', '/eastboston/blog/' , $permalink );
		} else if ($category[0]->slug=='web-design') {
			$permalink = str_replace('/blog/', '/webdesign/blog/' , $permalink );
		}
		return $permalink;
}, 10, 2 );

add_action( 'generate_rewrite_rules',function() {
	global $wp_rewrite;
	$wp_rewrite->rules =
		['^(eastboston|webdesign)/blog/?$' => 'index.php?category_name=$matches[1]']
		+ ['^(?:eastboston|webdesign)/blog/([0-9]{4})/([^/]+)(?:/([0-9]+))?/?$' => 'year=$matches[1]&name=$matches[2]&page=']
		+ $wp_rewrite->rules;
	return $wp_rewrite->rules;
});

WordPress Page as Directory Index

For /eastboston I wanted to have files accessible within the directory, so I used .htaccess so that /eastboston is a WordPress page, but I can also put a file at /eastboston/foo.html

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} /eastboston/?$
RewriteRule ^(.*)$ /wordpress/$1 [L]

WordPress Files in Subdirectory

I also moved WordPress into a subdirectory without changing the URL, in order to keep the directory structure clear for the site root. I had some trouble with the .htaccess configuration at first, but landed on the following.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?nattaylor.com$
RewriteCond %{REQUEST_URI} !^/wordpress/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /wordpress/$1
RewriteCond %{HTTP_HOST} ^(www.)?nattaylor.com$
RewriteRule ^(/)?$ wordpress/index.php [L] 

In the future, I might depart from this strategy and install multiple instance of WordPress so that I can have more control over which posts show up where, but for now I am happy with the simplicity.

«
»