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.


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.

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.