I am new to Wordpress so I don't really know how the rewrite rules should look like. My problem is that I have a custom template in my theme called portfolio-inner.php
My rule looks something like that ['portfolio/php/wordpress'] => 'portfolio-inner/?galid=3'
.
The problem is that all the examples show an index.php
URL as the destination, but I don't know how to build the index.php
URL for my template URL.
As a result, that rule shows the 404 page.
As a workaround, I edited the .htaccess
and added this:
Redirect /portfolio/php/wordpress /?galid=3
and I see the right gallery page, but that's not OK, because the browser's address bar changes and it should remain /portfolio/php/wordpress
I am new to Wordpress so I don't really know how the rewrite rules should look like. My problem is that I have a custom template in my theme called portfolio-inner.php
My rule looks something like that ['portfolio/php/wordpress'] => 'portfolio-inner/?galid=3'
.
The problem is that all the examples show an index.php
URL as the destination, but I don't know how to build the index.php
URL for my template URL.
As a result, that rule shows the 404 page.
As a workaround, I edited the .htaccess
and added this:
Redirect /portfolio/php/wordpress http://example.com/portfolio-inner/?galid=3
and I see the right gallery page, but that's not OK, because the browser's address bar changes and it should remain /portfolio/php/wordpress
- You can't rewrite requests directly to template files, that's not how WordPress works. Rewrite rules typically point to a WordPress object of some sort - a page, an archive, a post, etc., via query arguments appended to the main index.php file, which then loads a template file based on the type of request, following the template hierarchy. – Milo Commented Dec 5, 2013 at 16:14
2 Answers
Reset to default 1The index.php
you see is not the template file. It is pointed to index.php
file you see in the root of your WordPress installation.
How does the pretty URL translate into a template file
- When a person goes to a URL on your site, the
.htaccess
redirects all requests, that don't match an actual file, toindex.php
to kick off the whole WordPress process. - WordPress finds a match for the URL path to the rewrite rules using regular expressions.
- When a match is found, WordPress converts the regular expression for the rule into a query string. If you disable pretty URLs, WordPress will use the same query strings in the URLs.
- The query string is then parsed into the query vars which are defined variables. It will also fill in any implied vars.
- Then the default
WP_Query
object that is used in the main loop is created based upon the query vars. - Then WordPress determines which template to load from the theme base upon the query vars or an override using the
template_redirect
hook.
How do you override this
Without fully knowing what you are trying to do, I'm going to answer your question as is. There are probably optimizations for what you are doing.
We will add the custom rewrite rule. You will need to update your permalinks to apply the changes.
function my_custom_rewrite_rules( $rewrite ) {
$rewrite->rules['portfolio/php/wordpress'] = 'index.php?my_portfolio=1&galid=3';
}
add_action( 'generate_rewrite_rules', 'my_custom_rewrite_rules' );
Because we are using a custom query var my_portfolio
, we need to register it with WordPress.
function my_custom_query_vars( $vars ) {
$vars[] = 'my_portfolio';
return $vars;
}
add_filter( 'query_vars', 'my_custom_query_vars' );
Now we can tell WordPress to use our custom template when our query var my_portfolio
is true. To use our template, we use locate_template
to see if the template exists and then call it. If the template exists, it returns true
to tell it was found.
function my_custom_template_redirect() {
global $wp_query;
if ( $wp_query->get( 'my_portfolio' ) && locate_template( 'portfolio-inner.php', true ) ) {
exit;
}
}
add_action( 'template_redirect', 'my_custom_template_redirect' );
I ended up adding a rewrite rule to a page the uses my template. Also, I had to add galid as a rewrite endpoint for EP_PAGES. This tutorial helped a lot: http://wp.tutsplus.com/tutorials/creative-coding/the-rewrite-api-the-basics/