Title: Activate fields conditionally
Last modified: September 1, 2016

---

# Activate fields conditionally

 *  Resolved [alexadark](https://wordpress.org/support/users/alexadark/)
 * (@alexadark)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/activate-fields-conditionally/)
 * Hi,
    i am doing a page builder with complex fields groups, each group is a layout.
   i want to put options to activate certain layouts or not, as i will use this 
   plugin to build different themes. so i have made theme options with set
 *     ```
       ontainer::make( 'theme_options', 'Theme Options' )
                ->add_fields( array(
       	         Field::make( 'set', 'crb_active_layouts' )
       	              ->add_options( array(
       		              'text_area'       => 'Text Area',
       		              'slider'          => 'Slider',
       		              'slideshow_panel' => 'Slideshow Panel',
       		              'parallax'        => 'Parallax Area',
       		              'panel_switcher'  => 'Panel Switcher'
       	              ) )
                ) );
       ```
   
 * then retrieve the array like that
    `$active_layouts = carbon_get_theme_option('
   crb_active_layouts');` then i suppose i can make the condition like that
 * if(in_array(‘slider’,$active_layouts){
    //Field registration }
 * i think my logic is good, but not the syntax, perhaps because i am bad with oop
 * [https://wordpress.org/plugins/carbon-fields/](https://wordpress.org/plugins/carbon-fields/)

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Plugin Author [htmlBurger](https://wordpress.org/support/users/htmlburger/)
 * (@htmlburger)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/activate-fields-conditionally/#post-7580881)
 * Hi [@alexadark](https://wordpress.org/support/users/alexadark/),
 * Your approach is great! In my opinion – don’t complicate things with OOP, its
   a simple problem that needs a simple solution.
 * If you have further questions, don’t hesitate to contact us.
 *  Thread Starter [alexadark](https://wordpress.org/support/users/alexadark/)
 * (@alexadark)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/activate-fields-conditionally/#post-7580882)
 * Hi,
    yes but i don’t know how to do it… basically i have this structure `Container::
   make( ‘post_meta’, ‘layouts’ ) ->show_on_post_type( ‘page’ ) ->show_on_template(‘
   builder-page.php’ ) ->add_fields( array( Field::make( ‘complex’, ‘crb_block_layouts’)/***
   TEXT AREA */
 *  ->add_fields( ‘text_area’, array(
    //fields for text area ) ) /** * SLIDER */-
   >add_fields( ‘slider’, array( //fields fore slider ))`
 * then i have the functions to displays them in another files
 * if for exemple i don’t want to activate the slider how can i write it ?
    because
   if i write a conditional arround it gives me a php error
 * \`Container::make( ‘post_meta’, ‘layouts’ )
    ->show_on_post_type( ‘page’ ) ->
   show_on_template( ‘builder-page.php’ ) ->add_fields( array( Field::make( ‘complex’,‘
   crb_block_layouts’ ) /** * TEXT AREA */
 *  ->add_fields( ‘text_area’, array(
    //fields for text area ) ) if(//condition){/***
   SLIDER */ ->add_fields( ‘slider’, array( //fields fore slider )) }`
 * but i don’t know how to write my condition, as i say it is a syntax problem
 *  Plugin Author [htmlBurger](https://wordpress.org/support/users/htmlburger/)
 * (@htmlburger)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/activate-fields-conditionally/#post-7580883)
 * [@alexadark](https://wordpress.org/support/users/alexadark/), you can make a 
   field builder function that populates the complex field with the needed groups.
   Maybe something like:
 *     ```
       Container::make( 'post_meta', 'layouts' )
       	->show_on_post_type( 'page' )
       	->show_on_template( 'builder-page.php' )
       	->add_fields( array(
       		crb_get_layouts_complex_field( 'crb_block_layouts' )
       	) );
   
       function crb_get_layouts_complex_field( $name ) {
       	$active_layouts = (array) carbon_get_theme_option('crb_active_layouts');
   
       	$complex = Field::make( 'complex', $name );
   
       	$fields = array(
       		'text_area' => array(
       			// fields for text area
       		),
   
       		'slider' => array(
       			// fields for slider
       		),
       	);
   
       	foreach ($active_layouts as $layout) {
       		if ( empty( $fields[$layout] ) ) {
       			continue;
       		}
   
       		$complex->add_fields( $layout, $fields[$layout] );
       	}
   
       	return $complex;
       }
       ```
   
 *  Thread Starter [alexadark](https://wordpress.org/support/users/alexadark/)
 * (@alexadark)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/activate-fields-conditionally/#post-7580884)
 * Thank you !
    i am going to try !
 *  Thread Starter [alexadark](https://wordpress.org/support/users/alexadark/)
 * (@alexadark)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/activate-fields-conditionally/#post-7580885)
 * It works !

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Activate fields conditionally’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/carbon-fields_cacbcc.svg)
 * [Carbon Fields](https://wordpress.org/plugins/carbon-fields/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/carbon-fields/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/carbon-fields/)
 * [Active Topics](https://wordpress.org/support/plugin/carbon-fields/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/carbon-fields/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/carbon-fields/reviews/)

## Tags

 * [conditionals](https://wordpress.org/support/topic-tag/conditionals/)

 * 5 replies
 * 2 participants
 * Last reply from: [alexadark](https://wordpress.org/support/users/alexadark/)
 * Last activity: [9 years, 10 months ago](https://wordpress.org/support/topic/activate-fields-conditionally/#post-7580885)
 * Status: resolved