# 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**
* **repeater**

### 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
do_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 );
```

### Outputting Custom Fields <a href="#processing-input-data" id="processing-input-data"></a>

All values from Custom Fields will be saved as Post Meta fields with corresponding keys. So they can be easily retrieved and displayed as post meta fields.

**Example:**

```php
$my_text_field = get_post_meta( get_the_ID(), 'my-text-field', true );

echo esc_html( $my_text_field );
```

## 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
)
```

### TinyMCE Field <a href="#number" id="number"></a>

This custom field enables you to insert a text area with a powerful WYSIWYG text editor. Here is the code to insert:

<pre class="language-php"><code class="lang-php">array(
<strong>  'type'     => 'tinymce',
</strong>  'name'     => 'my-tinymce-field',
  'label'    => __( 'Tinymce field', 'masterstudy-lms-learning-management-system' ),
  'default'  => 'Text for tinymce', // Optional
  'required' => true, // Optional
)
</code></pre>

Path to the required folder:

```
{path to the free version of MasterStudy}/includes/filters.php
```

### 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
)
```

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

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

{% hint style="info" %}
DatePicker field accepts and returns a value in Timestamp format. So the output of the date value is also different from other fields.

**Example:**

```
$my_date_field = get_post_meta( get_the_ID(), 'my-date-field', true );

echo gmdate( get_option('date_format'), $my_date_field );
```

{% endhint %}

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

```php
array(
      'type'     => 'time',
      'name'     => 'my-time-field',
      'label'    => __( 'Time field', 'masterstudy-lms-learning-management-system' ),
      'default'  => '11:00', // Optional
      'required' => false, // 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' ),
		),
	),
)
```

### Repeater

You can use all Field Types inside a Repeater. For example:

```php
array(
	'type'   => 'repeater',
	'name'   => 'my-repeater-field',
	'label'  => __( 'Repeater field', 'masterstudy-lms-learning-management-system' ),
	'fields' => array(
		array(
			'type'  => 'text',
			'name'  => 'my-repeater-text-field',
			'label' => __( 'Text field', 'masterstudy-lms-learning-management-system' ),
		),
		array(
			'type'  => 'date',
			'name'  => 'my-repeater-date-field',
			'label' => __( 'Date field', 'masterstudy-lms-learning-management-system' ),
		),
		// More fields
	),
),
```

{% hint style="warning" %}
Repeater field doesn't support Validation.
{% endhint %}

## 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'        => 'date',
			'name'        => 'my-date-field',
			'label'       => __( 'Date field', 'masterstudy-lms-learning-management-system' ),
			'default'     => 1707868800000, // Timestamp. Optional
			'required'    => true, // Optional
			'custom_html' => '<a href="#">Custom Button</a>', // 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' );
```
