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
Name | Type | Required | Default | Description |
---|---|---|---|---|
$content | string | true | - | 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. |
$haystack | array | true | - | 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;
}