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 empty2$user = [3'id' => 1,4'name' => 'John Doe',5'email' => 'john@example.com',6'phone' => '',7'bio' => null8];910// Check if email exists and is not empty11$email = cbo_check_field($user, 'email');12if ($email) {13echo "Email: $email";14} else {15echo "Email not provided";16}1718// Check if phone exists and is not empty19$phone = cbo_check_field($user, 'phone');20if ($phone) {21echo "Phone: $phone";22} else {23echo "Phone not provided";24}2526// Check if bio exists and is not empty27$bio = cbo_check_field($user, 'bio');28if ($bio) {29echo "Bio: $bio";30} else {31echo "Bio not provided";32}
Usage Examples
Form Validation
1// Validate a form submission2function validate_contact_form($form_data) {3$errors = [];45// Check required fields6if (!cbo_check_field($form_data, 'name')) {7$errors['name'] = 'Name is required';8}910if (!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}1516if (!cbo_check_field($form_data, 'message')) {17$errors['message'] = 'Message is required';18}1920return $errors;21}2223// Usage24$form_data = [25'name' => 'John Doe',26'email' => 'invalid-email',27'message' => ''28];2930$validation_errors = validate_contact_form($form_data);3132if (empty($validation_errors)) {33echo 'Form is valid, processing submission...';34// Process form submission35} else {36echo 'Please correct the following errors:<br>';37foreach ($validation_errors as $field => $error) {38echo "- $error<br>";39}40}
Working with ACF Fields
1// Display ACF fields only if they exist and are not empty2$fields = get_fields('post_123');34// Display title if it exists5$title = cbo_check_field($fields, 'custom_title');6if ($title) {7echo '<h1>' . $title . '</h1>';8} else {9echo '<h1>' . get_the_title() . '</h1>';10}1112// Display subtitle if it exists13$subtitle = cbo_check_field($fields, 'subtitle');14if ($subtitle) {15echo '<h2>' . $subtitle . '</h2>';16}1718// Display featured image if it exists19$image = cbo_check_field($fields, 'featured_image');20if ($image) {21echo '<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.