Advertisement
Creative Coding

Custom Page Template Page Based on URL Rewrite

by

Ever wondered about WordPress custom URL rewrites? Here I was dealing with an interesting problem. I wanted to add a rewrite rule on a permalink and wanted to use a custom template for that rewrite rule. However, this is not an exhaustive post about how to do rewrite rules, there is a very good detailed tutorial about this if you want to know more details.


Objective:

  1. Add a rewrite rule on a custom post type permalink
  2. Use a page template based on the rewrite rule

Here we have a movie post type. The permalink structure is movie/{movie-name}. I want to append a photos and videos endpoint to that url, so the structure will be movie/{movie-name}/photos and movie/{movie-name}/videos. As you can guess, I want to show all the photos and videos on a separate page from that movie and want to use separate page template to accomplish this.


1. Register Custom URL

function prefix_movie_rewrite_rule() {
	add_rewrite_rule( 'movie/([^/]+)/photos', 'index.php?movie=$matches[1]&photos=yes', 'top' );
	add_rewrite_rule( 'movie/([^/]+)/videos', 'index.php?movie=$matches[1]&videos=yes', 'top' );
}

add_action( 'init', 'prefix_movie_rewrite_rule' );

By this add_rewrite_rule function, we are adding two custom rewrite rules to the URL and setting two query variables to the URL. I am not using add_rewrite_endpoint because I don't want to add this endpoint to every permalink I have. But by only adding this rewrite rule they won't work and WordPress will reject them because it doesn't recognize those query vars we added via our rewrite rule function. We have to tell WordPress to be modest on these query vars.


2. Register Query Vars

function prefix_register_query_var( $vars ) {
	$vars[] = 'photos';
	$vars[] = 'videos';

	return $vars;
}

add_filter( 'query_vars', 'prefix_register_query_var' );

Now, as we've added those query vars to WordPress, it will recognize them and we can take advantage of them. But if you go to the URL movie/{movie-name}/photos, still you'll see that your single-movie.php or single.php templates are taking care of these URLs. If you see 404 error for our custom URL, don't worry, just save your permalinks again.


3. Set the Template

function prefix_url_rewrite_templates() {

	if ( get_query_var( 'photos' ) && is_singular( 'movie' ) ) {
		add_filter( 'template_include', function() {
			return get_template_directory() . '/single-movie-image.php';
		});
	}

	if ( get_query_var( 'videos' ) && is_singular( 'movie' ) ) {
		add_filter( 'template_include', function() {
			return get_template_directory() . '/single-movie-video.php';
		});
	}
}

add_action( 'template_redirect', 'prefix_url_rewrite_templates' );

Here we are saying that, if there is a query var photos or videos and it's a single movie page, we are setting the template as single-movie-image.php and single-movie-video.php respectively.

So, single-movie-image.php is responsible for movie/{movie-name}/photos query and single-movie-video.php is responsible for movie/{movie-name}/videos query. Simple eh?

Note: In the section of setting the page template, we are using PHP anonymous functions. So, this code will need minimum PHP 5.3.0 support or you could use the filters the old way. One thing to remember with using anonymous functions is that, you can't unregister a filter or action as you could do normally. But it's an easy way to bind an action/filter without giving the function a name.
Related Posts
  • Code
    Tips
    New wp-config Tweaks You Probably Don't Know8 new wp config tweaks you probably didnt know about 400
    The wp-config.php file: One of the most loved WordPress feature for some, one of the worst nightmares for others. There are countless tips and tricks in a plethora of articles, and you can't get enough of them; however, this article is aiming to be different. In this article, we're going to get familiar with eight new wp-config tricks that are less known than functionality such as turning off post revisions, increasing the memory limit, or other similar features.Read More…
  • Code
    WordPress
    Quick Tip: Post Types, Taxonomies and PermalinksPost types taxonomies urls
    Custom Post Types and taxonomies are two powerful features of WordPress. Unfortunately, they can have a tendency to cause problems if developers aren't familiar with how permalinks, URLs, and rewriting works within WordPress. In this quick tip, we aim to cover the topic very briefly to make sure you know all you need to know about WordPress URLs, custom post types, taxonomies, and how they all relate.Read More…
  • Code
    Creative Coding
    Using WordPress For Web Application Development: Available Features, Part 6: URL Rewriting (or Routes)Application foundation 400
    One of the nicest things about modern web application development frameworks is that they provide a way to generate really clean routes—or URL schemes—that map to the conceptual model of how the application is structured.Read More…
  • Code
    Creative Coding
    Using WordPress for Web Application Development: Features: SessionsApplication foundation 400
    In this series, we're taking a look at how it's possible to build web applications using WordPress. Thus far, we've talked about how WordPress is a foundation (rather than a framework), its architecture, how we need to conceptually think of it when approaching it especially coming from other languages, and then we began talking about the components that make up a basic web application.Read More…
  • Code
    Creative Coding
    The Rewrite API: Post Types & TaxonomiesPreview
    This is part two of a series looking at WordPress' Rewrite API. In part one we took a whistle stop tour of the basics of WordPress' Rewrite API. In this tutorial we will look at the rewrite settings available to us when registering a post type or taxonomy. While custom post types and taxonomies (unlike the default posts, categories and tags) don't benefit from any Settings -> Permalink interface, setting up rewrites for custom types is still fairly straightforward. We'll also be using the methods introduced in part one, so if you haven't already I recommend you read WordPress' Rewrite API Part One: The Basics.Read More…
  • Code
    Creative Coding
    The Rewrite API: The BasicsPreview
    This is part one of a two part series looking at WordPress' Rewrite API. In this tutorial we look at how rewrites work and the basic methods available to create custom rewrite rules.Read More…