Add Custom Conditions to Dynamic Content Widget

When working with GD Dynamic Content and GD Post Badge widgets, you can use the following code snippet to add custom conditions such as is_contains_any and is_not_contains_any to your shortcodes. 

Here is an example of such usage: 

[gd_dynamic_content key="package_id" condition="is_contains_any" search="3,11,15"]DO SOMETHING[/gd_dynamic_content]

Adding Custom Conditions

We recommend using the Code Snippets plugin for adding snippets to your site. 

function gd_snippet_badge_conditions( $conditions ) {
	$conditions['is_contains_any'] = __( 'is contains any of', 'geodirectory' );
	$conditions['is_not_contains_any'] = __( 'is not contains any of', 'geodirectory' );
	return $conditions;
}

add_filter( 'geodir_badge_conditions', 'gd_snippet_badge_conditions', 10, 1 );

function gd_snippet_dynamic_content_check_match_found( $match_found, $args, $find_post ) {
	if ( ! empty( $args['condition'] ) && in_array( $args['condition'], array( 'is_contains_any',  'is_not_contains_any' ) ) ) {
		$match_found = false;
		$match_field = $args['key'];
		if ( isset( $find_post->{$match_field} ) && trim( $find_post->{$match_field} ) !== '' ) {
			$match_value = geodir_strtolower( stripslashes( $find_post->{$match_field} ) );
			$search_value = geodir_strtolower( stripslashes( $args['search'] ) );
			$match_value = explode( ",", $match_value );
			$search_value = explode( ",", $search_value );
			foreach ( $search_value as $value ) {
				$value = trim( $value );
				if ( $value !== '' && in_array( $value, $match_value ) ) {
				$match_found = true;
				break;
				}
			}
		if ( $args['condition'] == 'is_not_contains_any' ) {
			$match_found = $match_found ? false : true;
			}
		}
	}
return $match_found;
}

add_filter( 'geodir_dynamic_content_check_match_found', 'gd_snippet_dynamic_content_check_match_found', 10, 3 );
The above snippet will work with the GD > Dynamic Content widget. If you wish to use it with the GD > Post Badge widget or shortcode, simply add an extra filter: 
'geodir_post_badge_check_match_found', 'gd_snippet_dynamic_content_check_match_found', 10, 3 );
	
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us