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

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:

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

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
)

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

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'        => '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