Skip to content

Instantly share code, notes, and snippets.

@patrickatwsrn
Last active April 4, 2023 10:52
Show Gist options
  • Save patrickatwsrn/65aebe394dd072f994ba576ae84bb18b to your computer and use it in GitHub Desktop.
Save patrickatwsrn/65aebe394dd072f994ba576ae84bb18b to your computer and use it in GitHub Desktop.
Wordpress ACF Opening hours

topic

Simple ACF Option Page to handle opening hours inside a dedicated option page.

Installation:

1. Create ACF options page

add include('acf/acf-options-page.php') to functions.php

2. Import Fieldgroup into ACF

Got to Dashboard->ACF->Tools. Use import json on the right side to import acf-opening-hours.json

3. Create output shortcode

add include('acf/shortcode-opening-hours.php') to functions.php

4##. Add shortcode Add [opening_hours] to a page, or a html customizer field.

User Interace

image

Result

image

/* exported from acf pro */
[
{
"key": "group_642af70225c29",
"title": "Öffnungszeiten",
"fields": [
{
"show_column_filter": false,
"allow_bulkedit": 0,
"allow_quickedit": 0,
"show_column": 0,
"show_column_weight": 1000,
"show_column_sortable": false,
"key": "field_642be38acbf64",
"label": "Überschrift",
"name": "header",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"maxlength": "",
"placeholder": "",
"prepend": "",
"append": ""
},
{
"show_column_filter": false,
"allow_bulkedit": false,
"allow_quickedit": false,
"show_column": false,
"show_column_weight": 1000,
"show_column_sortable": false,
"key": "field_642af702d7f50",
"label": "Wochentag",
"name": "weekday",
"aria-label": "",
"type": "repeater",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"layout": "table",
"pagination": 0,
"min": 0,
"max": 0,
"collapsed": "",
"button_label": "Add Row",
"rows_per_page": 20,
"sub_fields": [
{
"show_column_filter": 0,
"allow_bulkedit": 0,
"allow_quickedit": 0,
"show_column": 0,
"show_column_weight": 1000,
"show_column_sortable": false,
"key": "field_642af74ee5e65",
"label": "Tag",
"name": "tag",
"aria-label": "",
"type": "select",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "33",
"class": "",
"id": ""
},
"choices": {
"Mo": "Montag",
"Di": "Dienstag",
"Mi": "Mittwoch",
"Do": "Donnerstag",
"Fr": "Freitag",
"Sa": "Samstag",
"So": "Sonntag"
},
"default_value": false,
"return_format": "array",
"multiple": 0,
"allow_null": 0,
"ui": 0,
"ajax": 0,
"placeholder": "",
"parent_repeater": "field_642af702d7f50"
},
{
"show_column_filter": false,
"allow_bulkedit": false,
"allow_quickedit": false,
"show_column": false,
"show_column_weight": 1000,
"show_column_sortable": false,
"key": "field_642af8a594fb5",
"label": "Öffnungszeiten",
"name": "oeffnungszeiten",
"aria-label": "",
"type": "repeater",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "66",
"class": "",
"id": ""
},
"layout": "table",
"min": 0,
"max": 0,
"collapsed": "",
"button_label": "Add Row",
"rows_per_page": 20,
"sub_fields": [
{
"show_column_filter": false,
"allow_bulkedit": false,
"allow_quickedit": false,
"show_column": false,
"show_column_weight": 1000,
"show_column_sortable": false,
"key": "field_642af8f994fb6",
"label": "Öffnungszeit",
"name": "von_-_bis",
"aria-label": "",
"type": "group",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"layout": "block",
"sub_fields": [
{
"show_column_filter": 0,
"allow_bulkedit": 0,
"allow_quickedit": 0,
"show_column": 0,
"show_column_weight": 1000,
"show_column_sortable": false,
"key": "field_642af96294fb7",
"label": "Geöffnet",
"name": "geoffnet",
"aria-label": "",
"type": "select",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "50",
"class": "",
"id": ""
},
"choices": {
"Geoeffnet": "Geöffnet",
"Geschlossen": "Geschlossen"
},
"default_value": "Geöffnet",
"return_format": "label",
"multiple": 0,
"allow_null": 0,
"ui": 0,
"ajax": 0,
"placeholder": ""
},
{
"show_column_filter": false,
"allow_bulkedit": false,
"allow_quickedit": false,
"show_column": false,
"show_column_weight": 1000,
"show_column_sortable": false,
"key": "field_642afb9794fb8",
"label": "Zeitraum (leer lassen, wenn geschlossen)",
"name": "zeitraum",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "50",
"class": "",
"id": ""
},
"default_value": "",
"maxlength": "",
"placeholder": "",
"prepend": "",
"append": ""
}
],
"parent_repeater": "field_642af8a594fb5"
}
],
"parent_repeater": "field_642af702d7f50"
}
]
},
{
"show_column_filter": false,
"allow_bulkedit": 0,
"allow_quickedit": 0,
"show_column": 0,
"show_column_weight": 1000,
"show_column_sortable": false,
"key": "field_642be36ccbf63",
"label": "Fußnote",
"name": "footer",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"maxlength": "",
"placeholder": "",
"prepend": "",
"append": ""
}
],
"location": [
[
{
"param": "options_page",
"operator": "==",
"value": "restaurant"
}
]
],
"menu_order": 0,
"position": "normal",
"style": "default",
"label_placement": "top",
"instruction_placement": "label",
"hide_on_screen": "",
"active": true,
"description": "",
"show_in_rest": 0
}
]
<?php
/* exported from acf pro */
if ( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array(
'key' => 'group_642af70225c29',
'title' => 'Öffnungszeiten',
'fields' => array(
array(
'show_column_filter' => false,
'allow_bulkedit' => 0,
'allow_quickedit' => 0,
'show_column' => 0,
'show_column_weight' => 1000,
'show_column_sortable' => false,
'key' => 'field_642be38acbf64',
'label' => 'Überschrift',
'name' => 'header',
'aria-label' => '',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'maxlength' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
),
array(
'show_column_filter' => false,
'allow_bulkedit' => false,
'allow_quickedit' => false,
'show_column' => false,
'show_column_weight' => 1000,
'show_column_sortable' => false,
'key' => 'field_642af702d7f50',
'label' => 'Wochentag',
'name' => 'weekday',
'aria-label' => '',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'layout' => 'table',
'pagination' => 0,
'min' => 0,
'max' => 0,
'collapsed' => '',
'button_label' => 'Add Row',
'rows_per_page' => 20,
'sub_fields' => array(
array(
'show_column_filter' => 0,
'allow_bulkedit' => 0,
'allow_quickedit' => 0,
'show_column' => 0,
'show_column_weight' => 1000,
'show_column_sortable' => false,
'key' => 'field_642af74ee5e65',
'label' => 'Tag',
'name' => 'tag',
'aria-label' => '',
'type' => 'select',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '33',
'class' => '',
'id' => '',
),
'choices' => array(
'Mo' => 'Montag',
'Di' => 'Dienstag',
'Mi' => 'Mittwoch',
'Do' => 'Donnerstag',
'Fr' => 'Freitag',
'Sa' => 'Samstag',
'So' => 'Sonntag',
),
'default_value' => false,
'return_format' => 'array',
'multiple' => 0,
'allow_null' => 0,
'ui' => 0,
'ajax' => 0,
'placeholder' => '',
'parent_repeater' => 'field_642af702d7f50',
),
array(
'show_column_filter' => false,
'allow_bulkedit' => false,
'allow_quickedit' => false,
'show_column' => false,
'show_column_weight' => 1000,
'show_column_sortable' => false,
'key' => 'field_642af8a594fb5',
'label' => 'Öffnungszeiten',
'name' => 'oeffnungszeiten',
'aria-label' => '',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '66',
'class' => '',
'id' => '',
),
'layout' => 'table',
'min' => 0,
'max' => 0,
'collapsed' => '',
'button_label' => 'Add Row',
'rows_per_page' => 20,
'sub_fields' => array(
array(
'show_column_filter' => false,
'allow_bulkedit' => false,
'allow_quickedit' => false,
'show_column' => false,
'show_column_weight' => 1000,
'show_column_sortable' => false,
'key' => 'field_642af8f994fb6',
'label' => 'Öffnungszeit',
'name' => 'von_-_bis',
'aria-label' => '',
'type' => 'group',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'layout' => 'block',
'sub_fields' => array(
array(
'show_column_filter' => 0,
'allow_bulkedit' => 0,
'allow_quickedit' => 0,
'show_column' => 0,
'show_column_weight' => 1000,
'show_column_sortable' => false,
'key' => 'field_642af96294fb7',
'label' => 'Geöffnet',
'name' => 'geoffnet',
'aria-label' => '',
'type' => 'select',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '50',
'class' => '',
'id' => '',
),
'choices' => array(
'Geoeffnet' => 'Geöffnet',
'Geschlossen' => 'Geschlossen',
),
'default_value' => 'Geöffnet',
'return_format' => 'label',
'multiple' => 0,
'allow_null' => 0,
'ui' => 0,
'ajax' => 0,
'placeholder' => '',
),
array(
'show_column_filter' => false,
'allow_bulkedit' => false,
'allow_quickedit' => false,
'show_column' => false,
'show_column_weight' => 1000,
'show_column_sortable' => false,
'key' => 'field_642afb9794fb8',
'label' => 'Zeitraum (leer lassen, wenn geschlossen)',
'name' => 'zeitraum',
'aria-label' => '',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '50',
'class' => '',
'id' => '',
),
'default_value' => '',
'maxlength' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
),
),
'parent_repeater' => 'field_642af8a594fb5',
),
),
'parent_repeater' => 'field_642af702d7f50',
),
),
),
array(
'show_column_filter' => false,
'allow_bulkedit' => 0,
'allow_quickedit' => 0,
'show_column' => 0,
'show_column_weight' => 1000,
'show_column_sortable' => false,
'key' => 'field_642be36ccbf63',
'label' => 'Fußnote',
'name' => 'footer',
'aria-label' => '',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'maxlength' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
),
),
'location' => array(
array(
array(
'param' => 'options_page',
'operator' => '==',
'value' => 'restaurant',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
'show_in_rest' => 0,
));
endif;
<?php
/*
Add ACF options page
*/
if( function_exists('acf_add_options_page') ) {
acf_add_options_page(array(
'page_title' => 'Restaurant',
'menu_title' => 'Restaurant',
'menu_slug' => 'restaurant',
'capability' => 'edit_posts',
'redirect' => false
));
}
<?php
add_shortcode('opening_hours', 'opening_hours');
// page=restaurant
function opening_hours($atts, $content=''){
extract(shortcode_atts(array(
"field" => null,
"post_id" => null
), $atts));
//echo "<pre>".print_r($weekday,true)."</pre>";
if ( have_rows( 'weekday', 'option' ) ) {
echo '<div class="oeffnungszeiten">';
echo '<p class="text-center"><img class="alignnone wp-image-27431" src="https://soban-stuttgart.de/wp-content/uploads/2023/04/Soban_Logo_gleich-640x191-1-640x191.png" alt="" width="420" height="125" /></p>';
echo '<h3>'. get_field( 'header', 'option' ).'</h3>';
echo '<table class="opening_hours">';
echo '<tbody>';
$countrows = 0;
while ( have_rows( 'weekday', 'option' ) ) {
$countrows++;
echo '<tr id="row_'.$countrows.'">';
the_row();
$tag_selected_option = get_sub_field( 'tag' );
if ( $tag_selected_option ) {
echo '<th>';
echo '<span class="show-desktop">'. esc_html( $tag_selected_option['label'] ) .'</span>';
echo '<span class="show-mobile">' . esc_html( $tag_selected_option['value'] ) .'</span>';
echo '</th>';
}
if ( have_rows( 'oeffnungszeiten' ) ) {
$i = 0;
while ( have_rows( 'oeffnungszeiten' ) ) {
the_row();
if ( have_rows( 'von_-_bis' ) ) {
$i++;
echo "<!--" . $i . "-->";
while ( have_rows( 'von_-_bis' ) ) {
the_row();
$geoffnet_selected_option = get_sub_field( 'geoffnet' );
if ( $geoffnet_selected_option ) {
if ( get_sub_field( 'geoffnet' ) == 'Geöffnet') {
if( !empty(get_sub_field( 'zeitraum' )) ){
echo '<td class="von-bis">';
echo get_sub_field( 'zeitraum' );
echo ' <span class="show-desktop">Uhr</span>';
echo '</td>';
}
else {
//
}
}
else {
$i++;
echo '<td class="geschlossen">geschlossen</td>';
echo '<td class="geschlossen">geschlossen</td>';
}
}
else {
}
}
}
}
if($i == 1){
echo "<td>geschlossen</td>";
}
}
else {
// No rows found
}
echo "</tr>";
}
echo '<tbody>';
echo '</table>';
echo '<p>'. get_field( 'footer', 'option' ) . '</p>';
echo '</div>';
}
else {
// No rows found
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment