LLMS_Admin_Settings::output_field( array $field )

Output fields


Parameters Parameters

$field

(array) (Required) Array of field settings.<br>

  • 'id'
    (string) The setting ID. Used as the from element's name and id attributes and automatically correspond to an option key using the WP options API.<br>
  • 'type'
    (string) The field type. Accepts: 'title', 'table', 'subtitle', 'desc', 'custom-html', 'custom-html-no-wrap', 'sectionstart', 'sectionend', 'button', 'hidden', 'keyval', 'text', 'email', 'number', 'password', 'textarea', 'wpeditor', 'select', 'multiselect', 'radio', 'checkbox', 'image', 'single_select_page', 'single_select_membership'.<br>
  • 'title'
    (string) The title / name of the option as displayed to the user.<br>
  • 'name'
    (string) For "button" fields only: used as HTML name attribute. If not supplied the default value "save" will be used. For other field types used as a fallback for $title if no value is supplied.<br>
  • 'class'
    (string) A space-separated list of CSS class names to apply the setting form element (the <input>, <select> etc...).<br>
  • 'css'
    (string) An inline CSS style string.<br>
  • 'default'
    (string) The default value of the setting.<br>
  • 'desc'
    (string) The setting's description.<br>
  • 'desc_tooltip'
    (bool) If true, displays $desc in a hoverable tooltip.<br>
  • 'value'
    (string) The value of the setting. If supplied this will override the automatic setting retrieval using get_option( $id, $default ).<br>
  • 'custom_attributes'
    (array) An associative array of custom HTML attributes to be added to the form element (the <input>, <select> etc...).<br>
  • 'disabled'
    (bool) If true adds the llms-disabled-field class to the settings field wrapper.<br>
  • 'required'
    (bool) If true, text, email, number, and password fields will require user input.<br>
  • 'secure_option'
    (string) The name of settings secure option equivalent. If specified, the fields value will be automatically removed from the database and the value will be masked when displayed on on screen. See llms_get_secure_option() for more information.<br>
  • 'sanitize'
    (string) Automatically apply the specified sanitization to the value before storing and outputting the stored value. Supported filters: + "slug": Uses sanitize_title() on the value when storing and urldecode() when displaying.<br>
  • 'after_html'
    (string) Additional HTML to add after the field's form element.<br>
  • 'editor_settings'
    (array) Used with "wpeditor" field type only. An array of options to pass to wp_editor() as the $settings argument.<br>
  • 'options'
    (array) For "select", "multiselect", and "radio" fields, an array of key/value pairs where the key is the setting value stored in database and the value is the setting label displayed on screen.<br>


Top ↑

Return Return

(void)


Top ↑

Source Source

File: includes/admin/class.llms.admin.settings.php

	public static function output_field( $field ) {

		// Set missing values with defaults.
		$field = self::set_field_defaults( $field );

		// Setup custom attributes.
		$custom_attributes = self::format_field_custom_attributes( $field['custom_attributes'] ?? array() );

		// Setup field description and tooltip.
		extract( self::set_field_descriptions( $field ) );
		$description .= ' ' . $field['after_html'];

		// Get the field value.
		$option_value = isset( $field['value'] ) ? $field['value'] : self::get_option( $field['id'], $field['default'] );

		// Setup the disabled CSS class.
		$disabled_class = ( isset( $field['disabled'] ) && true === $field['disabled'] ) ? 'llms-disabled-field' : '';

		// Switch based on type.
		switch ( $field['type'] ) {

			// Section Titles.
			case 'title':
				if ( ! empty( $field['title'] ) ) {
					echo '<p class="llms-label">' . esc_html( $field['title'] ) . '</p>';
				}
				if ( ! empty( $field['desc'] ) ) {
					// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in wp_kses_post()
					echo '<p class="llms-description">' . wpautop( wptexturize( wp_kses_post( $field['desc'] ) ) ) . '</p>';
				}

				echo '<table class="form-table">' . "\n\n";

				if ( ! empty( $field['id'] ) ) {

					do_action( 'lifterlms_settings_' . sanitize_title( $field['id'] ) );

				}
				break;

			case 'table':
				echo '<tr valign="top" class="' . esc_attr( $disabled_class ) . '"><td>';

					$field['table']->get_results();
					$field['table']->output_table_html();

				echo '</td></tr>';
				break;

			case 'subtitle':
				if ( ! empty( $field['title'] ) ) {
					echo '<tr valign="top" class="' . esc_attr( $disabled_class ) . '"><td colspan="2">
				    	<h3 class="llms-subtitle">' . esc_html( $field['title'] ) . '</h3>';
					if ( ! empty( $field['desc'] ) ) {
						echo '<p>' . wp_kses_post( $field['desc'] ) . '</p>';
					}
					echo '</tr></td>';
				}
				break;

			case 'desc':
				if ( ! empty( $field['desc'] ) ) {
					// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in wp_kses_post()
					echo '<th colspan="2" style="font-weight: normal;">' . wpautop( wptexturize( wp_kses_post( $field['desc'] ) ) ) . '</th>';
				}

				break;

			case 'custom-html':
				if ( ! empty( $field['value'] ) ) {
					// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in value / template file..
					echo '<tr valign="top" class="' . esc_attr( $disabled_class ) . '"><td colspan="2">' . $field['value'] . '</tr></td>';
				}
				break;

			case 'custom-html-no-wrap':
				if ( ! empty( $field['value'] ) ) {
					// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in value / template file..
					echo $field['value'];
				}
				break;

			case 'sectionstart':
				if ( ! empty( $field['id'] ) ) {

					do_action( 'lifterlms_settings_' . sanitize_title( $field['id'] ) . '_before' );

					echo '<div class="llms-setting-group ' . esc_attr( $field['class'] ) . '">';

					do_action( 'lifterlms_settings_' . sanitize_title( $field['id'] ) . '_start' );

				}
				break;

			case 'sectionend':
				if ( ! empty( $field['id'] ) ) {

					do_action( 'lifterlms_settings_' . sanitize_title( $field['id'] ) . '_end' );

				}

				echo '</table>';
				echo '</div>';

				if ( ! empty( $field['id'] ) ) {

					do_action( 'lifterlms_settings_' . sanitize_title( $field['id'] ) . '_after' );

				}
				break;

			case 'button':
				$name = isset( $field['name'] ) ? $field['name'] : 'save';
				echo '<tr valign="top" class="' . esc_attr( $disabled_class ) . '"><th><label for="' . esc_attr( $field['id'] ) . '">' . esc_html( $field['title'] ) . '</label>';
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $tooltip escaped in set_field_descriptions().
				echo $tooltip;
				echo '</th>';

				echo '<td class="forminp forminp-' . esc_attr( sanitize_title( $field['type'] ) ) . '">';
				echo '<div id="llms-form-wrapper">';
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $description escaped in set_field_descriptions().
				echo $description . '<br><br>';
				echo '<input name="' . esc_attr( $name ) . '" class="llms-button-primary" type="submit" value="' . esc_attr( $field['value'] ) . '" />';
				echo '</div>';
				echo '</td></tr>';
				// phpcs:ignore -- commented out code
				// get_submit_button( 'Filter Results', 'primary', 'llms_search', true, array( 'id' => 'llms_analytics_search' ) );
				break;

			case 'hidden':
				echo '<th></th>';
				echo '<td><input type="hidden"
					name="' . esc_attr( $field['id'] ) . '"
					id="' . esc_attr( $field['id'] ) . '"
					value="' . esc_attr( $field['value'] ) . '">';
				break;

			case 'keyval':
				?><tr valign="top">
					<th>
						<label for="<?php echo esc_attr( $field['id'] ); ?>"><?php echo esc_html( $field['title'] ); ?></label>
						<?php echo $tooltip; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in set_field_descriptions(). ?>
					</th>
					<td class="forminp forminp-<?php echo esc_attr( sanitize_title( $field['type'] ) ); ?>">
						<div id="<?php echo esc_attr( $field['id'] ); ?>"><?php echo wp_kses_post( $field['value'] ); ?></div>
					</td>
				</tr>
				<?php

				break;

			case 'text':
			case 'email':
			case 'number':
			case 'password':
				$type     = $field['type'];
				$class    = '';
				$required = ! empty( $field['required'] );

				$secure_val   = isset( $field['secure_option'] ) ? llms_get_secure_option( $field['secure_option'], false ) : false;
				$option_value = ( false !== $secure_val ) ? str_repeat( '*', strlen( $secure_val ) ) : $option_value;

				// Ensure slugs with non-latin characters are not displayed as urlencoded strings.
				if ( ! empty( $field['sanitize'] ) && 'slug' === $field['sanitize'] ) {
					$option_value = urldecode( $option_value );
				}

				?>
				<tr valign="top" class="<?php echo esc_attr( $disabled_class ); ?>">
					<th>
						<label for="<?php echo esc_attr( $field['id'] ); ?>">
							<?php echo esc_html( $field['title'] ); ?>
							<?php echo $required ? '<span class="llms-required">*</span>' : ''; ?>
						</label>
						<?php echo $tooltip; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in set_field_descriptions(). ?>
					</th>
					<td class="forminp forminp-<?php echo esc_attr( sanitize_title( $field['type'] ) ); ?>">
						<input
							name="<?php echo esc_attr( $field['id'] ); ?>"
							id="<?php echo esc_attr( $field['id'] ); ?>"
							type="<?php echo esc_attr( $type ); ?>"
							style="<?php echo esc_attr( $field['css'] ); ?>"
							value="<?php echo esc_attr( $option_value ); ?>"
							class="<?php echo esc_attr( $field['class'] ); ?>"
							<?php echo $secure_val ? 'disabled="disabled"' : ''; ?>
							<?php echo implode( ' ', $custom_attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in format_field_custom_attributes. ?>
							<?php echo $required ? 'required="required"' : ''; ?>
							/> <?php echo wp_kses_post( $description ); ?>
					</td>
				</tr>
				<?php
				break;

			// Textarea.
			case 'textarea':
				?>
				<tr valign="top" class="<?php echo esc_attr( $disabled_class ); ?>">
					<th>
						<label for="<?php echo esc_attr( $field['id'] ); ?>"><?php echo esc_html( $field['title'] ); ?></label>
						<?php echo $tooltip; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in set_field_descriptions. ?>
					</th>
					<td class="forminp forminp-<?php echo esc_attr( sanitize_title( $field['type'] ) ); ?>">
						<textarea
							name="<?php echo esc_attr( $field['id'] ); ?>"
							id="<?php echo esc_attr( $field['id'] ); ?>"
							style="<?php echo esc_attr( $field['css'] ); ?>"
							class="<?php echo esc_attr( $field['class'] ); ?>"
							<?php echo implode( ' ', $custom_attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in format_field_custom_attributes. ?>
							><?php echo esc_textarea( $option_value ); ?></textarea>
						<?php echo wp_kses_post( $description ); ?>
					</td>
				</tr>
				<?php
				break;

			case 'wpeditor':
				$editor_settings = isset( $field['editor_settings'] ) ? $field['editor_settings'] : array();
				?>
				<tr valign="top" class="<?php echo esc_attr( $disabled_class ); ?>">
					<th>
						<label for="<?php echo esc_attr( $field['id'] ); ?>"><?php echo esc_html( $field['title'] ); ?></label>
						<?php echo $tooltip; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in set_field_descriptions. ?>
					</th>
					<td class="forminp forminp-<?php echo esc_attr( sanitize_title( $field['type'] ) ); ?>">
						<?php wp_editor( $option_value, $field['id'], $editor_settings ); ?>
						<?php echo wp_kses_post( $description ); ?>
					</td>
				</tr>
				<?php
				break;

			// Select boxes.
			case 'select':
			case 'multiselect':
				$field_name = esc_attr( $field['id'] );
				if ( 'multiselect' === $field['type'] ) {
					$field_name .= '[]';
				}
				?>
				<tr valign="top" class="<?php echo esc_attr( $disabled_class ); ?>">
					<th>
						<label for="<?php echo esc_attr( $field['id'] ); ?>"><?php echo esc_html( $field['title'] ); ?></label>
						<?php echo $tooltip; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in set_field_descriptions. ?>
					</th>
					<td class="forminp forminp-<?php echo esc_attr( sanitize_title( $field['type'] ) ); ?>">
						<select
							name="<?php echo esc_attr( $field_name ); ?>"
							id="<?php echo esc_attr( $field['id'] ); ?>"
							style="<?php echo esc_attr( $field['css'] ); ?>"
							class="<?php echo esc_attr( $field['class'] ); ?>"
							<?php echo implode( ' ', $custom_attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in format_field_custom_attributes. ?>
							<?php
							if ( 'multiselect' === $field['type'] ) {
								echo 'multiple="multiple"'; }
							?>
							>
							<?php
							foreach ( $field['options'] as $key => $val ) {

								// Convert an array from llms_make_select2_post_array().
								if ( is_array( $val ) ) {
									$key = $val['key'];
									$val = $val['title'];
								}

								?>
								<option value="<?php echo esc_attr( $key ); ?>"
								<?php
								if ( is_array( $option_value ) ) {
									selected( in_array( $key, $option_value ), true );
								} else {
									selected( $option_value, $key );
								}
								?>
								><?php echo wp_kses_post( $val ); ?></option>
								<?php
							}
							?>
							</select>
						<?php echo wp_kses_post( $description ); ?>
					</td>
				</tr>
				<?php
				break;

			// Radio inputs.
			case 'radio':
				?>
				<tr valign="top" class="<?php echo esc_attr( $disabled_class ); ?>">
					<th>
						<label for="<?php echo esc_attr( $field['id'] ); ?>"><?php echo esc_html( $field['title'] ); ?></label>
						<?php echo $tooltip; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in set_field_descriptions. ?>
					</th>
					<td class="forminp forminp-<?php echo esc_attr( sanitize_title( $field['type'] ) ); ?>">
						<fieldset>
							<?php echo wp_kses_post( $description ); ?>
							<ul>
							<?php
							foreach ( $field['options'] as $key => $val ) {
								?>
								<li>
									<label><input
										name="<?php echo esc_attr( $field['id'] ); ?>"
										value="<?php echo esc_attr( $key ); ?>"
										type="radio"
										style="<?php echo esc_attr( $field['css'] ); ?>"
										class="<?php echo esc_attr( $field['class'] ); ?>"
										<?php echo implode( ' ', $custom_attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in format_field_custom_attributes. ?>
										<?php checked( $key, $option_value ); ?>
										/> <?php echo esc_html( $val ); ?></label>
									</li>
									<?php
							}
							?>
							</ul>
						</fieldset>
					</td>
				</tr>
				<?php
				break;

			// Checkbox input.
			case 'checkbox':
				$visbility_class = array();

				if ( ! isset( $field['hide_if_checked'] ) ) {
					$field['hide_if_checked'] = false;
				}
				if ( ! isset( $field['show_if_checked'] ) ) {
					$field['show_if_checked'] = false;
				}
				if ( 'yes' === $field['hide_if_checked'] || 'yes' === $field['show_if_checked'] ) {
					$visbility_class[] = 'hidden_option';
				}
				if ( 'option' === $field['hide_if_checked'] ) {
					$visbility_class[] = 'hide_options_if_checked';
				}
				if ( 'option' === $field['show_if_checked'] ) {
					$visbility_class[] = 'show_options_if_checked';
				}
				if ( ! isset( $field['checkboxgroup'] ) || 'start' === $field['checkboxgroup'] ) {
					?>
						<tr valign="top" class="<?php echo esc_attr( implode( ' ', $visbility_class ) ); ?> <?php echo esc_attr( $disabled_class ); ?>">
							<th><?php echo esc_html( $field['title'] ); ?></th>
							<td class="forminp forminp-checkbox">
								<fieldset>
					<?php
				} else {
					?>
						<fieldset class="<?php echo esc_attr( implode( ' ', $visbility_class ) ); ?>">
					<?php
				}

				if ( ! empty( $field['title'] ) ) {
					?>
						<legend class="screen-reader-text"><span><?php echo esc_html( $field['title'] ); ?></span></legend>
					<?php
				}

				?>
					<label for="<?php echo esc_attr( $field['id'] ); ?>">
						<input
							name="<?php echo esc_attr( $field['id'] ); ?>"
							id="<?php echo esc_attr( $field['id'] ); ?>"
							type="checkbox"
							value="1"
							<?php checked( $option_value, 'yes' ); ?>
							<?php echo implode( ' ', $custom_attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in format_field_custom_attributes. ?>
						/> <?php echo wp_kses_post( $description ); ?>
					</label> <?php echo $tooltip; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in set_field_descriptions. ?>
				<?php

				if ( ! isset( $field['checkboxgroup'] ) || 'end' === $field['checkboxgroup'] ) {
					?>
								</fieldset>
							</td>
						</tr>
					<?php
				} else {
					?>
						</fieldset>
					<?php
				}
				break;

			case 'image':
				$type  = $field['type'];
				$class = '';

				if ( $option_value ) {
					// Media lib object ID.
					if ( is_numeric( $option_value ) ) {
						$size       = isset( $field['image_size'] ) ? $field['image_size'] : 'medium';
						$attachment = wp_get_attachment_image_src( $option_value, $size );
						$src        = $attachment[0];
					} else {
						// Raw img src.
						$src = $option_value;
					}
				} else {
					$src = '';
				}

				?>
				<tr valign="top" class="<?php echo esc_attr( $disabled_class ); ?>">
					<th>
						<label for="<?php echo esc_attr( $field['id'] ); ?>"><?php echo esc_html( $field['title'] ); ?></label>
						<?php echo $tooltip; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in set_field_descriptions. ?>
					</th>
					<td class="forminp forminp-<?php echo esc_attr( sanitize_title( $field['type'] ) ); ?>">

						<img class="llms-image-field-preview" src="<?php echo esc_url( $src ); ?>">
						<button class="llms-button-secondary llms-image-field-upload" data-id="<?php echo esc_attr( $field['id'] ); ?>" type="button">
							<span class="dashicons dashicons-admin-media"></span>
							<?php esc_html_e( 'Upload', 'lifterlms' ); ?>
						</button>
						<button class="llms-button-danger llms-image-field-remove<?php echo ( ! $src ) ? ' hidden' : ''; ?>" data-id="<?php echo esc_attr( $field['id'] ); ?>" type="button">
							<span class="dashicons dashicons-no"></span>
						</button>
						<input
							name="<?php echo esc_attr( $field['id'] ); ?>"
							id="<?php echo esc_attr( $field['id'] ); ?>"
							type="hidden"
							style="<?php echo esc_attr( $field['css'] ); ?>"
							value="<?php echo esc_attr( $option_value ); ?>"
							class="<?php echo esc_attr( $field['class'] ); ?>"
							<?php echo implode( ' ', $custom_attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in format_field_custom_attributes. ?>
							/> <?php echo wp_kses_post( $description ); ?>
					</td>
				</tr>
				<?php
				break;

			// Single page selects.
			case 'single_select_page':
				$args = array(
					'name'             => $field['id'],
					'id'               => $field['id'],
					'sort_column'      => 'menu_order',
					'sort_order'       => 'ASC',
					'show_option_none' => ' ',
					'class'            => $field['class'],
					'echo'             => false,
					'selected'         => absint( self::get_option( $field['id'] ) ),
				);

				if ( isset( $field['args'] ) ) {
					$args = wp_parse_args( $field['args'], $args );
				}

				?>
				<tr valign="top" class="single_select_page">
					<th><?php echo esc_html( $field['title'] ); ?> <?php echo $tooltip; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in set_field_descriptions. ?></th>
					<td class="forminp">
						<?php
						// PHPCS ignore reason: This is a dropdown and the output is escaped in wp_dropdown_pages.
						// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
						echo str_replace( ' id=', " data-placeholder='" . esc_html__( 'Select a page&hellip;', 'lifterlms' ) . "' style='" . esc_attr( $field['css'] ) . "' class='" . esc_attr( $field['class'] ) . "' id=", wp_dropdown_pages( $args ) );
						?>
						<?php echo wp_kses_post( $description ); ?>
					</td>
				</tr>
				<?php
				break;

			// Single page selects.
			case 'single_select_membership':
				$args  = array(
					'posts_per_page' => -1,
					'post_type'      => 'llms_membership',
					'nopaging'       => true,
					'post_status'    => 'publish',
					'class'          => $field['class'],
					'selected'       => absint( self::get_option( $field['id'] ) ),
				);
				$posts = get_posts( $args );

				if ( isset( $field['args'] ) ) {
					$args = wp_parse_args( $field['args'], $args );
				}

				?>
				<tr valign="top" class="single_select_membership">
					<th><?php echo esc_html( $field['title'] ); ?> <?php echo $tooltip; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in set_field_descriptions. ?></th>
					<td class="forminp">
						<select class="<?php echo esc_attr( $args['class'] ); ?>" style="<?php echo esc_attr( $field['css'] ); ?>" name="lifterlms_membership_required" id="lifterlms_membership_required">
							<option value=""> <?php esc_html_e( 'None', 'lifterlms' ); ?></option>
							<?php
							foreach ( $posts as $post ) :
								setup_postdata( $post );
								if ( $args['selected'] === $post->ID ) {
									$selected = 'selected';
								} else {
									$selected = '';
								}
								?>
							<option value="<?php echo esc_attr( $post->ID ); ?>" <?php echo esc_attr( $selected ); ?> ><?php echo esc_html( $post->post_title ); ?></option>
						<?php endforeach; ?>
						</select>
					</td>
				</tr>
				<?php
				break;

			// Default: run an action.
			default:
				do_action( 'lifterlms_admin_field_' . $field['type'], $field, $option_value, $description, $tooltip, $custom_attributes );

				break;
		}
	}


Top ↑

Changelog Changelog

Changelog
Version Description
7.0.0 Add $after_html to all field types.
5.0.2 Pass any option value sanitized as a "slug" through urldecode() prior to displaying it.
3.37.9 Add option for fields to show an asterisk for required fields.
3.34.4 Add "keyval" field for displaying custom html next to a setting key.
3.29.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.