add_noptin_merge_tags( string $content, array $merge_tags, bool $strict = true, bool $strip_missing = true )

Applies Noptin merge tags.


Description Description

Noptin uses a fast logic-less templating engine to parse merge tags and insert them into content.


Top ↑

Parameters Parameters

$content

(string) (Required)

$merge_tags

(array) (Required)

$strict

(bool) (Optional)

Default value: true

$strip_missing

(bool) (Optional)

Default value: true


Top ↑

Return Return

(string)


Top ↑

Source Source

File: includes/functions.php

function add_noptin_merge_tags( $content, $merge_tags, $strict = true, $strip_missing = true ) {

	$merge_tags     = $strict ? noptin_clean( $merge_tags ) : wp_kses_post_deep( $merge_tags );
	$all_merge_tags = flatten_noptin_array( $merge_tags );

	// Handle conditions.
	preg_match_all( '/\[\[#(\w*)\]\](.*?)\[\[\/\1\]\]/s', $content, $matches );

	if ( ! empty( $matches ) ) {

		foreach ( $matches[1] as $i => $match ) {

			if ( empty( $all_merge_tags[ $match ] ) ) {
				$content = str_replace( $matches[0][ $i ], '', $content );
			} else {

				$array       = array();
				$multi_array = array();

				foreach ( $all_merge_tags as $key => $value ) {

					if ( false !== strpos( $key, $match ) ) {
						$key = str_replace( $match . '.', '', $key );

						if ( is_numeric( $key ) ) {
							$array[] = $value;
						} else {
							$multi_array[ $key ] = $value;
						}

					}

				}

				// Fetched matched.
				$matched = $matches[2][ $i ];

				// Handle numeric arrays.
				if ( isset( $array[0] ) && is_scalar( $array[0] ) ) {
					$array   = '<ul><li>' . implode( '</li><li>', $array ) . '</li></ul>';
					$matched = str_replace( '[[.]]', $array, $matched );
				} else {
					$matched = add_noptin_merge_tags( $matched, $multi_array, $strict, false );
				}

				$content = str_replace( $matches[0][ $i ], $matched, $content );
			}

		}

	}

	// Replace all available tags with their values.
	foreach ( $all_merge_tags as $key => $value ) {
		if ( is_scalar( $value ) ) {
			$content = str_ireplace( "[[$key]]", $value, $content );
		}
	}

	// Remove unavailable tags.
	if ( $strip_missing ) {
		$content = preg_replace( '/\[\[[\w\.]+\]\]/', '', $content );
	}

	$content = preg_replace( '/ +([,.!])/s', '$1', $content );

	return $content;

}


Top ↑

Changelog Changelog

Changelog
VersionDescription
1.5.1Introduced.

Top ↑

User Contributed Notes User Contributed Notes

30 day money back guarantee
Safe & Secure online payment
Custom CSS Slide Example
Custom Animation Example
Default Animation