Programmatically set page template based on page ID

I needed to do something very similar for one of the plugins that I was developing. I registered two separate cpt’s during plugin activation, and each of those cpt’s should use a custom single template that I had bundled with the plugin code base.

You should be able to do the same for page templates, using the page_template filter.

Here is my function using the single_template filter, which can be adjusted to use page_template instead. You’ll simply need to grab the global $post variable so you can check the post type or the post ID and then serve a template based on that data.

add_filter( 'page_template', 'custom_page_template' );
public function custom_page_template( $page_template ) {
        global $post;

        switch ( $post->ID) {

            default :
            case '2' : // page ID 2
                if( file_exists( get_stylesheet_directory() . '/single-event-template.php' ) ) {
                    $page_template = get_stylesheet_directory() . '/single-event-template.php'; 
                return $single_template;
            break;

            case '14' : // page ID 14
                $page_template= plugin_dir_path( dirname( __FILE__ ) ) . 'public/partials/single-event-location-template.php';
                return $page_template;
            break;

        } // end switch

}

I haven’t tested this using page templates, but from the codex page this seems like it should work.

Working off your example, you may want to try something similar to the following:

add_filter( 'page_template', 'my_function' );
function my_function( $page_template ){
  global $post;
  $page_id = $post->ID;
  if ( $my_options['my-reg-page'] == $page_id ) {
    $page_template = get_template_directory() . '/reg-page.php'; 
  } else {
   $page_template = get_template_directory() . '/some-other-page-template.php'; 
  } 
  return $page_template;
}