Radio()

Description #

The Radio type field object.

Changelog #

VersionDescription
4.1.0Introduced.

Source #

File: lib/form/class-radio.php

class Radio extends Field {
	/**
	 * The field type.
	 *
	 * @var string
	 */
	public $type = 'radio';

	/**
	 * Prepare field.
	 *
	 * @return void
	 */
	protected function prepare() {
		$this->args = wp_parse_args(
			$this->args,
			array(
				'label' => __( 'AnsPress Radio Field', 'anspress-question-answer' ),
			)
		);

		// Call parent prepare().
		parent::prepare();

		// Make sure checkbox value are sanitized.
		$this->sanitize_cb = array_merge( array( 'text_field' ), $this->sanitize_cb );
	}

	/**
	 * Field markup.
	 *
	 * @return void
	 */
	public function field_markup() {
		parent::field_markup();

		$value = $this->value();

		if ( $this->get( 'options' ) ) {
			foreach ( $this->get( 'options' ) as $val => $label ) {
				$checked = checked( $value, $val, false );
				$this->add_html( '<label>' );
				$this->add_html( '<input type="radio" value="' . esc_attr( $val ) . '" name="' . esc_attr( $this->field_name ) . '" id="' . sanitize_html_class( $this->field_name . $val ) . '" class="ap-form-control" ' . $checked . $this->custom_attr() . '/>' );
				$this->add_html( $label . '</label>' );
			}
		}

		/** This action is documented in lib/form/class-input.php */
		do_action_ref_array( 'ap_after_field_markup', array( &$this ) );
	}
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment