Your should add rewrite endpoint template on theme activation hook and init hook. Also there is some little checks to perform to prevent errors.
function sjc_theme_activate(){
sjc_theme_add_rewrite_endpoint();
flush_rewrite_rules();
}
//for more info http://codex.wordpress.org/Plugin_API/Action_Reference/after_switch_theme
add_action( 'after_switch_theme', 'sjc_theme_activate' );
function sjc_theme_deactivate(){
flush_rewrite_rules();
}
//for more info http://codex.wordpress.org/Plugin_API/Action_Reference/switch_theme
add_action( 'switch_theme', 'sjc_theme_deactivate' );
function sjc_add_query_vars($vars) {
$vars[] = 'template';
return $vars;
}
add_filter('query_vars', 'sjc_add_query_vars');
function sjc_template($template) {
global $wp_query;
//you should check if file exists first
if ( (isset($wp_query->query_vars['template'])) && ($wp_query->query_vars['template'] == 'basic') && (file_exists(dirname( __FILE__ ) . '/single-basic.php')) ) {
return dirname( __FILE__ ) . '/single-basic.php';
}else {
return $template;
}
}
add_filter('single_template', 'sjc_template');
//add rewrite endpoint
function sjc_theme_add_rewrite_endpoint(){
add_rewrite_endpoint( 'template', EP_PERMALINK | EP_PAGES );
}
//for more info http://codex.wordpress.org/Rewrite_API/add_rewrite_endpoint
add_action('init', 'sjc_theme_add_rewrite_endpoint');
then Activate pretty links for wordpress. You can visit any post with
http://example.com/post-title/?template=basic
http://example.com/post-title/template/basic
And get the same result (don’t forget to add single-basic.php file in theme base dir). I hope this will help you.