Fields

The fields utility function provides a tool for checking the existence and non-emptiness of fields in arrays.

Available Function

cbo_check_field($arr, $key)

Checks if a field exists and is not empty in an array.

Parameters:

  • $arr: The array to check.
  • $key: The key of the field to check.

Return Value:

Returns the value of the field if it exists and is not empty, or false otherwise.

1
// Check if a field exists and is not empty
2
$user = [
3
'id' => 1,
4
'name' => 'John Doe',
5
'email' => 'john@example.com',
6
'phone' => '',
7
'bio' => null
8
];
9
10
// Check if email exists and is not empty
11
$email = cbo_check_field($user, 'email');
12
if ($email) {
13
echo "Email: $email";
14
} else {
15
echo "Email not provided";
16
}
17
18
// Check if phone exists and is not empty
19
$phone = cbo_check_field($user, 'phone');
20
if ($phone) {
21
echo "Phone: $phone";
22
} else {
23
echo "Phone not provided";
24
}
25
26
// Check if bio exists and is not empty
27
$bio = cbo_check_field($user, 'bio');
28
if ($bio) {
29
echo "Bio: $bio";
30
} else {
31
echo "Bio not provided";
32
}

Usage Examples

Form Validation

1
// Validate a form submission
2
function validate_contact_form($form_data) {
3
$errors = [];
4
5
// Check required fields
6
if (!cbo_check_field($form_data, 'name')) {
7
$errors['name'] = 'Name is required';
8
}
9
10
if (!cbo_check_field($form_data, 'email')) {
11
$errors['email'] = 'Email is required';
12
} elseif (!filter_var($form_data['email'], FILTER_VALIDATE_EMAIL)) {
13
$errors['email'] = 'Please enter a valid email address';
14
}
15
16
if (!cbo_check_field($form_data, 'message')) {
17
$errors['message'] = 'Message is required';
18
}
19
20
return $errors;
21
}
22
23
// Usage
24
$form_data = [
25
'name' => 'John Doe',
26
'email' => 'invalid-email',
27
'message' => ''
28
];
29
30
$validation_errors = validate_contact_form($form_data);
31
32
if (empty($validation_errors)) {
33
echo 'Form is valid, processing submission...';
34
// Process form submission
35
} else {
36
echo 'Please correct the following errors:<br>';
37
foreach ($validation_errors as $field => $error) {
38
echo "- $error<br>";
39
}
40
}

Working with ACF Fields

1
// Display ACF fields only if they exist and are not empty
2
$fields = get_fields('post_123');
3
4
// Display title if it exists
5
$title = cbo_check_field($fields, 'custom_title');
6
if ($title) {
7
echo '<h1>' . $title . '</h1>';
8
} else {
9
echo '<h1>' . get_the_title() . '</h1>';
10
}
11
12
// Display subtitle if it exists
13
$subtitle = cbo_check_field($fields, 'subtitle');
14
if ($subtitle) {
15
echo '<h2>' . $subtitle . '</h2>';
16
}
17
18
// Display featured image if it exists
19
$image = cbo_check_field($fields, 'featured_image');
20
if ($image) {
21
echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] . '">';
22
}

Best Practices

  • Use cbo_check_field to safely check for the existence and non-emptiness of array keys.
  • Provide fallback values when a field might not exist or be empty.
  • Use for form validation to check if required fields are provided.
  • Use with ACF fields to ensure fields exist before trying to use them.
  • Remember that empty strings, null values, and 0 will all return false from this function.