# Course Builder Custom Fields

Course Builder Custom Fields help to customize Course Builder by adding fields like:

* **input** (text, number, radio, checkbox, date, ..)
* **textarea**
* **select**

### Registering Custom Fields <a href="#registering-custom-fields" id="registering-custom-fields"></a>

Course, Lesson, and Quiz Custom Fields could be added using filters:

* `masterstudy_lms_course_custom_fields`
* `masterstudy_lms_lesson_custom_fields`
* `masterstudy_lms_quiz_custom_fields`

Example:

```php
function masterstudy_lms_custom_fields( $custom_fields ) {
	$quiz_custom_fields = array(
		array(
			'type'    => 'text',
			'name'    => 'my-text-field',
			'label'   => __( 'Text field', 'masterstudy-lms-learning-management-system' ),
		),
		...
	);

	return array_merge( $custom_fields, $quiz_custom_fields );
}

add_filter( 'masterstudy_lms_course_custom_fields', 'masterstudy_lms_custom_fields' );
```

### How to filter custom fields before sending to Course Builder <a href="#filtering-custom-fields-before-sending-to-course-builder" id="filtering-custom-fields-before-sending-to-course-builder"></a>

Course Builder Custom Fields could be filtered before sending to Course Builder using `masterstudy_lms_course_builder_custom_fields` filter.

**Example:**

```php
function masterstudy_lms_filter_custom_fields( $custom_fields, $post_id ) {
  ... // Here Custom Fields could be manipulated 
  
  return $custom_fields;
}

add_filter( 'masterstudy_lms_course_builder_custom_fields', 'masterstudy_lms_filter_custom_fields', 10, 2 );
```

### Processing Input Data <a href="#processing-input-data" id="processing-input-data"></a>

All input data will be automatically saved to **post\_meta**. Additionally, data can be used with action:

```php
// Some codedo_action( 'masterstudy_lms_custom_fields_updated', int $post_id, array $data )
```

**Parameters:**

* `$post_id` *int*\
  Course, Lesson, or Quiz Post ID.
* `$data` *array*\
  \&#xNAN;*key => value* array of Custom Fields

**Example:**

```php
function masterstudy_lms_after_custom_fields_updated( $post_id, $data ) {
  ... // Here is your code 
}

add_filter( 'masterstudy_lms_custom_fields_updated', 'masterstudy_lms_after_custom_fields_updated', 10, 2 );
```

## Custom Field Types <a href="#custom-field-types" id="custom-field-types"></a>

There are text, textarea, number, checkbox, radio and select types of custom fields. Let's look at examples of each.

### Text <a href="#text" id="text"></a>

```php
array(
	'type'        => 'text',
	'name'        => 'my-text-field',
	'label'       => __( 'Text field', 'masterstudy-lms-learning-management-system' ),
	'default'     => '', // Optional
	'required'    => true, // Optional
	'custom_html' => '<a href="#">Custom Link</a>', // Optional
)
```

### Textarea <a href="#textarea" id="textarea"></a>

```php
array(
	'type'     => 'textarea',
	'name'     => 'my-textarea-field',
	'label'    => __( 'Textarea field', 'masterstudy-lms-learning-management-system' ),
	'default'  => '', // Optional
	'required' => true, // Optional
)
```

### Number <a href="#number" id="number"></a>

```php
array(
	'type'        => 'number',
	'name'        => 'my-number-field',
	'label'       => __( 'Number field', 'masterstudy-lms-learning-management-system' ),
	'default'     => 10, // Optional
	'required'    => true, // Optional
	'custom_html' => '<a href="#">Custom Button</a>', // Optional
)
```

### Checkbox <a href="#checkbox" id="checkbox"></a>

```php
array(
	'type'        => 'checkbox',
	'name'        => 'my-checkbox-field',
	'label'       => __( 'Checkbox field', 'masterstudy-lms-learning-management-system' ),
	'default'     => true, // Optional
	'required'    => true, // Optional
	'custom_html' => '<div>HTML Code</div>', // Optional
)
```

### Radio <a href="#radio" id="radio"></a>

```php
array(
	'type'        => 'radio',
	'name'        => 'my-radio-field',
	'label'       => __( 'Radio field', 'masterstudy-lms-learning-management-system' ),
	'default'     => 'option-1', // Optional
	'required'    => true, // Optional
	'custom_html' => '<div>HTML Code</div>', // Optional
	'options'     => array(
		array(
			'value' => 'option-1',
			'label' => __( 'Option 1', 'masterstudy-lms-learning-management-system' ),
		),
		array(
			'value' => 'option-2',
			'label' => __( 'Option 2', 'masterstudy-lms-learning-management-system' ),
		),
	),
)
```

### Select <a href="#select" id="select"></a>

```php
array(
	'type'        => 'select',
	'name'        => 'my-select-field',
	'label'       => __( 'Select field', 'masterstudy-lms-learning-management-system' ),
	'default'     => 'option-1', // Optional
	'required'    => true, // Optional
	'custom_html' => '<div>HTML Code</div>', // Optional
	'options'     => array(
		array(
			'value' => 'option-1',
			'label' => __( 'Option 1', 'masterstudy-lms-learning-management-system' ),
		),
		array(
			'value' => 'option-2',
			'label' => __( 'Option 2', 'masterstudy-lms-learning-management-system' ),
		),
	),
)
```

## Full Example <a href="#full-example" id="full-example"></a>

Here is an example of all custom field types with filters used:

```php
<?php
function masterstudy_lms_custom_fields( $custom_fields ) {
	$my_custom_fields = array(
		array(
			'type'        => 'text',
			'name'        => 'my-text-field',
			'label'       => __( 'Text field', 'masterstudy-lms-learning-management-system' ),
			'default'     => '', // Optional
			'required'    => false, // Optional
			'custom_html' => '<a href="#">Custom Link</a>', // Optional
		),
		array(
			'type'        => 'number',
			'name'        => 'my-number-field',
			'label'       => __( 'Number field', 'masterstudy-lms-learning-management-system' ),
			'default'     => 10, // Optional
			'required'    => true, // Optional
			'custom_html' => '<div>HTML Code</div>', // Optional
		),
		array(
			'type'        => 'textarea',
			'name'        => 'my-textarea-field',
			'label'       => __( 'Textarea field', 'masterstudy-lms-learning-management-system' ),
			'default'     => '', // Optional
			'required'    => true, // Optional
			'custom_html' => '<div>HTML Code</div>', // Optional
		),
		array(
			'type'        => 'checkbox',
			'name'        => 'my-checkbox-field',
			'label'       => __( 'Checkbox field', 'masterstudy-lms-learning-management-system' ),
			'default'     => true, // Optional
			'required'    => false, // Optional
			'custom_html' => '<div>HTML Code</div>', // Optional
		),
		array(
			'type'        => 'radio',
			'name'        => 'my-radio-field',
			'label'       => __( 'Radio field', 'masterstudy-lms-learning-management-system' ),
			'default'     => 'option-1', // Optional
			'required'    => true, // Optional
			'custom_html' => '<div>HTML Code</div>', // Optional
			'options'     => array(
				array(
					'value' => 'option-1',
					'label' => __( 'Option 1', 'masterstudy-lms-learning-management-system' ),
				),
				array(
					'value' => 'option-2',
					'label' => __( 'Option 2', 'masterstudy-lms-learning-management-system' ),
				),
			),
		),
		array(
			'type'        => 'select',
			'name'        => 'my-select-field',
			'label'       => __( 'Select field', 'masterstudy-lms-learning-management-system' ),
			'default'     => 'option-1', // Optional
			'required'    => true, // Optional
			'custom_html' => '<div>HTML Code</div>', // Optional
			'options'     => array(
				array(
					'value' => 'option-1',
					'label' => __( 'Option 1', 'masterstudy-lms-learning-management-system' ),
				),
				array(
					'value' => 'option-2',
					'label' => __( 'Option 2', 'masterstudy-lms-learning-management-system' ),
				),
			),
		),
	);

	return array_merge( $custom_fields, $my_custom_fields );
}

add_filter( 'masterstudy_lms_course_custom_fields', 'masterstudy_lms_custom_fields' );
add_filter( 'masterstudy_lms_lesson_custom_fields', 'masterstudy_lms_custom_fields' );
add_filter( 'masterstudy_lms_quiz_custom_fields', 'masterstudy_lms_custom_fields' );
```
