Conditional Row Styling

Apply custom CSS styling to table rows based on entry field values or entry properties. Style rows differently based on status fields, payment status, user roles, or any other entry data.

Instructions

  1. Install this snippet with your preferred snippet management solution. https://gravitywiz.com/documentation/managing-snippets/

  2. Update the configuration section at the bottom of this snippet with your form ID and styling rules.

  3. Configure your rules:

    • ‘field_id’ can be a form field ID (e.g., 5) or an entry property: Common entry properties: ‘payment_status’, ‘payment_amount’, ‘is_starred’, ‘is_read’, ‘created_by’, ‘status’, ‘date_created’, ‘source_url’, ‘ip’, etc.
    • ‘conditions’ maps field values to CSS styles (for exact matches)
    • ‘operator_conditions’ allows comparison operators (>, <, >=, <=, !=, contains)
    • Styles can include any valid CSS property (use underscores or hyphens: background_color or ‘background-color’)
  4. Multiple rules can be stacked – if multiple conditions match, all styles will be applied.

Code

Filename: gpeb-conditional-row-styling.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
/**
 * Gravity Perks // Entry Blocks // Conditional Row Styling
 * https://gravitywiz.com/documentation/gravity-forms-entry-blocks/
 *
 * Apply custom CSS styling to table rows based on entry field values or entry properties.
 * Style rows differently based on status fields, payment status, user roles, or any other entry data.
 *
 * Instructions:
 *
 * 1. Install this snippet with your preferred snippet management solution.
 *    https://gravitywiz.com/documentation/managing-snippets/
 *
 * 2. Update the configuration section at the bottom of this snippet with your form ID and styling rules.
 *
 * 3. Configure your rules:
 *    - 'field_id' can be a form field ID (e.g., 5) or an entry property:
 *      Common entry properties: 'payment_status', 'payment_amount', 'is_starred',
 *      'is_read', 'created_by', 'status', 'date_created', 'source_url', 'ip', etc.
 *    - 'conditions' maps field values to CSS styles (for exact matches)
 *    - 'operator_conditions' allows comparison operators (>, <, >=, <=, !=, contains)
 *    - Styles can include any valid CSS property (use underscores or hyphens: background_color or 'background-color')
 *
 * 4. Multiple rules can be stacked - if multiple conditions match, all styles will be applied.
 */
class GPEB_Conditional_Row_Styling {

	private $form_id;
	private $rules;

	public function __construct( $config = array() ) {
		if ( empty( $config['form_id'] ) || empty( $config['rules'] ) ) {
			return;
		}

		$this->form_id = rgar( $config, 'form_id' );
		$this->rules   = rgar( $config, 'rules', array() );

		add_action( 'init', array( $this, 'init' ) );
	}

	public function init() {
		if ( ! function_exists( 'gp_entry_blocks' ) ) {
			return;
		}

		add_filter( 'gpeb_table_row_markup', array( $this, 'apply_conditional_styling' ), 10, 2 );
	}

	private function is_applicable_form( $current_form_id ) {
		return (int) $current_form_id === (int) $this->form_id;
	}

	public function apply_conditional_styling( $output, $row ) {
		if ( empty( $this->rules ) ) {
			return $output;
		}

		$entry = $row->entry;

		if ( ! $this->is_applicable_form( $entry['form_id'] ) ) {
			return $output;
		}

		$inline_styles = $this->get_matching_styles( $entry );

		if ( empty( $inline_styles ) ) {
			return $output;
		}

		return $this->inject_style_attribute( $output, $this->build_style_string( $inline_styles ) );
	}

	private function get_matching_styles( $entry ) {
		$styles = array();

		foreach ( $this->rules as $rule ) {
			$field_id = rgar( $rule, 'field_id' );

			if ( ! $field_id ) {
				continue;
			}

			$field_value = rgar( $entry, $field_id, '' );

			// Handle exact match conditions.
			$conditions = rgar( $rule, 'conditions', array() );
			foreach ( $conditions as $match_value => $rule_styles ) {
				if ( (string) $field_value === (string) $match_value && is_array( $rule_styles ) ) {
					$styles = array_merge( $styles, $rule_styles );
				}
			}

			// Handle operator-based conditions.
			$operator_conditions = rgar( $rule, 'operator_conditions', array() );
			foreach ( $operator_conditions as $condition ) {
				$operator      = rgar( $condition, 'operator', '=' );
				$compare_value = rgar( $condition, 'value' );
				$rule_styles   = rgar( $condition, 'styles', array() );

				if ( $this->evaluate_condition( $field_value, $operator, $compare_value ) && is_array( $rule_styles ) ) {
					$styles = array_merge( $styles, $rule_styles );
				}
			}
		}

		return $styles;
	}

	private function evaluate_condition( $field_value, $operator, $compare_value ) {
		// Handle non-numeric operators first.
		if ( $operator === '!=' ) {
			return (string) $field_value !== (string) $compare_value;
		}

		if ( $operator === 'contains' ) {
			return stripos( (string) $field_value, (string) $compare_value ) !== false;
		}

		// Strip non-numeric characters for numeric comparisons.
		$numeric_value = floatval( preg_replace( '/[^0-9.\-]/', '', $field_value ) );

		switch ( $operator ) {
			case '>':
				return $numeric_value > (float) $compare_value;

			case '>=':
				return $numeric_value >= (float) $compare_value;

			case '<':
				return $numeric_value < (float) $compare_value;

			case '<=':
				return $numeric_value <= (float) $compare_value;

			default:
				return false;
		}
	}

	private function build_style_string( $styles ) {
		$style_parts = array();

		foreach ( $styles as $property => $value ) {
			$css_property  = str_replace( '_', '-', $property );
			$css_value     = esc_attr( $value );
			$style_parts[] = $css_property . ': ' . $css_value;
		}

		return implode( '; ', $style_parts ) . ';';
	}

	private function inject_style_attribute( $html, $new_styles ) {
		// Check for existing style attribute with double quotes.
		if ( preg_match( '/<tr\s[^>]*style="([^"]*)"/i', $html, $matches ) ) {
			$combined = rtrim( $matches[1], '; ' ) . '; ' . $new_styles;
			return preg_replace(
				'/(<tr\s[^>]*)style="[^"]*"/i',
				'$1style="' . esc_attr( $combined ) . '"',
				$html,
				1
			);
		}

		// Check for existing style attribute with single quotes.
		if ( preg_match( "/<tr\s[^>]*style='([^']*)'/i", $html, $matches ) ) {
			$combined = rtrim( $matches[1], '; ' ) . '; ' . $new_styles;
			return preg_replace(
				"/(<tr\s[^>]*)style='[^']*'/i",
				'$1style="' . esc_attr( $combined ) . '"',
				$html,
				1
			);
		}

		// No existing style attribute, add one.
		return preg_replace(
			'/<tr(\s|>)/i',
			'<tr style="' . esc_attr( $new_styles ) . '"$1',
			$html,
			1
		);
	}
}

# Configuration
new GPEB_Conditional_Row_Styling( array(
	'form_id' => 123, // Update to your form ID.

	'rules' => array(

		// Example 1: Style rows by payment status (exact match).
		array(
			'field_id'   => 'payment_status',
			'conditions' => array(
				'Paid'    => array(
					'background-color' => '#d4edda',
					'border-left'      => '4px solid #28a745',
				),
				'Pending' => array(
					'background-color' => '#fff3cd',
					'border-left'      => '4px solid #ffc107',
				),
				'Failed'  => array(
					'background-color' => '#f8d7da',
					'border-left'      => '4px solid #dc3545',
				),
			),
		),

		// Example 2: Style rows by a dropdown field value (field ID 5).
		array(
			'field_id'   => 5,
			'conditions' => array(
				'High Priority' => array(
					'background-color' => '#fff0f0',
					'font-weight'      => 'bold',
					'color'            => '#c00',
				),
				'Low Priority'  => array(
					'background-color' => '#f0f0f0',
					'color'            => '#666',
					'font-style'       => 'italic',
				),
			),
		),

		// Example 3: Highlight starred entries.
		array(
			'field_id'   => 'is_starred',
			'conditions' => array(
				'1' => array(
					'background-color' => '#fffbeb',
					'border-left'      => '4px solid #f59e0b',
				),
			),
		),

		// Example 4: Style based on numeric comparison (e.g., order total in field 10).
		array(
			'field_id'            => 10,
			'operator_conditions' => array(
				array(
					'operator' => '>=',
					'value'    => 500,
					'styles'   => array(
						'background-color' => '#d4edda',
						'font-weight'      => 'bold',
					),
				),
				array(
					'operator' => '<',
					'value'    => 50,
					'styles'   => array(
						'background-color' => '#f8d7da',
						'color'            => '#721c24',
					),
				),
			),
		),

		// Example 5: Use != operator to highlight incomplete entries.
		array(
			'field_id'            => 8, // Status field.
			'operator_conditions' => array(
				array(
					'operator' => '!=',
					'value'    => 'Complete',
					'styles'   => array(
						'background-color' => '#fff3cd',
						'border-left'      => '4px solid #856404',
					),
				),
			),
		),

		// Example 6: Use 'contains' to match partial text.
		array(
			'field_id'            => 3, // Email field.
			'operator_conditions' => array(
				array(
					'operator' => 'contains',
					'value'    => '@company.com',
					'styles'   => array(
						'background-color' => '#e7f3ff',
						'border-left'      => '4px solid #0066cc',
					),
				),
			),
		),

	),
) );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.