BP_Core_HTML_Element::__construct( array $r = array() )

Constructor.

Description

Parameters

$r

(Optional) An array of arguments.

  • 'element'
    (string) The element to render. eg. 'a' for the anchor element.
  • 'attr'
    (array) Optional. The element's attributes set as key/value pairs. eg. array( 'href' => <a href="http://example.com">http://example.com</a>, 'class' => 'my-class' )
  • 'inner_html'
    (string) Optional. The inner HTML for the element if applicable. Please note that this isn't sanitized, so you should use your own sanitization routine before using this parameter.

Default value: array()

Source

File: bp-core/classes/class-bp-core-html-element.php

	public function __construct( $r = array() ) {
		$elem = sanitize_html_class( $r['element'] );
		if ( empty( $elem ) ) {
			return;
		}

		// Render attributes.
		$attributes = '';
		foreach( (array) $r['attr'] as $attr => $val ) {
			// If attribute is empty, skip.
			if ( empty( $val ) ) {
				continue;
			}

			if ( 'href' === $attr || 'formaction' === $attr || 'src' === $attr ) {
				$val = esc_url( $val );
			} elseif ( 'id' === $attr ) {
				$val = sanitize_html_class( $val );
			} else {
				$val = esc_attr( $val );
			}

			$attributes .= sprintf( '%s="%s" ', sanitize_html_class( $attr ), $val );
		}

		// <input> / <img> is self-closing.
		if ( 'input' === $elem || 'img' === $elem ) {
			$this->open_tag = sprintf( '<%1$s %2$s />', $elem, $attributes );

		// All other elements.
		} else {
			$this->open_tag   = sprintf( '<%1$s %2$s>', $elem, $attributes );
			$this->inner_html = ! empty( $r['inner_html'] ) ? $r['inner_html'] : '';
			$this->close_tag  = sprintf( '</%1$s>',	$elem );
		}
	}

Changelog

Changelog
Version Description
BuddyPress 2.7.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.