Skip to content

Instantly share code, notes, and snippets.

@darinronne
Last active June 9, 2023 12:54
Show Gist options
  • Save darinronne/7f041d5c949c01ffc8956c84bfba40bb to your computer and use it in GitHub Desktop.
Save darinronne/7f041d5c949c01ffc8956c84bfba40bb to your computer and use it in GitHub Desktop.
Additional Organization Schema Data when using Yoast and ACF in Wordpress
<?php
/**
* ACF Additional Organization Schema Data
*
* Inserts "Alternate Organization Names" into the existing Organization JSON that Yoast creates.
* And creates new Location Schema JSON that is added to HEAD of the site.
* Tags: ACF, Advanced Custom Fields, Yoast, SEO, Local, Locations, Schema, Wordpress
*
* @package Meta13_Functionality
* @since 1.0.0
*
* If you need to edit the ACF "Additional Organization Schema" field group.
*
* 1. First output the $acf_additional_organization_schema array as JSON.
* echo '<pre>' . wp_json_encode( $acf_additional_organization_schema ) . '</pre>'; die;
* 2. Save the output to a .json file.
* 3. Comment out "acf_add_local_field_group( $acf_additional_organization_schema );", so that it won't conflict with your field group changes.
* 4. Using the Custom Fields > Tools page, import the JSON file.
* 5. Make your changes. Do your testing.
* 6. Using the Custom Fields > Tools page, export the "Additional Organization Schema" field group as PHP.
* 7. Replace the contents of the $acf_additional_organization_schema array with that of the new array.
* 8. Remove the comment from "acf_add_local_field_group( $acf_additional_organization_schema );".
*/
/**
* The "Additional Organization Schema" functions
*/
function meta13_add_additional_organization_schema() {
// Only continue if ACF and YOAST are installed and activate.
if ( ! class_exists( 'acf' ) || ! class_exists( 'WPSEO_Options' ) ) {
return false;
}
// Add "Additional Organization Schema" options page underneath "General Options" menu item.
// "General Options" is created in acf.php.
if ( function_exists( 'acf_add_options_page' ) ) {
acf_add_options_sub_page(
array(
'page_title' => 'Additional Organization Schema',
'menu_title' => 'Add\'l Org. Schema',
'parent_slug' => 'general-options',
)
);
}
// Defining the ACF "Additional Organization Schema" field group and fields.
$acf_additional_organization_schema = array(
// START.
'key' => 'group_6bedcb8fa6f50',
'title' => 'Additional Organization Schema',
'fields' => array(
array(
'key' => 'field_6bedf300da901',
'label' => 'What is this?',
'name' => '',
'type' => 'message',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'message' => 'The data on this settings page is used to supplement Yoast SEO\'s Schema data, which is set here: <a href="/wp-admin/admin.php?page=wpseo_titles">Search Appearance</a> and <a href="/wp-admin/admin.php?page=wpseo_social">Social</a>. <br><br>Schema data is used by search engines to better understand your content and provide better search results. Schema data may also be used by search engines to enhance its appearance in their search results.',
'new_lines' => 'wpautop',
'esc_html' => 0,
),
array(
'key' => 'field_6beedffc5cfab',
'label' => 'Alternate Organization Names',
'name' => 'alternate_names',
'type' => 'repeater',
'instructions' => 'If your organization name is searched often with different variations, include those names here.',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => 0,
'max' => 0,
'layout' => 'table',
'button_label' => 'Add Name',
'sub_fields' => array(
array(
'key' => 'field_6beee01a5cfac',
'label' => 'Name',
'name' => 'name',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
),
),
array(
'key' => 'field_6bede50431cb4',
'label' => 'Locations',
'name' => 'locations',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => 0,
'max' => 0,
'layout' => 'block',
'button_label' => 'Add Location',
'sub_fields' => array(
array(
'key' => 'field_6bede9a9f0b3c',
'label' => 'General',
'name' => '',
'type' => 'tab',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'placement' => 'top',
'endpoint' => 0,
),
array(
'key' => 'field_5cf7c7bd455a8',
'label' => 'Location Name',
'name' => 'location_name',
'type' => 'text',
'instructions' => 'This is almost always the same as the Organization Name.',
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '33',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_6bedcb8fb135f',
'label' => 'Local Business Type',
'name' => 'business_type',
'type' => 'select',
'instructions' => '',
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '33',
'class' => '',
'id' => '',
),
'choices' => array(
'LocalBusiness' => 'Local Business',
'AccountingService' => 'Accounting Service',
'AdultEntertainment' => 'Adult Entertainment',
'AmusementPark' => 'Amusement Park',
'AnimalShelter' => 'Animal Shelter',
'AutomatedTeller' => 'Automated Teller',
'AutomotiveBusiness' => 'Automotive Business',
'ArtGallery' => 'Art Gallery',
'AutoDealer' => 'Auto Dealer',
'AutoPartsStore' => 'Auto Parts Store',
'AutoRental' => 'Auto Rental',
'Attorney' => 'Attorney',
'AutoRepair' => 'Auto Repair',
'AutoWash' => 'Auto Wash',
'Bakery' => 'Bakery',
'BankOrCreditUnion' => 'Bank Or Credit Union',
'BarOrPub' => 'Bar Or Pub',
'BedAndBreakfast' => 'Bed And Breakfast',
'BeautySalon' => 'Beauty Salon',
'BikeStore' => 'Bike Store',
'BookStore' => 'Book Store',
'BowlingAlley' => 'Bowling Alley',
'Brewery' => 'Brewery',
'CafeOrCoffeeShop' => 'Cafe Or Coffee Shop',
'Casino' => 'Casino',
'ChildCare' => 'Child Care',
'ClothingStore' => 'Clothing Store',
'ComedyClub' => 'Comedy Club',
'ComputerStore' => 'Computer Store',
'ConvenienceStore' => 'Convenience Store',
'DaySpa' => 'Day Spa',
'Dentist' => 'Dentist',
'DepartmentStore' => 'Department Store',
'DiagnosticLab' => 'Diagnostic Lab',
'DryCleaningOrLaundry' => 'Dry Cleaning Or Laundry',
'Electrician' => 'Electrician',
'ElectronicsStore' => 'Electronics Store',
'EmergencyService' => 'Emergency Service',
'EmploymentAgency' => 'Employment Agency',
'EntertainmentBusiness' => 'Entertainment Business',
'ExerciseGym' => 'Exercise Gym',
'FastFoodRestaurant' => 'Fast Food Restaurant',
'FireStation' => 'Fire Station',
'Florist' => 'Florist',
'FoodEstablishment' => 'Food Establishment',
'FurnitureStore' => 'Furniture Store',
'GasStation' => 'Gas Station',
'GovernmentOffice' => 'Government Office',
'GardenStore' => 'Garden Store',
'GeneralContractor' => 'General Contractor',
'GolfCourse' => 'Golf Course',
'GroceryStore' => 'Grocery Store',
'HardwareStore' => 'Hardware Store',
'HealthAndBeautyBusiness' => 'Health And Beauty Business',
'HairSalon' => 'Hair Salon',
'HealthClub' => 'Health Club',
'HobbyShop' => 'Hobby Shop',
'HomeAndConstructionBusiness' => 'Home And Construction Business',
'HomeGoodsStore' => 'Home Goods Store',
'Hospital' => 'Hospital',
'Hostel' => 'Hostel',
'Hotel' => 'Hotel',
'HVACBusiness' => 'HVAC Business',
'HousePainter' => 'House Painter',
'IceCreamShop' => 'Ice Cream Shop',
'InsuranceAgency' => 'Insurance Agency',
'InternetCafe' => 'Internet Cafe',
'JewelryStore' => 'Jewelry Store',
'Library' => 'Library',
'LiquorStore' => 'Liquor Store',
'Locksmith' => 'Locksmith',
'LodgingBusiness' => 'Lodging Business',
'MedicalClinic' => 'Medical Clinic',
'MensClothingStore' => 'Mens Clothing Store',
'MobilePhoneStore' => 'Mobile Phone Store',
'Motel' => 'Motel',
'MotorcycleRepair' => 'Motorcycle Repair',
'MotorcycleDealer' => 'Motorcycle Dealer',
'MovieRentalStore' => 'Movie Rental Store',
'MovieTheater' => 'Movie Theater',
'MusicStore' => 'Music Store',
'NailSalon' => 'Nail Salon',
'NightClub' => 'Night Club',
'Notary' => 'Notary',
'OfficeEquipmentStore' => 'Office Equipment Store',
'Optician' => 'Optician',
'OutletStore' => 'Outlet Store',
'PawnShop' => 'Pawn Shop',
'PetStore' => 'Pet Store',
'Plumber' => 'Plumber',
'Pharmacy' => 'Pharmacy',
'Physician' => 'Physician',
'PoliceStation' => 'Police Station',
'PostOffice' => 'Post Office',
'ProfessionalService' => 'Professional Service',
'PublicSwimmingPool' => 'Public Swimming Pool',
'RadioStation' => 'Radio Station',
'RealEstateAgent' => 'Real Estate Agent',
'RecyclingCenter' => 'Recycling Center',
'Restaurant' => 'Restaurant',
'RoofingContractor' => 'Roofing Contractor',
'SelfStorage' => 'Self Storage',
'ShoeStore' => 'Shoe Store',
'ShoppingCenter' => 'Shopping Center',
'SkiResort' => 'Ski Resort',
'SportsActivityLocation' => 'Sports Activity Location',
'SportingGoodsStore' => 'Sporting Goods Store',
'SportsClub' => 'Sports Club',
'StadiumOrArena' => 'Stadium Or Arena',
'Store' => 'Store',
'TattooParlor' => 'Tattoo Parlor',
'TennisComplex' => 'Tennis Complex',
'Tire Shop' => 'Tire Shop',
'Toy Shop' => 'Toy Shop',
'TelevisionStation' => 'Television Station',
'TouristInformationCenter' => 'Tourist Information Center',
'TravelAgency' => 'Travel Agency',
'VeterinaryCare' => 'Veterinary Care',
'WholesaleStore' => 'Wholesale Store',
'Winery' => 'Winery',
),
'default_value' => array(
0 => 'LocalBusiness',
),
'allow_null' => 0,
'multiple' => 0,
'ui' => 1,
'ajax' => 0,
'return_format' => 'value',
'placeholder' => '',
),
array(
'key' => 'field_6bedcb8fb12d3',
'label' => 'Photo of Building',
'name' => 'location_image',
'type' => 'image',
'instructions' => 'Resolution max: 1920x1920',
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '33',
'class' => '',
'id' => '',
),
'return_format' => 'url',
'preview_size' => 'thumbnail',
'library' => 'all',
'min_width' => '',
'min_height' => '',
'min_size' => '',
'max_width' => 1920,
'max_height' => 1920,
'max_size' => '',
'mime_types' => 'jpg, png',
),
array(
'key' => 'field_6bedcb8fb13f1',
'label' => 'Address',
'name' => '',
'type' => 'tab',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'placement' => 'top',
'endpoint' => 0,
),
array(
'key' => 'field_6bedcb8fb1475',
'label' => 'Street',
'name' => 'address_street',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_6bedcb8fb14f7',
'label' => 'City',
'name' => 'address_locality',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '50',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_6bedcb8fb157a',
'label' => 'State/Region',
'name' => 'address_region',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '25',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_6bedcb8fb15fd',
'label' => 'Postal Code',
'name' => 'address_postal',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '25',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_6bedcb8fb167e',
'label' => 'Country',
'name' => 'address_country',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_6bedefbed49bd',
'label' => 'Map URL',
'name' => 'map_url',
'type' => 'url',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
),
array(
'key' => 'field_6bedcb8fb1700',
'label' => 'Openings',
'name' => '',
'type' => 'tab',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'placement' => 'top',
'endpoint' => 0,
),
array(
'key' => 'field_6bedcb8fb1782',
'label' => 'Opening Hours',
'name' => 'opening_hours',
'type' => 'repeater',
'instructions' => 'Open all day: 00:00 - 23:59',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => 'field_698d63f15337d',
'min' => 0,
'max' => 7,
'layout' => 'table',
'button_label' => 'Add Day',
'sub_fields' => array(
array(
'key' => 'field_6bedcb8fdad53',
'label' => 'Days',
'name' => 'days',
'type' => 'select',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '40',
'class' => '',
'id' => '',
),
'choices' => array(
'Mo' => 'Mo',
'Tu' => 'Tu',
'We' => 'We',
'Th' => 'Th',
'Fr' => 'Fr',
'Sa' => 'Sa',
'Su' => 'Su',
),
'default_value' => array(
),
'allow_null' => 0,
'multiple' => 1,
'ui' => 1,
'ajax' => 0,
'return_format' => 'value',
'placeholder' => '',
),
array(
'key' => 'field_6bedcb8fdae1f',
'label' => 'from',
'name' => 'from',
'type' => 'time_picker',
'instructions' => '',
'required' => 0,
'conditional_logic' => array(
array(
array(
'field' => 'field_6bedcb8fdaf78',
'operator' => '!=',
'value' => '1',
),
),
),
'wrapper' => array(
'width' => '20',
'class' => '',
'id' => '',
),
'display_format' => 'g:i a',
'return_format' => 'H:i',
),
array(
'key' => 'field_6bedcb8fdaecc',
'label' => 'to',
'name' => 'to',
'type' => 'time_picker',
'instructions' => '',
'required' => 0,
'conditional_logic' => array(
array(
array(
'field' => 'field_6bedcb8fdaf78',
'operator' => '!=',
'value' => '1',
),
),
),
'wrapper' => array(
'width' => '20',
'class' => '',
'id' => '',
),
'display_format' => 'g:i a',
'return_format' => 'H:i',
),
array(
'key' => 'field_6bedcb8fdaf78',
'label' => 'Closed',
'name' => 'closed',
'type' => 'true_false',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '20',
'class' => '',
'id' => '',
),
'message' => '',
'default_value' => 0,
'ui' => 1,
'ui_on_text' => '',
'ui_off_text' => '',
),
),
),
array(
'key' => 'field_6bedcb8fb1806',
'label' => 'Special Days',
'name' => 'special_days',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => 0,
'max' => 0,
'layout' => 'table',
'button_label' => 'Add Date',
'sub_fields' => array(
array(
'key' => 'field_6bedcb8feed0a',
'label' => 'Date: from',
'name' => 'date_from',
'type' => 'date_picker',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '20',
'class' => '',
'id' => '',
),
'display_format' => 'M j, Y',
'return_format' => 'Y-m-d',
'first_day' => 1,
),
array(
'key' => 'field_6bedcb8feed9f',
'label' => 'Date: to',
'name' => 'date_to',
'type' => 'date_picker',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '20',
'class' => '',
'id' => '',
),
'display_format' => 'M j, Y',
'return_format' => 'Y-m-d',
'first_day' => 1,
),
array(
'key' => 'field_6bedcb8feee29',
'label' => 'Time: from',
'name' => 'time_from',
'type' => 'time_picker',
'instructions' => '',
'required' => 0,
'conditional_logic' => array(
array(
array(
'field' => 'field_6bedcb8feef36',
'operator' => '!=',
'value' => '1',
),
),
),
'wrapper' => array(
'width' => '20',
'class' => '',
'id' => '',
),
'display_format' => 'g:i a',
'return_format' => 'H:i',
),
array(
'key' => 'field_6bedcb8feeeb0',
'label' => 'Time: to',
'name' => 'time_to',
'type' => 'time_picker',
'instructions' => '',
'required' => 0,
'conditional_logic' => array(
array(
array(
'field' => 'field_6bedcb8feef36',
'operator' => '!=',
'value' => '1',
),
),
),
'wrapper' => array(
'width' => '20',
'class' => '',
'id' => '',
),
'display_format' => 'g:i a',
'return_format' => 'H:i',
),
array(
'key' => 'field_6bedcb8feef36',
'label' => 'Closed',
'name' => 'closed',
'type' => 'true_false',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '20',
'class' => '',
'id' => '',
),
'message' => '',
'default_value' => 0,
'ui' => 1,
'ui_on_text' => 'Yes',
'ui_off_text' => 'No',
),
),
),
array(
'key' => 'field_6bedcb8fb1887',
'label' => 'Contact',
'name' => '',
'type' => 'tab',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'placement' => 'top',
'endpoint' => 0,
),
array(
'key' => 'field_6bedcb8fb1909',
'label' => 'Phone',
'name' => 'company_phone',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '33',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '+1',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_6bedcb8fb1991',
'label' => 'Fax Number',
'name' => 'fax_number',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '33',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '+1',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_6bedcb8fb1a12',
'label' => 'Email',
'name' => 'email',
'type' => 'email',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '33',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
),
array(
'key' => 'field_6bedcb8fb1a93',
'label' => 'Contact',
'name' => 'contact',
'type' => 'repeater',
'instructions' => 'Phone numbers for special sections (Sales, Support, Toll Free)',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => 'field_698d6ce7f5325',
'min' => 0,
'max' => 0,
'layout' => 'table',
'button_label' => 'Add Contact',
'sub_fields' => array(
array(
'key' => 'field_6bedcb901e968',
'label' => 'Type',
'name' => 'type',
'type' => 'select',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '30',
'class' => '',
'id' => '',
),
'choices' => array(
'customer support' => 'customer support',
'technical support' => 'technical support',
'billing support' => 'billing support',
'bill payment' => 'bill payment',
'sales' => 'sales',
'reservations' => 'reservations',
'credit card support' => 'credit card support',
'emergency' => 'emergency',
'baggage tracking' => 'baggage tracking',
'roadside assistance' => 'roadside assistance',
'package tracking' => 'package tracking',
),
'default_value' => array(
),
'allow_null' => 1,
'multiple' => 0,
'ui' => 1,
'ajax' => 0,
'return_format' => 'value',
'placeholder' => '',
),
array(
'key' => 'field_6bedcb901e9ff',
'label' => 'Phone',
'name' => 'phone',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '40',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '+1',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_6bedcb901ea8b',
'label' => 'Option',
'name' => 'option',
'type' => 'select',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '30',
'class' => '',
'id' => '',
),
'choices' => array(
'HearingImpairedSupported' => 'Hearing Impaired Supported',
'TollFree' => 'Toll Free',
),
'default_value' => array(
),
'allow_null' => 1,
'multiple' => 1,
'ui' => 1,
'ajax' => 0,
'return_format' => 'value',
'placeholder' => '',
),
),
),
),
),
),
'location' => array(
array(
array(
'param' => 'options_page',
'operator' => '==',
'value' => 'acf-options-addl-org-schema',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'seamless',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
// END.
);
// echo '<pre>' . wp_json_encode( $acf_additional_organization_schema ) . '</pre>'; die; // 1.
// Create the ACF fields in the WP admin using the $acf_additional_organization_schema array defined above.
// This however doesn't create a field group on the "Custom Fields" page, so you can't edit the field group in the admin.
// Instructions for editing the field group is in the docblock.
if ( function_exists( 'acf_add_local_field_group' ) ) {
acf_add_local_field_group( $acf_additional_organization_schema ); // 3.
}
// Create the schema data JSON and add to the HEAD of the site.
add_action( 'wp_head', 'meta13_build_locations_schema_data' );
function meta13_build_locations_schema_data() {
$schema = array(
'@context' => 'http://schema.org',
);
// Locations.
if ( have_rows( 'locations', 'option' ) ) {
$x = 0;
while ( have_rows( 'locations', 'option' ) ) :
the_row();
// Type.
$schema['@graph'][ $x ]['@type'] = get_sub_field( 'business_type' );
// Parent Organization.
$schema['@graph'][ $x ]['parentOrganization'] = array( '@id' => get_home_url() . '/#organization' );
// Name.
$schema['@graph'][ $x ]['name'] = get_sub_field( 'location_name', 'option' );
// Alternate Names.
// Don't know if I need to add to each location.
// if ( have_rows( 'alternate_names', 'option' ) ) {
// while ( have_rows( 'alternate_names', 'option' ) ) :
// the_row();
// $schema['@graph'][ $x ]['alternateName'][] = get_sub_field( 'name' );
// endwhile;
// }
// Phone.
if ( get_sub_field( 'company_phone' ) ) {
$schema['@graph'][ $x ]['telephone'] = '+1' . get_sub_field( 'company_phone' );
}
// Fax.
if ( get_sub_field( 'fax_number' ) ) {
$schema['@graph'][ $x ]['faxNumber'] = '+1' . get_sub_field( 'fax_number' );
}
// Email.
if ( get_sub_field( 'email' ) ) {
$schema['@graph'][ $x ]['email'] = get_sub_field( 'email' );
}
// Address.
$schema['@graph'][ $x ]['address']['@type'] = 'PostalAddress';
$schema['@graph'][ $x ]['address']['streetAddress'] = get_sub_field( 'address_street' );
$schema['@graph'][ $x ]['address']['addressLocality'] = get_sub_field( 'address_locality' );
$schema['@graph'][ $x ]['address']['addressRegion'] = get_sub_field( 'address_region' );
$schema['@graph'][ $x ]['address']['postalCode'] = get_sub_field( 'address_postal' );
$schema['@graph'][ $x ]['address']['addressCountry'] = get_sub_field( 'address_country' );
// Map URL.
if ( get_sub_field( 'map_url' ) ) {
$schema['@graph'][ $x ]['hasMap'] = get_sub_field( 'map_url' );
}
// Building Photo.
if ( get_sub_field( 'location_image' ) ) {
$schema['@graph'][ $x ]['image'] = get_sub_field( 'location_image' );
}
// Opening Hours.
if ( have_rows( 'opening_hours', 'option' ) ) {
while ( have_rows( 'opening_hours', 'option' ) ) :
the_row();
$closed = get_sub_field( 'closed' );
$from = $closed ? '00:00' : get_sub_field( 'from' );
$to = $closed ? '00:00' : get_sub_field( 'to' );
$openings = array(
'@type' => 'OpeningHoursSpecification',
'dayOfWeek' => get_sub_field( 'days' ),
'opens' => $from,
'closes' => $to,
);
$schema['@graph'][ $x ]['openingHoursSpecification'][] = $openings;
$openings = '';
endwhile;
}
// Special Days / Holidays.
if ( have_rows( 'special_days', 'option' ) ) {
while ( have_rows( 'special_days', 'option' ) ) :
the_row();
$closed = get_sub_field( 'closed' );
$date_from = get_sub_field( 'date_from' );
$date_to = get_sub_field( 'date_to' );
$time_from = $closed ? '00:00' : get_sub_field( 'time_from' );
$time_to = $closed ? '00:00' : get_sub_field( 'time_to' );
$special_days = array(
'@type' => 'OpeningHoursSpecification',
'validFrom' => $date_from,
'validThrough' => $date_to,
'opens' => $time_from,
'closes' => $time_to,
);
$schema['@graph'][ $x ]['openingHoursSpecification'][] = $special_days;
$openings = '';
endwhile;
}
// Special Phone Numbers.
if ( have_rows( 'contact', 'options' ) ) {
while ( have_rows( 'contact', 'options' ) ) :
the_row();
$contacts = array(
'@type' => 'ContactPoint',
'contactType' => get_sub_field( 'type' ),
'telephone' => '+1' . get_sub_field( 'phone' ),
);
if ( get_sub_field( 'option' ) ) {
$contacts['contactOption'] = get_sub_field( 'option' );
}
$schema['@graph'][ $x ]['contactPoint'][] = $contacts;
$contacts = '';
endwhile;
}
// Increment the count.
$x++;
endwhile;
}
echo '<script type="application/ld+json">' . wp_json_encode( $schema ) . '</script>';
}
// Add Alternate Names to the existing Yoast Organization Schema.
add_filter( 'wpseo_schema_organization', 'meta13_add_data_to_yoast_schema_output' );
function meta13_add_data_to_yoast_schema_output( $data ) {
// Alternate Names.
if ( have_rows( 'alternate_names', 'option' ) ) {
while ( have_rows( 'alternate_names', 'option' ) ) :
the_row();
$data['alternateName'][] = get_sub_field( 'name' );
endwhile;
}
return $data;
}
}
// Because this is in a mu-plugin, you have to wait for ACF to load.
add_action( 'plugins_loaded', 'meta13_add_additional_organization_schema' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment