0

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 Dec 5 '13 at 16:14
1

The 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

  1. When a person goes to a URL on your site, the .htaccess redirects all requests, that don't match an actual file, to index.php to kick off the whole WordPress process.
  2. WordPress finds a match for the URL path to the rewrite rules using regular expressions.
  3. 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.
  4. The query string is then parsed into the query vars which are defined variables. It will also fill in any implied vars.
  5. Then the default WP_Query object that is used in the main loop is created based upon the query vars.
  6. 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' );
0

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/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.