WP Customize Control Hide and Show Controls With a Radio Button in loop

$wp_customize->add_section(‘demo_home_product’, array(
‘title’ => __(‘Home Products’, ‘demo’),
‘panel’ => ‘demo_home_options’
));

$wp_customize->add_setting(‘demo_home_product_header’, array(
‘default’ => ‘New Product Information’,
‘capability’ => ‘edit_theme_options’,
‘sanitize_callback’ == ‘esc_url_raw’,
‘transport’ => ‘refresh’
));

$wp_customize->add_control(‘demo_home_product_header’, array(
‘label’ => __(‘Products section header’, ‘demo’),
‘section’ => ‘demo_home_product’,
‘settings’ => ‘demo_home_product_header’,
//’description’ => __(‘Default 6 video display in page’, ‘demo’),
‘type’ => ‘text’,
)
);

$wp_customize->add_setting(‘demo_home_product_count’, array(
‘default’ => ‘8’,
‘sanitize_callback’ => ‘esc_textarea’,
‘transport’ => ‘refresh’,
‘capability’ => ‘manage_options’
));
$wp_customize->add_control( new WP_Customize_Control( $wp_customize ,’demo_home_product_count’, array(
‘label’ => __(‘Select Product Count’, ‘demo’),
‘section’ => ‘demo_home_product’,
‘settings’ => ‘demo_home_product_count’,
‘type’ => ‘text’,
)));

$wp_customize->add_setting( ‘demo_home_products_artical_link_target’, array(
‘default’ => ‘0’,
‘sanitize_callback’ => ‘demo_sanitize_checkbox’,
‘transport’ => ‘refresh’,
)
);
$wp_customize->add_control( ‘demo_home_products_artical_link_target’, array(
‘label’ => ‘Open link in a new tab’,
‘section’ => ‘demo_home_product’,
‘settings’ => ‘demo_home_products_artical_link_target’,
‘type’ => ‘checkbox’,
)
);

function choice_a_callback( $control ) {
if ( $control->manager->get_setting(‘demo_radio_control1’)->value() == ‘a’ ) {
return true;
} else {
return false;
}
var_dump( ‘choice_a_callback’ );
}

function choice_b_callback( $control ) {
if ( $control->manager->get_setting(‘demo_radio_control1’)->value() == ‘b’ ) {
return true;
} else {
return false;
}
}
function choice_callback( $control ) {
$radio_setting = $control->manager->get_setting(‘demo_radio_control1’)->value();
$control_id = $control->id;
if ( $control_id == ‘demo_home_products_artical_slug1’ && $radio_setting == ‘a’ ) return true;
if ( $control_id == ‘home_products_artical_pdf_link1’ && $radio_setting == ‘b’ ) return true;
return false;
}

$home_product_count = get_theme_mod(‘demo_home_product_count’,’4′);

for($p = 1; $p <= $home_product_count; $p++){

$wp_customize->add_setting(‘home_product_images’.$p, array(
‘transport’ => ‘refresh’,
‘sanitize_callback’ => ‘absint’,
‘default’ => get_template_directory_uri().’/images/banner’.$p.’.jpg’,
//’height’ => 50,
));

$wp_customize->add_control( new WP_Customize_Cropped_Image_Control( $wp_customize, ‘home_product_images’.$p, array(
‘label’ => ‘Card Image #’.$p,
‘section’ => ‘demo_home_product’,
‘settings’ => ‘home_product_images’.$p,
‘width’ => 400,
‘height’ => 300,
‘flex_width’ => false,
‘flex_height’ => false,
)));

$wp_customize->add_setting( ‘demo_home_products_artical_tittle’.$p, array(
‘default’ => ‘Product name’.$p,
‘sanitize_callback’ => ‘esc_textarea’,
‘transport’ => ‘refresh’,
‘capability’ => ‘manage_options’
)
);

// $wp_customize->add_setting( ‘demo_home_products_artical_link_target’.$p, array(
// ‘default’ => ‘0’,
// ‘sanitize_callback’ => ‘demo_sanitize_checkbox’,
// ‘transport’ => ‘refresh’,
// //’capability’ => ‘manage_options’
// )
// );
// $wp_customize->add_control( ‘demo_home_products_artical_link_target’.$p, array(
// ‘label’ => ‘Open link in a new tab’,
// ‘section’ => ‘demo_home_product’,
// ‘settings’ => ‘demo_home_products_artical_link_target’.$p,
// ‘type’ => ‘checkbox’,
// )
// );

$wp_customize->add_setting( ‘demo_radio_control’.$p, array(
‘default’ => ‘a’,
) );

$wp_customize->add_control( ‘demo_radio_control’.$p, array(
‘label’ => ‘radio_control’,
‘section’ => ‘demo_home_product’,
‘settings’ => ‘demo_radio_control’.$p,
‘type’ => ‘radio’,
‘choices’ => array(
‘a’ => ‘Choice A’,
‘b’ => ‘Choice B’,
),
)
);

$wp_customize->add_setting( ‘demo_home_products_artical_slug’.$p, array(
‘default’ => ‘link path’.$p,
‘transport’ => ‘refresh’,
)
);
$wp_customize->add_control( ‘demo_home_products_artical_slug’.$p, array(
‘label’ => ‘Product Link path’.$p,
‘section’ => ‘demo_home_product’,
‘settings’ => ‘demo_home_products_artical_slug’.$p,
‘type’ => ‘text’,
‘active_callback’ => ‘choice_a_callback’,
)
);

// = File Upload =
// =============================
$wp_customize->add_setting(‘home_products_artical_pdf_link’.$p, array(
‘default’ => ‘#!’.$p,
‘capability’ => ‘edit_theme_options’,
// ‘sanitize_callback’ => ‘absint’,
));
$wp_customize->add_control( new WP_Customize_Upload_Control($wp_customize, ‘home_products_artical_pdf_link’.$p, array(
‘label’ => __(‘Atach Pdf – ‘.$p, ‘themename’),
‘section’ => ‘demo_home_product’,
‘settings’ => ‘home_products_artical_pdf_link’.$p,
‘active_callback’ => ‘choice_b_callback’,
// ‘active_callback’ => function( $control ) {
// $control->get_setting(‘demo_radio_control’.$p )->value() == ‘b’;
// },
)));

$wp_customize->add_setting( ‘demo_home_products_artical_link_text’.$p, array(
‘default’ => ‘Read More’,
‘sanitize_callback’ => ‘esc_textarea’,
‘transport’ => ‘refresh’,
‘capability’ => ‘manage_options’
)
);
$wp_customize->add_control( ‘demo_home_products_artical_link_text’.$p, array(
‘label’ => ‘Products card Link’.$p.’ Text’,
‘section’ => ‘demo_home_product’,
‘settings’ => ‘demo_home_products_artical_link_text’.$p,
‘type’ => ‘text’,
)
);
}

Read more here:: WP Customize Control Hide and Show Controls With a Radio Button in loop

Leave a Reply

Your email address will not be published. Required fields are marked *