show a specific metabox dependent on the page template

For a few days I am trying to show a specific metabox dependent on the page template. I can do it but my problem is when I have selected a page template and hit the update button then the metabox does not show. metabox is showing when I reload the page. Actually, I want when I will hit the update button then the metabox will be shown. please help me. Note: I use CMB2 for meta boxes. I tried this way

function cmb2_add_pricingtable() {

    $prefix = '_next_pt_';

    $cmb = new_cmb2_box( array(
        'id'           = $prefix . 'pricing_table',
        'title'        = __( 'Pricing Table', 'next' ),
        'object_types' = array( 'page' ),
        'context'      = 'normal',
        'priority'     = 'high',
        'show_names'    = true,
    ) );

    $group=$cmb-add_field( array(
        'name' = __( 'Pricing Table', 'next' ),
        'id' = $prefix . 'pricing_table',
        'type' = 'group',
    ) );
    $cmb-add_group_field($group, array(
        'name' = __( 'Caption', 'next' ),
        'id' = $prefix . 'price_caption',
        'type' = 'text',
        
    ) );

    $cmb-add_group_field($group, array(
        'name' = __( 'Price Option', 'next' ),
        'id' = $prefix . 'price_option',
        'type' = 'text',
        'repeatable'=true,
    ) );
    $cmb-add_group_field($group, array(
        'name' = __( 'Price', 'next' ),
        'id' = $prefix . 'price',
        'type' = 'text',
        
    ) );

}

$post_id = null;
if(isset($_REQUEST['post']) || isset($_REQUEST['post_ID'])){
    $post_id = empty($_REQUEST['post_ID']) ? $_REQUEST['post'] : $_REQUEST['post_ID'];  
}

$pageTemplate=get_post_meta( $post_id, '_wp_page_template', true );

if('price-page-template.php'==$pageTemplate){
    add_action( 'cmb2_init', 'cmb2_add_pricingtable' ); 

}

Topic page-template metabox custom-field Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.