Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to setup a "Couch Mode" (distraction free reading) on my WordPress blog as laid out in this article.

The guide in the article follows these steps:

  1. Create a new page in WordPress with slug read
  2. Add the following rewrite rule at the top of the .htaccess file:

    RewriteRule ^read/([0-9]+)/?$ /read/?u=$1 [QSA,L]

  3. Save this file as page-read.php

  4. Add this link to to single.php of my theme using <?php the_ID(); ?>

I did everything as per the article but when I click the "Read story without the clutter" link on my posts it throws an error:

Not Found

The requested URL /read/761/ was not found on this server.

My local development wordpress installation is: localhost/techylab/

While the post where I am trying this Couch Mode is: localhost/techylab/761/something

share|improve this question
1  
If you're getting your server's 404, that means you got your rewrite rule wrong. That article is a bad example of how to accomplish this, try looking around this site for some examples using add_rewrite_endpoint. –  Milo Apr 2 '13 at 14:33
    
@Milo Sorry to say but I am not able to figure out what's wrong with the rewrite rule here. I do not know much about them. RewriteRule ^read/([0-9]+)/?$ /read/?u=$1 [QSA,L] Also FYI when I am opening the page with "read" slug, it opens with new page template. –  Anurag Apr 2 '13 at 18:50
add comment

1 Answer 1

up vote 2 down vote accepted

Here's a method to achieve what you want internally without adding anything to the .htaccess file.

It works by adding a rewrite endpoint named read, so any single post permalink with read appended to the end will have a different single post template applied.

For example, if your normal single post permalink is:

localhost/techylab/some-post-title/

The alternate version will be available at:

localhost/techylab/some-post-title/read/  

First, add the rewrite endpoint. This goes in your theme's functions.php:

function wpa_read_endpoint(){
    add_rewrite_endpoint( 'read', EP_PERMALINK );
}
add_action( 'init', 'wpa_read_endpoint' );

After adding this, visit your permalinks settings page in admin to flush the rewrite rules so this endpoint will be added to the rules.

Next, filter the single post template and check for the presence of the read query var. If it exists, load the template within the theme folder named read.php. This is your simplified post template with whatever markup and template tags you want. The correct post has already been queried, so no need for a special query to load the post via the ID as in the version you posted, the normal loop will work just like in any other template.

function wpa_read_template( $template = '' ) {
    global $wp_query;
    if( ! array_key_exists( 'read', $wp_query->query_vars ) ) return $template;

    $template = locate_template( 'read.php' );
    return $template;
}
add_filter( 'single_template', 'wpa_read_template' );
share|improve this answer
    
Thanks for taking the effort to suggest an alternate for this. But I have some confusions: 1. I added the rewrite endpoint in functions.php and flushed the rewrite rules. Now would my .htaccess file contain any extra rules in it? Other than the usual default of Wordpress? 2. What do you mean by check the presence of read query? Did you mean <?php the_content('Read more &raquo;'); ?> ? 3. The code that you had mentioned at the end, where will this go in my single.php ? I mean I will still need to give a hyperlink like localhost/techylab/something/read right? –  Anurag Apr 3 '13 at 13:50
1  
WordPress has its own internal rewrites, which this is a part of, so you won't see anything change in .htaccess. No changes to that file are needed for this to work. The second bit of code is what is checking if read exists in the request, that also goes in your functions.php file. It's hooked to a filter that runs when WordPress is loading the template for a single post, and overrides the normal template selection to load your special template. You will still need to manually append /read/ to the end of any post permalink you want viewed via your read.php template. –  Milo Apr 3 '13 at 15:13
    
thanks for the clarification again. I did exactly as told. Now when I am manually appending /read/ to the end of any single post url, it is being redirected to the post with id = 1 (usually Hello World). Am I missing something here again? –  Anurag Apr 4 '13 at 17:08
    
it's difficult to say what's going on. if you visited the permalinks page after updating, it should work as-is. I've pasted this straight into an unedited twenty twelve theme with no plugins and it does what it's supposed to. –  Milo Apr 5 '13 at 17:02
1  
I just duplicated single.php and renamed it. the query and custom loop in the template you linked is not necessary with this method, as WordPress already does the query automatically before the template is loaded, you just need the default vanilla loop. –  Milo Apr 5 '13 at 18:11
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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