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.

Okay, let's assume that I want to create a dynamic "printable" page using a system that doesn't play well with wordpress's theming engine, is there a way for me to define a "permalink" in my plugin that will call a predefined function, also in my plugin?

If you're familiar with Drupal it's similar to how their hook_menu works, but I only need it on a very limited basis, so it doesn't have to be that robust.

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

Something along the lines of this would probably do it:

function wpse21372_init(){
  add_rewrite_rule( 'your-page-regex/?$', 'index.php?wpse21372=1', 'top' );
  add_rewrite_tag( '%wpse21372%', '([^&]+)' );
}

add_action( 'wp', 'wpse21372_wp' );

function wpse21372_wp( $wp ){
  if( isset( $wp->query_vars['wpse21372'] ) && !empty( $wp->query_vars['wpse21372'] ) ){
    //You're on your custom page.
    //you may want to exit page
    //execution when you're done
    //so the rest of WordPress'
    //normal execution doesn't take
    //over from here.
  }
}

After you're done adding that code, make sure you flush the rewrite rules by going to Settings -> Permalinks.

share|improve this answer
    
Looks good, I'm going to test it out and let you know, thanks –  Trey Jun 29 '11 at 0:55
    
Can't get the rewrite rule to work, but that's fine, no end user will ever see this query string, and just checking the query for the appropriate value using the wp action hook works great, thanks again! –  Trey Jun 29 '11 at 1:04
    
actually, I had to use add_rewrite_tag('%my_var%','([^&]+)'); for it to work btw:) –  Trey Jun 29 '11 at 1:12
    
Of course! I'll add it to my answer so we have it all together. –  John P Bloch Jun 29 '11 at 1:17
add comment

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.