Search code examples
wordpressdynamicurl-rewritingpermalinks

Extend Wordpress Permalinks to create Virtual Pages and Folders


I am trying to find a way to make Wordpress extend its permalink structure with virtual folders so that they can be used to load dynamic content (ie. from a database) but keeping the WP header and template system so the my dynamic content can be placed in the layout.

Example: http://www.mydomain.com/customfolder/product1/

Where customfolder and product1 are not pages or categories present in the Wordpress database. Wordpress normally requires that they are created according to that said structure to work, if not it returns a 404 error.

I wish to use /customfolder/ to extend the permalinks with psuedo/virtual pages. The above link would hence be redirected internally to say products.php?product=$product1&template=style1. This way I can use the url to create a unlimited number of pages that loads dynamic content from a database and defining which template to use for that folder.

It should also be possible to setup different permalink folders to load different files, so that /product/map/ will be redirected to ie. map.php

I have tried both .htaccess and the internal WP_rewrite function without success. I always end up getting 404 errors. I know it's possible to do this through Wordpress and I have seen examples that have been close to what I am trying to do but so far I have not come up with a working solution. Here is the best one so far: http://www.binarymoon.co.uk/2010/02/creating-wordpress-permalink-structure-custom-content/

Any help is appreciated!


Solution

  • @csixty4 I managed to work out a solution that does what I wanted to do, more or less. I have not tried any plugins to create virtual pages. What I did was to setup like .htaccess like this:

    RewriteRule ^products/$ /path/mypage.php [L]
    RewriteRule ^products/(help|support)[^/]? /path/mypage.php?type=$1 [L]
    

    And then I created the file(s) I needed and called the WP loader and defined that the active theme should not be loaded so that I can output content where I want it.

    <?php
    define('WP_USE_THEMES', false);
    require("/wp-path/wp-blog-header.php");
    get_header();
    ?>
    
    My custom content goes here
    
    <?php
    get_footer();
    ?>
    

    If anyone have any ideas for improvements or suggestions to make this more powerful I would be glad to hear you out.