Creating a path in a plugin to call a function

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.

Related posts

Leave a Reply

1 comment

  1. 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.