beans_array_shortcodes

Search content for shortcodes and filter shortcodes through their hooks.

Shortcodes must be delimited with curly brackets (e.g. {key}) and correspond to the searched array key.

beans_array_shortcodes( string $content, array $haystack )

Return: (string) Content with shortcodes filtered out.

Parameters

NameTypeRequiredDefaultDescription
$contentstringtrue-Content containing the shortcode(s) delimited with curly brackets (e.g. {key}). Shortcode(s) correspond to the searched array key and will be replaced by the array value if found.
$haystackarraytrue-The associative array used to replace shortcode(s).

Source

function beans_array_shortcodes( $content, $haystack ) {

	if ( preg_match_all( '#{(.*?)}#', $content, $matches ) ) {

		foreach ( $matches[1] as $needle ) {

			$sub_keys = explode( '.', $needle );
			$value = false;

			foreach ( $sub_keys as $sub_key ) {

				$search = $value ? $value : $haystack;
				$value = beans_get( $sub_key, $search );

			}

			if ( $value ) {
				$content = str_replace( '{' . $needle . '}', $value, $content );
			}
		}
	}

	return $content;

}