What is the best way to output plugin result in certain url

There are two steps:

function my_plugin_rewrite_rule() {
  global $wp;

  $wp->add_query_var( 'show_hello_world' );
  add_rewrite_rule( 'show-hello-world/?$', 'index.php?show_hello_world=1', 'top' );
}
add_action( 'init', 'my_plugin_rewrite_rule' );

That takes care of rewriting. Remember to flush the rewrite rules.

Now, your plugin can check for get_query_var( 'show_hello_world' ); and load a certain file:

function my_plugin_template( $path ) {
 if ( get_query_var( 'show_hello_world' ) )
    return locate_template( 'my-plugin.php' );
  else
    return $path;
}
add_filter( 'template_include', 'my_plugin_template' );

Leave a Comment