Skip to content

Method

Render the field.

render( array $args = array() )

public

Parameters

$args array Default array()
Optional. An array of arguments for rendering a form field.
$before string
The content to render before the field.
$after string
The content to render after the field.
$control_before string
The content to render before the control.
$control_after string
The content to render after the control.

Source

public function render( $args = array() ) {
    $is_hidden = ( 'hidden' === $this->get_type() );

    if ( 'action' === $this->get_type() ) {
        return tml_buffer_action_hook( $this->get_name(), $this->render_args );
    }

    $defaults = wp_parse_args(
        $this->render_args,
        array(
            'before'         => $is_hidden ? '' : '<div class="tml-field-wrap tml-%s-wrap">',
            'after'          => $is_hidden ? '' : '</div>',
            'control_before' => '',
            'control_after'  => '',
        )
    );

    /**
     * Fires before a form field is rendered.
     *
     * @since 7.0.13
     *
     * @param string                    $form_name  The form name.
     * @param string                    $field_name The field name.
     * @param Theme_My_Login_Form_Field $field      The field object.
     */
    do_action( 'tml_render_form_field', $this->form->get_name(), $this->name, $this );

    $args = wp_parse_args( $args, $defaults );

    $output = '';

    if ( ! empty( $args['before'] ) ) {
        $output .= sprintf( $args['before'], $this->get_name() ) . "\n";
    }

    /**
     * Filter the content before the field.
     *
     * @since 7.0.13
     *
     * @param string                    $output     The output.
     * @param string                    $form_name  The form name.
     * @param string                    $field_name The field name
     * @param Theme_My_Login_Form_Field $field      The form object.
     */
    $output = apply_filters( 'tml_before_form_field', $output, $this->form->get_name(), $this->name, $this );

    $attributes = '';
    foreach ( $this->get_attributes() as $key => $value ) {
        $attributes .= ' ' . $key . '="' . esc_attr( $value ) . '"';
    }
    $classes = $this->get_classes();
    if ( $classes ) {
        $attributes .= ' class="' . implode( ' ', $classes ) . '"';
    }

    $label = '';
    if ( $this->get_label() ) {
        if ( $this->get_attribute( 'id' ) ) {
            $label = sprintf(
                '<label class="tml-label" for="%1$s">%2$s</label>',
                $this->get_attribute( 'id' ),
                $this->get_label()
            ) . "\n";
        } else {
            $label = sprintf(
                '<span class="tml-label">%s</span>',
                $this->get_label()
            ) . "\n";
        }
    }

    $error = '';
    if ( $this->get_error() ) {
        $error = '<span class="tml-error">' . $this->get_error() . '</span>';
    }

    switch ( $this->get_type() ) {
        case 'custom':
            $output .= $label;
            $output .= $this->get_content();
            break;

        case 'checkbox':
            $output .= $args['control_before'];
            $output .= '<input name="' . $this->get_name() . '" type="checkbox" value="' . esc_attr( $this->get_value() ) . '"' . $attributes . ">\n";
            $output .= $args['control_after'];
            $output .= $label;
            break;

        case 'password':
            $output .= $label;
            $output .= $error;
            $output .= $args['control_before'];
            $output .= '<div class="tml-pwd">';
            $output .= '<input name="' . $this->get_name() . '" type="password" value="' . esc_attr( $this->get_value() ) . '"' . $attributes . ">\n";
            // phpcs:ignore WordPress.WP.I18n.MissingArgDomain -- reuses WP core's translated wp-login.php string verbatim.
            $output .= '<button type="button" class="tml-toggle-pwd" aria-label="' . esc_attr__( 'Show password' ) . '"><span class="dashicons dashicons-visibility" aria-hidden="true"></span></button>';
            $output .= "</div>\n";
            $output .= $args['control_after'];
            break;

        case 'radio-group':
            $output .= $label;
            $output .= $error;
            $output .= $args['control_before'];

            $options = array();
            foreach ( $this->get_options() as $value => $label ) {
                $id = $this->get_name() . '_' . $value;

                $option = '<input name="' . $this->get_name() . '" id="' . $id . '" type="radio" value="' . esc_attr( $value ) . '"' . $attributes;
                if ( (string) $this->get_value() === (string) $value ) {
                    $option .= ' checked="checked"';
                }
                $option .= '>' . "\n";
                $option .= '<label class="tml-label" for="' . $id . '">' . $label . "</label>\n";

                $options[] = $option;
            }
            $output .= implode( '<br />', $options );

            $output .= $args['control_after'];
            break;

        case 'dropdown':
            $output .= $label;
            $output .= $error;
            $output .= $args['control_before'];
            $output .= '<select name="' . $this->get_name() . '"' . $attributes . ">\n";
            foreach ( $this->get_options() as $value => $option ) {
                $output .= '<option value="' . esc_attr( $value ) . '"';
                if ( (string) $this->get_value() === (string) $value ) {
                    $output .= ' selected="selected"';
                }
                $output .= '>' . esc_html( $option ) . "</option>\n";
            }
            $output .= "</select>\n";
            $output .= $args['control_after'];
            break;

        case 'textarea':
            $output .= $label;
            $output .= $args['control_before'];
            $output .= '<textarea name="' . $this->get_name() . '"' . $attributes . '>' . esc_textarea( $this->get_value() ) . "</textarea>\n";
            $output .= $args['control_after'];
            break;

        case 'button':
        case 'submit':
            $output .= $args['control_before'];
            $output .= '<button name="' . $this->get_name() . '" type="' . $this->get_type() . '"' . $attributes . '>' . $this->get_value() . "</button>\n";
            $output .= $args['control_after'];
            break;

        default:
            $output .= $label;
            $output .= $error;
            $output .= $args['control_before'];
            $output .= '<input name="' . $this->get_name() . '" type="' . $this->get_type() . '" value="' . esc_attr( $this->get_value() ) . '"' . $attributes . ">\n";
            $output .= $args['control_after'];
    }

    if ( $this->get_description() ) {
        $output .= '<span class="tml-description">' . $this->get_description() . "</span>\n";
    }

    /**
     * Filter the content after the field.
     *
     * @since 7.0.13
     *
     * @param string                    $output     The output.
     * @param string                    $form_name  The form name.
     * @param string                    $field_name The field name
     * @param Theme_My_Login_Form_Field $field      The form object.
     */
    $output = apply_filters( 'tml_after_form_field', $output, $this->form->get_name(), $this->name, $this );

    if ( ! empty( $args['after'] ) ) {
        $output .= $args['after'] . "\n";
    }

    return $output;
}

Uses

Changelog

VersionDescription
7.0 Introduced.