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

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:

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

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

Example:

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

All input data will be automatically saved to post_meta. Additionally, data can be used with action:

do_action( 'masterstudy_lms_custom_fields_updated', int $post_id, array $data )

Parameters:

  • $post_id int Course, Lesson, or Quiz Post ID.

  • $data array key => value array of Custom Fields

Example:

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

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:

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

echo esc_html( $my_text_field );

Custom Field Types

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

Text

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

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

Number

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

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
)

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 );

Checkbox

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

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

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:

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
	),
),

Repeater field doesn't support Validation.

Full Example

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

<?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' );

Last updated