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 Re-Write Page URL's so that they END with .htm

I was able to do this using...

/* Add .htm extension to Page URL Links */
add_action('init', 'htm_page_permalink', -1);
function htm_page_permalink() {
    global $wp_rewrite;
    if ( !strpos($wp_rewrite->get_page_permastruct(), '.htm')){
        $wp_rewrite->page_structure = $wp_rewrite->page_structure . '.htm';
    }
}

/* Remove the trailing slash/ on Pages */
add_filter('user_trailingslashit', 'no_page_slash',66,2);
function no_page_slash($string, $type){
    global $wp_rewrite;
    if ($wp_rewrite->using_permalinks() && $wp_rewrite->use_trailing_slashes==true && $type == 'page'){
        return untrailingslashit($string);
    }else{
        return $string;
    }
}

But then I ran into a problem where I am using my Blog post list on a Page named Blog. I need this page to NOT end with .htm

So I then used this code to change the Page Links to filter out the .htm on my blog page...

/* Filter .htm out of Blog Page Links */
function blog_page_url($content) {
    $current_path = '/blog.htm';
    $new_path = '/blog/';
    $content = str_replace($current_path, $new_path, $content);
    return $content;
}
add_filter('_get_page_link', 'blog_page_url');

And I used this Re-write rule to make /blog/ load the Blog Page ...

/* Setup Custom Re-write for the Blog Page */
add_action( 'init', 'blog_page_rewrite' );
function blog_page_rewrite(){
    add_rewrite_rule('blog', 'index.php?pagename=blog','top' );
    //add_rewrite_rule('blog/(.*?)/', 'index.php?p=$matches[1]','top' );
}

But now I have the problme that any URL with the /blog/ in it is showing the Blog page.

This means my actual blog post which are set up in WP Permalink settings like this...

/blog/%postname%/ are not working, instead they always show the Blog page which list all the blog posts.

In my blog_page_rewrite() function above you can see I had tested an extra re-write rule to hopefully work with the Blog posts but it had no affect.

share|improve this question

1 Answer 1

Is your Blog page the one that's set in the back end at Settings > Reading > Posts page? If so, try checking for that in your first function:

/* Add .htm extension to Page URL Links */
add_action('init', 'htm_page_permalink', -1);
function htm_page_permalink() {
    global $post;
    if( get_option( 'page_for_posts' ) == $post->ID ) {
         // don't add .htm to this page
         return;
    }
    global $wp_rewrite;
    if ( !strpos($wp_rewrite->get_page_permastruct(), '.htm')){
        $wp_rewrite->page_structure = $wp_rewrite->page_structure . '.htm';
    }
}

The Option Reference pointed me at page_for_posts.

share|improve this answer
    
I actually just got it all working with my current solution by changing my add_rewrite_rule paramters from top to bottom but I will also investigate what you have here and see how it works as well, thanks –  jasondavis Apr 26 '13 at 23:51
    
Just letting you know it doesn't work, I appreciate the attempt and in theory it really looks like it should work –  jasondavis Apr 26 '13 at 23:58
    
That's strange. Oh well, you got it working another way... –  Pat J Apr 27 '13 at 17:02

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.