[wp-hackers] Showing custom page types
Chris Jean
gaarai at gaarai.com
Thu Jan 29 18:48:08 GMT 2009
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/
More information about the wp-hackers
mailing list