Utility functions provide common functionality used throughout the VertiSub theme, including form handling, ACF integration, and custom field management.
Handles contact form submission via AJAX with nonce verification and email sending.
Location: inc/utils.php:11
Sends JSON response (success or error) and terminates execution.
AJAX Actions:
wp_ajax_sancho_contact - For logged-in users
wp_ajax_nopriv_sancho_contact - For non-logged-in users
POST Parameters Expected:
nonce - Security nonce for verification
name - Contact name (sanitized as text)
email - Contact email (sanitized as email)
message - Contact message (sanitized as textarea)
Security:
- Verifies nonce with
sancho_nonce action
- Dies with ‘Security check failed’ if nonce is invalid
- Sanitizes all user input
Email Configuration:
- Sends to site admin email
- Subject: “New Contact Form Submission”
- Content-Type: text/html; charset=UTF-8
// Frontend AJAX implementation
jQuery.ajax({
url: ajax_object.ajax_url,
type: 'POST',
data: {
action: 'sancho_contact',
nonce: ajax_object.nonce,
name: jQuery('#name').val(),
email: jQuery('#email').val(),
message: jQuery('#message').val()
},
success: function(response) {
if (response.success) {
alert(response.data);
} else {
alert('Error: ' + response.data);
}
}
});
// Required nonce in template
wp_localize_script('your-script', 'ajax_object', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('sancho_nonce')
));
Saves extra information for “About Us” page with comprehensive field sanitization.
Location: inc/utils.php:49
The ID of the post being saved.
Saves sanitized meta data to _about_extra post meta or returns early if validation fails.
Validation:
- Verifies nonce with
vertisub_about_extra_nonce action
- Prevents autosave interference
- Checks user capabilities with
edit_post
Supported Fields:
main_image - Sanitized with esc_url_raw()
main_video - Sanitized with esc_url_raw()
years - Integer value (allows empty string)
projects - Integer value (allows empty string)
clients - Integer value (allows empty string)
- Other fields - Sanitized with
wp_kses_post() (allows safe HTML)
Hook: Automatically added to save_post action
// Example metabox for About page
function my_about_metabox_callback($post) {
$data = get_post_meta($post->ID, '_about_extra', true);
wp_nonce_field('vertisub_save_about_extra', 'vertisub_about_extra_nonce');
?>
<p>
<label>Main Image URL:</label>
<input type="url" name="about_extra[main_image]"
value="<?php echo esc_attr($data['main_image'] ?? ''); ?>" />
</p>
<p>
<label>Main Video URL:</label>
<input type="url" name="about_extra[main_video]"
value="<?php echo esc_attr($data['main_video'] ?? ''); ?>" />
</p>
<p>
<label>Years of Experience:</label>
<input type="number" name="about_extra[years]"
value="<?php echo esc_attr($data['years'] ?? ''); ?>" />
</p>
<p>
<label>Number of Projects:</label>
<input type="number" name="about_extra[projects]"
value="<?php echo esc_attr($data['projects'] ?? ''); ?>" />
</p>
<p>
<label>Number of Clients:</label>
<input type="number" name="about_extra[clients]"
value="<?php echo esc_attr($data['clients'] ?? ''); ?>" />
</p>
<p>
<label>Additional Content:</label>
<textarea name="about_extra[content]"><?php
echo esc_textarea($data['content'] ?? '');
?></textarea>
</p>
<?php
}
// Retrieve About page data in template
$about_data = get_post_meta(get_the_ID(), '_about_extra', true);
if (!empty($about_data['main_image'])) {
echo '<img src="' . esc_url($about_data['main_image']) . '" alt="About">';
}
if (!empty($about_data['years'])) {
echo '<div class="stat"><span>' . esc_html($about_data['years']) .
'</span> Years of Experience</div>';
}
if (!empty($about_data['projects'])) {
echo '<div class="stat"><span>' . esc_html($about_data['projects']) .
'</span> Projects Completed</div>';
}
if (!empty($about_data['clients'])) {
echo '<div class="stat"><span>' . esc_html($about_data['clients']) .
'</span> Satisfied Clients</div>';
}
ACF Integration
vertisub_get_acf_field()
Returns the value of an Advanced Custom Fields (ACF) field with fallback support.
Location: inc/utils.php:84
The name of the ACF field to retrieve.
Whether to retrieve the field value from the options page.
The value of the ACF field, or an empty string if the field does not exist or ACF is not active.
Features:
- Checks if ACF plugin is active before attempting retrieval
- Returns empty string if ACF is not available (prevents fatal errors)
- Supports both post fields and option pages
- Works with all ACF field types
ACF Field Types Supported:
- Text, Textarea, Number, Email, URL
- Select, Checkbox, Radio, True/False
- Image, File, Gallery
- Wysiwyg Editor, Oembed
- Repeater, Flexible Content, Group
- And all other ACF field types
// Get field from current post
$hero_title = vertisub_get_acf_field('hero_title');
if (!empty($hero_title)) {
echo '<h1>' . esc_html($hero_title) . '</h1>';
}
// Get field from ACF options page
$site_phone = vertisub_get_acf_field('company_phone', true);
$site_email = vertisub_get_acf_field('company_email', true);
$site_address = vertisub_get_acf_field('company_address', true);
echo '<div class="contact-info">';
if (!empty($site_phone)) {
echo '<p>Phone: ' . esc_html($site_phone) . '</p>';
}
if (!empty($site_email)) {
echo '<p>Email: ' . esc_html($site_email) . '</p>';
}
if (!empty($site_address)) {
echo '<p>Address: ' . esc_html($site_address) . '</p>';
}
echo '</div>';
// Work with image fields
$logo = vertisub_get_acf_field('site_logo', true);
if (!empty($logo)) {
// If returning image array
if (is_array($logo)) {
echo '<img src="' . esc_url($logo['url']) . '" ' .
'alt="' . esc_attr($logo['alt']) . '">';
}
// If returning image ID
elseif (is_numeric($logo)) {
echo wp_get_attachment_image($logo, 'full');
}
// If returning image URL
else {
echo '<img src="' . esc_url($logo) . '" alt="Logo">';
}
}
// Work with repeater fields
$team_members = vertisub_get_acf_field('team_members');
if (!empty($team_members)) {
echo '<div class="team-grid">';
foreach ($team_members as $member) {
echo '<div class="team-member">';
echo '<h3>' . esc_html($member['name']) . '</h3>';
echo '<p>' . esc_html($member['position']) . '</p>';
if (!empty($member['photo'])) {
echo '<img src="' . esc_url($member['photo']['url']) . '" ' .
'alt="' . esc_attr($member['name']) . '">';
}
echo '</div>';
}
echo '</div>';
}
// Safe usage without ACF plugin active
// This will return empty string instead of causing fatal error
$custom_field = vertisub_get_acf_field('some_field');
if (empty($custom_field)) {
// Fallback to default value or standard WordPress meta
$custom_field = get_post_meta(get_the_ID(), 'some_field', true);
}
Best Practices:
- Always check if return value is empty before using
- Use this function instead of direct
get_field() calls for better compatibility
- For option pages, always pass
true as second parameter
- The function provides graceful degradation if ACF is deactivated