Skip to content

Instantly share code, notes, and snippets.

@nathaningram
Last active December 16, 2024 17:08
Show Gist options
  • Select an option

  • Save nathaningram/38257995b9bc60d65fb9e7d6892b70cb to your computer and use it in GitHub Desktop.

Select an option

Save nathaningram/38257995b9bc60d65fb9e7d6892b70cb to your computer and use it in GitHub Desktop.
Starter Site 2024 - Site Info MU Plugin
<?php
/*
Plugin Name: BWW - Site Info
Description: Adds Schema, GA/GSC Tags, Form Emails, and Other Data base on Site Info Settings
Version: 2025.01
Plugin URI: https://brilliantly.net
Author: Brilliant Web Works, Inc.
Author URI: https://brilliantly.net
License: GPL2
*/
// Security Check
if (!defined('ABSPATH')) {
die();
}
//////////////////////////////////////////////////////////////////////////////
// Create Shortcode for Site Schema
//////////////////////////////////////////////////////////////////////////////
add_shortcode('bww_site_schema', 'bww_site_schema_shortcode');
function bww_site_schema_shortcode() {
$all_options = get_option('site-info');
// Extract additional fields
$description = $all_options['bww_info_description'] ?? '';
$google_map_url = $all_options['bww_info_google_map_share_url'] ?? '';
$contact_phone = $all_options['bww_info_phone'] ?? '';
$contact_email = $all_options['bww_info_email'] ?? '';
$schema_type = $all_options['bww_info_schema_type'] ?? 'LocalBusiness';
$business_name = $all_options['bww_info_business_name'] ?? '';
$address_1 = $all_options['bww_info_address_1'] ?? '';
$address_2 = $all_options['bww_info_address_2'] ?? '';
$city = $all_options['bww_info_city'] ?? '';
$state_abbrev = $all_options['bww_info_state_abbrev'] ?? '';
$postal_code = $all_options['bww_info_postal_code'] ?? '';
$phone = $all_options['bww_info_phone'] ?? '';
$email = $all_options['bww_info_email'] ?? '';
$price_range = $all_options['bww_info_schema_price'] ?? '';
$latitude = $all_options['bww_info_latitude'] ?? '';
$longitude = $all_options['bww_info_longitude'] ?? '';
$start_date = $all_options['bww_info_start_date'] ?? '';
$time_zone = $all_options['bww_info_time_zone'] ?? '';
$logo_id = $all_options['bww_info_logo'] ?? '';
$logo_square_id = $all_options['bww_info_logo_square'] ?? '';
$logo_4x3_id = $all_options['bww_info_logo_4x3'] ?? '';
$logo_16x9_id = $all_options['bww_info_logo_16x9'] ?? '';
$social_links = [
'facebook' => $all_options['bww_info_facebook'] ?? '',
'x' => $all_options['bww_info_x'] ?? '',
'instagram' => $all_options['bww_info_instagram'] ?? '',
'linkedin' => $all_options['bww_info_linkedin'] ?? '',
'youtube' => $all_options['bww_info_youtube'] ?? '',
];
$site_url = site_url();
// Get logo URLs from IDs
$logo_url = !empty($logo_id) ? wp_get_attachment_url($logo_id) : '';
$logo_square_url = !empty($logo_square_id) ? wp_get_attachment_url($logo_square_id) : '';
$logo_4x3_url = !empty($logo_4x3_id) ? wp_get_attachment_url($logo_4x3_id) : '';
$logo_16x9_url = !empty($logo_16x9_id) ? wp_get_attachment_url($logo_16x9_id) : '';
// Opening hours
$hours = [
'Mo' => $all_options['bww_info_hours_monday'] ?? '',
'Tu' => $all_options['bww_info_hours_tuesday'] ?? '',
'We' => $all_options['bww_info_hours_wednesday'] ?? '',
'Th' => $all_options['bww_info_hours_thursday'] ?? '',
'Fr' => $all_options['bww_info_hours_friday'] ?? '',
'Sa' => $all_options['bww_info_hours_saturday'] ?? '',
'Su' => $all_options['bww_info_hours_sunday'] ?? '',
];
// Format opening hours
$opening_hours = [];
foreach ($hours as $day => $time) {
if (!empty($time)) {
$opening_hours[] = "{$day} {$time}";
}
}
ob_start();
?>
<!-- Begin Schema.org from BWW Site Info -->
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "<?php echo esc_js($schema_type); ?>",
"name": "<?php echo esc_js($business_name); ?>",
"description": "<?php echo esc_js($description); ?>",
"address": {
"@type": "PostalAddress",
"streetAddress": "<?php echo esc_js($address_1 . ' ' . $address_2); ?>",
"addressLocality": "<?php echo esc_js($city); ?>",
"addressRegion": "<?php echo esc_js($state_abbrev); ?>",
"postalCode": "<?php echo esc_js($postal_code); ?>",
"addressCountry": "US"
},
"hasMap": "<?php echo esc_url($google_map_url); ?>",
"telephone": "<?php echo esc_js($phone); ?>",
"email": "<?php echo esc_js($email); ?>",
"geo": {
"@type": "GeoCoordinates",
"latitude": "<?php echo esc_js($latitude); ?>",
"longitude": "<?php echo esc_js($longitude); ?>"
},
<?php if (!empty($opening_hours)) : ?>
"openingHours": [
<?php echo '"' . implode('","', array_map('esc_js', $opening_hours)) . '"'; ?>
],
<?php endif; ?>
"priceRange": "<?php echo esc_js($price_range); ?>",
<?php if (!empty($start_date)) : ?>
"foundingDate": "<?php echo esc_js($start_date); ?>",
<?php endif; ?>
<?php if (!empty($time_zone)) : ?>
"timeZone": "<?php echo esc_js($time_zone); ?>",
<?php endif; ?>
<?php if (!empty($logo_url)) : ?>
"logo": "<?php echo esc_url($logo_url); ?>",
<?php endif; ?>
<?php if (!empty($logo_square_url) || !empty($logo_4x3_url) || !empty($logo_16x9_url)) : ?>
"image": [
<?php
$images = array_filter([$logo_square_url, $logo_4x3_url, $logo_16x9_url]);
echo '"' . implode('","', array_map('esc_url', $images)) . '"';
?>
],
<?php endif; ?>
<?php if (!empty(array_filter($social_links))) : ?>
"sameAs": [
<?php
$links = array_filter($social_links);
echo '"' . implode('","', array_map('esc_url', $links)) . '"';
?>
],
<?php endif; ?>
"contactPoint": {
"@type": "ContactPoint",
"telephone": "<?php echo esc_js($contact_phone); ?>",
"contactType": "Customer Service",
"email": "<?php echo esc_js($contact_email); ?>"
},
"url": "<?php echo esc_url($site_url); ?>"
}
</script>
<!-- End Schema.org from BWW Site Info -->
<?php
return ob_get_clean();
}
//////////////////////////////////////////////////////////////////////////////
// Add GA and GSC Code to <head> from Site Settings
//////////////////////////////////////////////////////////////////////////////
add_action('wp_head', function () {
// Retrieve the fields from the Meta Box settings page
$all_options = get_option('site-info');
$ga_tag = $all_options['bww_info_ga_tag'] ?? '';
$gsc_validation = $all_options['bww_info_gsc_validation'] ?? '';
// Output the fields, if they contain values
if (!empty($ga_tag) || !empty($gsc_validation)) {
echo "\n<!-- Begin Injected Meta Tags -->\n";
if (!empty($ga_tag)) {
echo "<!-- Begin GA Code from Site Info -->\n";
echo $ga_tag . "\n";
echo "<!-- End GA Code from Site Info -->\n";
}
if (!empty($gsc_validation)) {
echo "<!-- Begin GSC Code from Site Info -->\n";
echo $gsc_validation . "\n";
echo "<!-- End GSC Code from Site Info -->\n";
}
echo "<!-- End Injected Meta Tags -->\n";
}
}, 99);
//////////////////////////////////////////////////////////////////////////////
// Change Default GF Send To Email to the one in Site Info
//////////////////////////////////////////////////////////////////////////////
add_filter('gform_notification', function ($notification, $form, $entry) {
// Retrieve the replacement email from the Meta Box settings
$all_options = get_option('site-info');
$replacement_email = $all_options['bww_info_form_email'] ?? '';
// Validate the replacement email
if (!empty($replacement_email) && is_email($replacement_email)) {
// Check if the "Send To Email" field contains the {admin_email} tag
if (strpos($notification['to'], '{admin_email}') !== false) {
// Replace {admin_email} with the replacement email
$notification['to'] = str_replace('{admin_email}', $replacement_email, $notification['to']);
}
}
return $notification;
}, 10, 3);
//////////////////////////////////////////////////////////////////////////////
// Dynamic Copyright Date Based on Start Date and Biz Name
// Usage: [bww-copyright]
//////////////////////////////////////////////////////////////////////////////
// Shortcode to render a dynamic copyright statement
function bww_copyright_shortcode() {
// Retrieve the start year and business name from the Meta Box custom fields
$all_options = get_option('site-info');
$start_year = $all_options['bww_info_start_date'] ?? '';
$business_name = $all_options['bww_info_business_name'] ?? '';
// Validate business name
if (empty($business_name)) {
$business_name = __('Your Business Name', 'bww_copyright'); // Default fallback
}
// Validate start year
$output_start_year = '';
if (!empty($start_year) && is_numeric($start_year) && strlen($start_year) === 4) {
$output_start_year = (int)$start_year;
}
// Current year
$current_year = date('Y');
// Generate the copyright statement
$copyright = '&copy; Copyright ';
$copyright .= $output_start_year ? $output_start_year . '-' : '';
$copyright .= $current_year . ' ';
$copyright .= esc_html($business_name) . '. All Rights Reserved.';
return $copyright;
}
// Register the shortcode
add_shortcode('bww-copyright', 'bww_copyright_shortcode');
//////////////////////////////////////////////////////////////////////////////
// Clickable Main Phone Number from Site Info
// usage [phone]
//////////////////////////////////////////////////////////////////////////////
function bww_phone_shortcode() {
// Retrieve phone fields from the Meta Box settings page
$all_options = get_option('site-info');
$phone_number = $all_options['bww_info_phone'] ?? '';
$phone_link = $all_options['bww_info_phone_link'] ?? '';
// Validate the phone number and phone link
if (empty($phone_number) || empty($phone_link)) {
return __('Phone number not available.', 'bww_phone_shortcode');
}
// Return the phone number wrapped in a clickable <a> tag
return '<a style="text-decoration:none;" href="tel:' . esc_attr($phone_link) . '">' . esc_html($phone_number) . '</a>';
}
// Register the shortcode
add_shortcode('phone', 'bww_phone_shortcode');
//////////////////////////////////////////////////////////////////////////////
// Display Business Hours in a Table
// usage [business-hours]
//////////////////////////////////////////////////////////////////////////////
// Shortcode to display business hours
function bww_business_hours_shortcode() {
// Retrieve the business hours from the Meta Box settings page
$all_options = get_option('site-info');
$hours = [
'Monday' => $all_options['bww_info_hours_monday'] ?? '',
'Tuesday' => $all_options['bww_info_hours_tuesday'] ?? '',
'Wednesday' => $all_options['bww_info_hours_wednesday'] ?? '',
'Thursday' => $all_options['bww_info_hours_thursday'] ?? '',
'Friday' => $all_options['bww_info_hours_friday'] ?? '',
'Saturday' => $all_options['bww_info_hours_saturday'] ?? '',
'Sunday' => $all_options['bww_info_hours_sunday'] ?? '',
];
// Function to convert 24-hour time to 12-hour time with AM/PM
function convert_to_am_pm($time_range) {
if (empty($time_range) || strtolower($time_range) === 'closed') {
return 'Closed';
}
$times = explode('-', $time_range);
if (count($times) === 2) {
$start = date("g:i A", strtotime($times[0]));
$end = date("g:i A", strtotime($times[1]));
return "$start - $end";
}
return 'Invalid Time Format';
}
// Generate the business hours table
$output = '<table class="business-hours" style="width:auto;border-collapse: collapse;">';
foreach ($hours as $day => $time_range) {
$formatted_time = convert_to_am_pm($time_range);
$output .= '<tr>';
$output .= '<td style="padding: 5px 10px 5px 5px; text-align: left;"><strong>' . esc_html($day) . '</strong></td>';
$output .= '<td style="padding: 5px 10px 5px 5px; text-align: left;">' . esc_html($formatted_time) . '</td>';
$output .= '</tr>';
}
$output .= '</table>';
return $output;
}
// Register the shortcode
add_shortcode('business-hours', 'bww_business_hours_shortcode');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment