[wp-hackers] Showing custom page types

Mike Schinkel mikeschinkel at newclarity.net
Thu Jan 29 21:00:36 GMT 2009


Chris:

I have exactly the same needs.  URL management and related content is a real pain in WP (unlike Drupal which is easy and from whence I came.) I've been meaning to write about it but am glad someone else better known on the list has decided to tackle the issue first.

I've identified that /wp-content/template-loader.php doesn't provide any reasonable way to let someone call custom code off a custom URL.

I'd love to work with you on this. It's probably what I see as WordPress' biggest limitation for my own needs followed closely by lack of custom post types.
 
-Mike Schinkel
http://mikeschinkel.com/

P.S. I've also come to the conclusion that requiring the use of regex for URL mapping greatly reduces the number of people who can approach using WordPress as a CMS.  I'd really like to see URL Templates used as for the 80% cases and then regex could be used for the more advanced cases.  For example, if I wanted to create a list of companies, I'd like to be able to specify the URLs like this:

   /companies/
   /companies/{company}/
   /companies/{company}/overview/
   /companies/{company}/details/

I've done some work in that area at http://code.google.com/p/simpleurlmapper/



----- Original Message -----
From: "Chris Jean" <gaarai at gaarai.com>
To: wp-hackers at lists.automattic.com
Sent: Thursday, January 29, 2009 1:48:08 PM GMT -05:00 US/Canada Eastern
Subject: [wp-hackers] Showing custom page types

I'm working on a new project that uses WordPress as a framework rather 
than as just a blogging platform. I want to be able to have custom URL 
structures, such as /tool/option, that will end up passing variables to 
a set of code to render the page contents rather than one of the theme 
template files rendering the content.

I have it working, but I feel like there is a better way to do this. I 
want to run what I've done by this list to see if anyone has a better or 
more standard way of doing this.

    /* Part of plugin code pertaining to this discussion */

    add_action( 'init', 'add_rewrite_rules' );
    add_action( 'template_redirect', 'load_custom_page_setup' );

    function load_custom_page_setup() {
        $tool = get_query_var( 'tool' );
       
        if ( ! empty( $tool ) ) {
            add_action( 'render_custom', 'run_tool' );
        }
    }

    function add_rewrite_rules() {
        // This should only be run when the plugin is first initialized
    or when the
        // rules are flushed by some other process.
       
        global $wp, $wp_rewrite;
       
        $wp_rewrite->add_rule( 'lookup/?$', 'index.php?tool=lookup',
    'top' );
        $wp_rewrite->add_rule( 'lookup/([^/]+)/?$',
    'index.php?tool=lookup&option1=$matches[1]', 'top' );
        $wp_rewrite->add_rule( 'lookup/([^/]+)/([^/]+)/?$',
    'index.php?tool=lookup&option1=$matches[1]&option2=$matches[2]',
    'top' );
       
        // Quick hack just to make this work while I make changes
        $wp_rewrite->flush_rules();
       
        $wp->add_query_var( 'tool' );
        $wp->add_query_var( 'option1' );
        $wp->add_query_var( 'option2' );
    }

    function run_tool() {
        $tool = get_query_var( 'tool' );
       
        if ( ! empty( $tool ) ) {
            switch( $tool ) {
                case 'lookup':
                    run_lookup();
                    break;
            }
        }
    }

    if ( ! function_exists( 'is_custom' ) ) {
        function is_custom() {
            $lookup = get_query_var( 'lookup' );
           
            if ( ! empty( $lookup ) )
                return true;
            return false;
        }
    }

    /* End plugin code */


    /* Modification added home.php or index.php file depending on theme
    setup */

    <?php if ( ! function_exists( 'is_custom' ) || ! is_custom() ) : ?>
        /* Standard theme code/markup here */
    <?php else : ?>
        <?php do_action( 'render_custom' ); ?>
    <?php endif; ?>

    /* End theme modification */


Basically, I've found the only way to insert this custom output into a 
theme is by modifying the theme itself since there isn't a specification 
for custom output that fills in a blank template location.

I could create a page template, but that would either require passing 
the variables in a query string or creating a page for each possible 
combination of URL "paths" and manually parsing the URL in the code to 
derive the variables.

Ideally, I'd like there to be a way to keep all this logic and code 
completely within the plugin and not require the theme to be modified. 
As far as I can tell, the only way to make this standard is to add a new 
conditional tag, such as is_custom, and add to the template spec a new 
template file, such as custom.php, that is nothing more than a theme 
file that calls an action in place of content.

Just so you know of a couple of different ways that I tried out, I've 
done the following:

    * Used the template_redirect hook to check for my custom vars. If
      they are set, set the $wp_query->is_robots variable to true, clear
      out the do_robots action hook, and add my own function to the
      do_robots action. This actually works really well with one big
      caveat: there isn't a template. So, this method could work well if
      you don't require any of the theme template.
    * I started to journey down the road of trying to latch into the
      is_single process so that I could take advantage of the relatively
      simple setup that most single.php template files have. This would
      require me to setup my own title, content, etc variables when
      the_post is called. At first, I thought that this was a promising
      road, but it's really a minefield as it would require me to jump
      through hurdles of getting successful queries, faking and then
      replacing many internal variables, and that's just the tip of the
      iceberg.

Please share any thoughts or ideas that you have. If there is a way to 
do this within the current WordPress structure, I'd love to know about it.

-- 
Chris Jean
http://gaarai.com/
http://wp-roadmap.com/

_______________________________________________
wp-hackers mailing list
wp-hackers at lists.automattic.com
http://lists.automattic.com/mailman/listinfo/wp-hackers


More information about the wp-hackers mailing list