Way to hook into a sidebar call to replace it with a custom sidebar

I have figured this out. The trick is to use the get_sidebar hook and run some conditionals to check if we’re on a CPT page (archive or singular or cpt taxonomy archive) and if the sidebar we’ve hooked into is the one we want to replace ($sidebar == 'content').

If these conditionals are met we unregister sidebar-2 and add our own sidebar. This probably won’t work with a theme that doesn’t have sidebar-2 as the content sidebar.

//Register the alternative sidebar
register_sidebar( array(
    'name'         => __( 'Custom Sidebar' ),
    'id'           => 'cpt-sidebar',
    'description'  => __( 'Sidebar for showing cpt-specific content.' ),
    'before_title' => '<h1 class="widget-title">',
    'after_title'  => '</h1>', 
    'before_widget' => '<aside id="%1$s" class="widget %2$s">',
    'after_widget'  => '</aside>',
    ) );

add_action('get_sidebar','change_cpt_sidebar');
function change_cpt_sidebar($sidebar) {

    if ( (is_post_type_archive('my_cpt') || is_singular('my_cpt') || is_tax('cpt_tax')) && $sidebar == 'content') { // Check if we're on a CPT page
        unregister_sidebar( 'sidebar-2' );
        ?>
        <div id="content-sidebar" class="content-sidebar widget-area" role="complementary">
            <?php
        dynamic_sidebar( 'cpt-sidebar' );
        ?>
    </div>
    <?php
     }
}