Poniżej masz kompletny kod PHP + HTML + JS–gotowy do wrzucenia np. do pliku functions.php Twojego motywu (lub w formie własnej wtyczki). Obsługuje on:
Pobieranie treści z ACF
Wykrywanie, czy użytkownik ma uprawnienia do edycji wpisów (admin/redaktor)
Pokazywanie modala zawsze dla adminów, a dla zwykłych użytkowników co 3 dni po ostatnim zamknięciu
Zapisywanie czasu zamknięcia w localStorage przy dowolnej metodzie zamknięcia modala
(Opcjonalnie) odliczanie czasu do ustawionego w ACF terminu
php
<?php
/**
* Shortcode: [modal]
* Wstawia modal Bootstrap 5, który:
* - Dla administratorów/redaktorów wyświetla się zawsze
* - Dla niezalogowanych/bez uprawnień wyświetla się ponownie
* dopiero po upływie 3 dni od ostatniego zamknięcia
*/
function modal_shortcode($atts) {
// 1) Pobieranie atrybutów shortcode i pól ACF
$atts = shortcode_atts(array(
'header' => 'Domyślny Nagłówek',
'text' => 'Domyślny tekst modala',
'background_image' => '',
'cta_text' => 'Dowiedz się więcej',
'cta_link' => '#'
), $atts, 'modal');
// ID posta z ustawieniami modala (zmień na swoje)
$post_id = 14187;
$header_field = get_field('modal_header', $post_id);
$text_field = get_field('modal_text', $post_id);
$bg_img = get_field('modal_background_image', $post_id);
$cta_text_f = get_field('cta_text', $post_id);
$cta_link_f = get_field('cta_link', $post_id);
$enable_count = get_field('enable_countdown', $post_id);
$countdown_ts = '';
// fallbacky
$header = $header_field ?: $atts['header'];
$text = wp_kses_post( $text_field ?: $atts['text'] );
$cta_text = $cta_text_f ?: $atts['cta_text'];
$cta_link = $cta_link_f ?: $atts['cta_link'];
// background image handling
if (is_array($bg_img)) {
$bg_url = $bg_img['url'];
} elseif (is_numeric($bg_img)) {
$bg_url = wp_get_attachment_image_url($bg_img, 'full');
} else {
$bg_url = $atts['background_image'];
}
// countdown timestamp
if ($enable_count && $enable_count !== '0') {
$end_date = get_field('modal_countdown_end', $post_id);
if ($end_date) {
// JS potrzebuje ms
$countdown_ts = strtotime($end_date) * 1000;
}
}
// 2) Czy użytkownik ma uprawnienia do edycji postów? (admin/redaktor)
$is_admin_user = current_user_can('edit_posts') ? 'true' : 'false';
// 3) Budowanie outputu
ob_start();
?>
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg modal-dialog-centered">
<div class="modal-content position-relative">
<?php if ($bg_url): ?>
<div class="modal-background"
style="background-image: url('<?php echo esc_url($bg_url); ?>');"></div>
<?php endif; ?>
<div class="modal-header">
<h2 id="exampleModalLabel" class="fw-bold card-title"><?php echo esc_html($header); ?></h2>
<button type="button" class="btn btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body" style="min-height:300px;">
<?php echo $text; ?>
</div>
<div class="modal-footer d-flex justify-content-between align-items-center">
<a href="<?php echo esc_url($cta_link); ?>"
class="btn btn-secondary text-decoration-none px-3">
<?php echo esc_html($cta_text); ?>
</a>
<?php if ($enable_count && $countdown_ts): ?>
<div class="countdown-wrapper text-end">
<div class="countdown-label"
style="font-size:1.2rem; font-weight:bold; color:#e35f21;">
Do początku zostało...
</div>
<div id="modal-countdown"
data-end-time="<?php echo esc_attr($countdown_ts); ?>"
style="font-size:1.2rem; font-weight:bold; color:#fff;">
Ładowanie...
</div>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<script>
document.addEventListener("DOMContentLoaded", function() {
var isAdminUser = <?php echo $is_admin_user; ?>; // true | false
var modalEl = document.getElementById("exampleModal");
if (!modalEl || typeof bootstrap === 'undefined') {
return; // brak modala lub Bootstrap JS
}
var modal = new bootstrap.Modal(modalEl);
// 1) Zapisujemy czas zamknięcia przy dowolnym zamknięciu modala
modalEl.addEventListener('hide.bs.modal', function () {
if (!isAdminUser) {
localStorage.setItem("modalLastClosed", Date.now().toString());
}
});
// 2) Funkcja decydująca, czy pokazać modal teraz
function shouldShowModal() {
if (isAdminUser) {
return true;
}
var last = localStorage.getItem("modalLastClosed");
if (!last) {
return true;
}
var diff = Date.now() - parseInt(last, 10);
var threeDays = 3 * 24 * 60 * 60 * 1000;
return diff >= threeDays;
}
// 3) Pokaż modal, jeśli warunki OK
if (shouldShowModal()) {
modal.show();
}
// 4) Countdown (jeśli włączony)
<?php if ($enable_count && $countdown_ts): ?>
(function() {
var elem = document.getElementById("modal-countdown");
var endTime = parseInt(elem.dataset.endTime, 10);
function updateCountdown() {
var now = Date.now(), diff = endTime - now;
if (diff <= 0) {
elem.closest('.countdown-wrapper').style.display = 'none';
return;
}
var days = Math.floor(diff / (1000*60*60*24)),
hours = Math.floor((diff % (1000*60*60*24)) / (1000*60*60)),
mins = Math.floor((diff % (1000*60*60)) / (1000*60)),
secs = Math.floor((diff % (1000*60)) / 1000);
elem.textContent = days + "d " + hours + "h " + mins + "m " + secs + "s";
}
updateCountdown();
setInterval(updateCountdown, 1000);
})();
<?php endif; ?>
});
</script>
<?php
return ob_get_clean();
}
add_shortcode('modal', 'modal_shortcode');
Instrukcja wdrożenia i testowania:
Wklej powyższy kod do pliku functions.php Twojego motywu lub stwórz własną wtyczkę.
Upewnij się, że Bootstrap 5 (CSS + JS) jest załadowany na froncie.
Na dowolnym wpisie/stronie wstaw shortcode:
[modal]
W trybie incognito (jako niezalogowany) sprawdź, że:
Modal pokazuje się za pierwszym razem.
Po zamknięciu – w localStorage zostaje wpisany modalLastClosed.
Przy odświeżeniu strony w ciągu 3 dni modal się nie pokaże.
Po przekroczeniu 3 dni od czasu zapisanego w localStorage – modal znowu się otworzy.
Jako zalogowany administrator/redaktor modal zawsze będzie się wyświetlał.
Gotowe! Jeśli masz dodatkowe pytania lub potrzebujesz funkcji X/Y – daj znać. 😉
User
Modal ma się ponownie uruchomić po 3 dniach od ostatniego zamknięcia a nie pojawiać się przez 3 dni od ostatniego zamknięcia. Dla zalogowanego użytkownika do poanelu administracyjnego ma się pojawiać przez cały czas, by ułatwić zarządzanie i edycję
Assistant
Poniżej pełny, gotowy do wrzucenia kod, który:
Dla niezalogowanych lub użytkowników bez uprawnień edycji:
• pierwsze wyświetlenie – zawsze,
• kolejne – dopiero po upływie 3 dni od ostatniego zamknięcia.
Dla zalogowanych administratorów/redaktorów (czyli użytkowników z prawem edit_posts):
• modal wyświetla się za każdym razem – ułatwiając zarządzanie treścią.
Upewnij się, że na stronie jest załadowany Bootstrap 5 (JS + CSS).
php
User
Usuń niepotrzebne elementy debbugowania
Assistant
Poniżej pełna, “czysta” wersja shortcode’u – bez zbędnych console.logów i elementów debugowania. Wklej do functions.php lub własnej wtyczki:
php
User
Odstęp 3 dni nie działa
Assistant
Poniżej „ostateczna” wersja – kompletnie pozbawiona zbędnych elementów, korzystająca z hidden.bs.modal (Bootstrap 5) do zapisu zamknięcia oraz z czystej logiki JS, która odmierza, czy od ostatniego zamknięcia minęło co najmniej 3 dni (259 200 000 ms). Klucz localStorage jest unikalny (wp_modal_last_closed), by nic nie kolidowało.
php
User
Odstęp 3 dni nie działa
Assistant
Spróbujmy uprościć i zamiast localStorage + ręcznego obliczania różnicy czasy użyć nam “gotowego” wygaśnięcia – czyli ciasteczka (cookie) z czasem życia 3 dni. Dzięki temu:
nie musimy robić Date.now() – lastClosed – przeglądarka sama usunie ciasteczko po 3 dniach,
kod jest prostszy i bardziej niezawodny.
Poniżej pełny shortcode, który:
Dla zalogowanych adminów/redaktorów – pokazuje modal ZA KAŻDYM razem
Dla pozostałych – najpierw pokazuje, a kiedy zostanie zamknięty, ustawia ciasteczko „modal_closed=1” na 3 dni; dopóki ono istnieje – modal się nie pojawi.
php
User
Zapisz cały kod
Assistant
Poniżej masz kompletny, “gotowy do wrzucenia” kod – PHP + HTML + JS. Upewnij się, że na froncie jest załadowany Bootstrap 5 (CSS i JS).
php
<?php
/**
* Shortcode: [modal]
*/
function modal_shortcode($atts) {
// 1) Parametry shortcode + ACF (post ID = 14187)
$atts = shortcode_atts( array(
'header' => 'Domyślny Nagłówek',
'text' => 'Domyślny tekst modala',
'background_image' => '',
'cta_text' => 'Dowiedz się więcej',
'cta_link' => '#'
), $atts, 'modal' );
$post_id = 14187; // zmień na własne ID
// Pobieramy z ACF lub bierzemy fallback z $atts
$header_field = get_field('modal_header', $post_id);
$text_field = get_field('modal_text', $post_id);
$bg_img = get_field('modal_background_image', $post_id);
$cta_text_f = get_field('cta_text', $post_id);
$cta_link_f = get_field('cta_link', $post_id);
$enable_count = get_field('enable_countdown', $post_id);
$countdown_ts = '';
$header = $header_field ?: $atts['header'];
$text = wp_kses_post( $text_field ?: $atts['text'] );
$cta_text = $cta_text_f ?: $atts['cta_text'];
$cta_link = $cta_link_f ?: $atts['cta_link'];
// Background image (array | ID | url)
if ( is_array($bg_img) ) {
$bg_url = $bg_img['url'];
} elseif ( is_numeric($bg_img) ) {
$bg_url = wp_get_attachment_image_url($bg_img, 'full');
} else {
$bg_url = $atts['background_image'];
}
// Countdown (jeśli włączony)
if ( $enable_count && $enable_count !== '0' ) {
$end_date = get_field('modal_countdown_end', $post_id);
if ( $end_date ) {
$countdown_ts = strtotime($end_date) * 1000; // ms
}
}
// 2) Czy użytkownik ma prawo edycji postów? (admin/redaktor)
// jeśli tak – modal zawsze się pokazuje
$is_admin_js = current_user_can('edit_posts') ? 'true' : 'false';
// 3) Render HTML + JS
ob_start();
?>
<div class="modal fade" id="exampleModal" tabindex="-1"
aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg modal-dialog-centered">
<div class="modal-content position-relative">
<?php if ( $bg_url ): ?>
<div class="modal-background"
style="background-image:url('<?php echo esc_url($bg_url); ?>')"></div>
<?php endif; ?>
<div class="modal-header">
<h2 id="exampleModalLabel" class="fw-bold card-title">
<?php echo esc_html($header); ?>
</h2>
<button type="button" class="btn btn-close" data-bs-dismiss="modal"
aria-label="Close"></button>
</div>
<div class="modal-body" style="min-height:300px;">
<?php echo $text; ?>
</div>
<div class="modal-footer d-flex justify-content-between align-items-center">
<a href="<?php echo esc_url($cta_link); ?>"
class="btn btn-secondary px-3">
<?php echo esc_html($cta_text); ?>
</a>
<?php if ( $enable_count && $countdown_ts ): ?>
<div class="countdown-wrapper text-end">
<div class="countdown-label"
style="font-size:1.2rem; font-weight:bold; color:#e35f21;">
Do początku zostało...
</div>
<div id="modal-countdown"
data-end-time="<?php echo esc_attr($countdown_ts); ?>"
style="font-size:1.2rem; font-weight:bold; color:#fff;">
Ładowanie...
</div>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<script>
document.addEventListener("DOMContentLoaded", function(){
var isAdminUser = <?php echo $is_admin_js; ?>; // true | false
var modalEl = document.getElementById("exampleModal");
if (!modalEl || typeof bootstrap === 'undefined') {
return; // brak modala lub brak Bootstrapa
}
var modal = new bootstrap.Modal(modalEl);
// 1) Zapis czasu zamknięcia modala (dowolna metoda: X, ESC, kliknięcie tła)
modalEl.addEventListener('hide.bs.modal', function(){
if (!isAdminUser) {
localStorage.setItem("modalLastClosed", Date.now().toString());
}
});
// 2) Funkcja decyduje, czy POKAZAĆ modal teraz
function shouldShowModal(){
if (isAdminUser) {
// admin zawsze widzi modal
return true;
}
var last = localStorage.getItem("modalLastClosed");
if (!last) {
// nigdy nie zamknął → pokaz
return true;
}
var diff = Date.now() - parseInt(last,10);
var threeD = 3 * 24 * 60 * 60 * 1000; // 259200000 ms
// pokaz tylko, jeżeli OD ostatniego zamknięcia upłynęły co najmniej 3 dni:
return diff >= threeD;
}
// 3) Wyświetl, jeżeli funkcja zwróci true
if (shouldShowModal()) {
modal.show();
}
// 4) Countdown (jeżeli włączony)
<?php if ($enable_count && $countdown_ts): ?>
(function(){
var el = document.getElementById("modal-countdown");
var endTime = parseInt(el.dataset.endTime,10);
function update(){
var now = Date.now(), diff = endTime - now;
if (diff <= 0) {
el.closest('.countdown-wrapper').style.display = 'none';
return;
}
var days = Math.floor(diff/(1000*60*60*24)),
hours = Math.floor((diff%(1000*60*60*24))/(1000*60*60)),
mins = Math.floor((diff%(1000*60*60))/(1000*60)),
secs = Math.floor((diff%(1000*60))/1000);
el.textContent = days+"d "+hours+"h "+mins+"m "+secs+"s";
}
update();
setInterval(update,1000);
})();
<?php endif; ?>
});
</script>
<?php
return ob_get_clean();
}
add_shortcode('modal','modal_shortcode');
Jak to działa?
Po pierwszym zamknięciu (hide.bs.modal) dla zwykłego użytkownika w localStorage.modalLastClosed zapisujemy bieżący timestamp.
Przy każdym wejściu sprawdzamy:
– Jeśli jesteś adminem/redaktorem (isAdminUser===true) → zawsze pokazujemy modal.
– W przeciwnym razie:
• Jeśli klucz modalLastClosed nie istnieje → pokaz.
• Jeśli istnieje i od tej daty minęło co najmniej 3 dni → pokaz.
• W pozostałych przypadkach → nie pokazujemy.
Teraz modal pojawi się ponownie dopiero po upływie 3 dni od ostatniego zamknięcia, a administratorzy/redaktorzy zawsze go widzą.
<?php
/**
* Shortcode: [modal]
* – Dla adminów/redaktorów pokazuje się zawsze
* – Dla pozostałych: ponownie dopiero po 3 dniach od ostatniego zamknięcia
*/
function modal_shortcode($atts) {
// 1) Atrybuty shortcode + ACF
$atts = shortcode_atts([
'header' => 'Domyślny Nagłówek',
'text' => 'Domyślny tekst modala',
'background_image' => '',
'cta_text' => 'Dowiedz się więcej',
'cta_link' => '#'
], $atts, 'modal');
$post_id = 14187; // zmień na swoje ID
$header_field = get_field('modal_header', $post_id);
$text_field = get_field('modal_text', $post_id);
$bg_img = get_field('modal_background_image', $post_id);
$cta_text_f = get_field('cta_text', $post_id);
$cta_link_f = get_field('cta_link', $post_id);
$enable_count = get_field('enable_countdown', $post_id);
$countdown_ts = '';
// fallbacks
$header = $header_field ?: $atts['header'];
$text = wp_kses_post($text_field ?: $atts['text']);
$cta_text = $cta_text_f ?: $atts['cta_text'];
$cta_link = $cta_link_f ?: $atts['cta_link'];
// background image
if (is_array($bg_img)) {
$bg_url = $bg_img['url'];
} elseif (is_numeric($bg_img)) {
$bg_url = wp_get_attachment_image_url($bg_img, 'full');
} else {
$bg_url = $atts['background_image'];
}
// countdown timestamp
if ($enable_count && $enable_count !== '0') {
$end_date = get_field('modal_countdown_end', $post_id);
if ($end_date) {
$countdown_ts = strtotime($end_date) * 1000;
}
}
// 2) Uprawnienia użytkownika
$is_admin_js = current_user_can('edit_posts') ? 'true' : 'false';
// 3) Render
ob_start();
?>
<div class="modal fade" id="exampleModal" tabindex="-1"
aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg modal-dialog-centered">
<div class="modal-content position-relative">
<?php if ($bg_url): ?>
<div class="modal-background"
style="background-image:url('<?php echo esc_url($bg_url); ?>')"></div>
<?php endif; ?>
<div class="modal-header">
<h2 id="exampleModalLabel" class="fw-bold card-title">
<?php echo esc_html($header); ?>
</h2>
<button type="button" class="btn btn-close"
data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body" style="min-height:300px;">
<?php echo $text; ?>
</div>
<div class="modal-footer d-flex justify-content-between align-items-center">
<a href="<?php echo esc_url($cta_link); ?>"
class="btn btn-secondary px-3">
<?php echo esc_html($cta_text); ?>
</a>
<?php if ($enable_count && $countdown_ts): ?>
<div class="countdown-wrapper text-end">
<div class="countdown-label"
style="font-size:1.2rem; font-weight:bold; color:#e35f21;">
Do początku zostało...
</div>
<div id="modal-countdown"
data-end-time="<?php echo esc_attr($countdown_ts); ?>"
style="font-size:1.2rem; font-weight:bold; color:#fff;">
Ładowanie...
</div>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<script>
document.addEventListener("DOMContentLoaded", function(){
var isAdminUser = <?php echo $is_admin_js; ?>;
var modalEl = document.getElementById("exampleModal");
if (!modalEl || typeof bootstrap === 'undefined') return;
var modal = new bootstrap.Modal(modalEl);
// Zapisujemy czas zamknięcia
modalEl.addEventListener('hide.bs.modal', function(){
if (!isAdminUser) {
localStorage.setItem("modalLastClosed", Date.now().toString());
}
});
// Czy pokazać modal?
function shouldShowModal(){
if (isAdminUser) return true;
var last = localStorage.getItem("modalLastClosed");
if (!last) return true;
var diff = Date.now() - parseInt(last,10);
var threeD = 3 * 24 * 60 * 60 * 1000;
return diff >= threeD;
}
if (shouldShowModal()) {
modal.show();
}
// Countdown
<?php if ($enable_count && $countdown_ts): ?>
(function(){
var el = document.getElementById("modal-countdown");
var endTime = parseInt(el.dataset.endTime,10);
function update(){
var now = Date.now(), diff = endTime - now;
if (diff <= 0) {
el.closest('.countdown-wrapper').style.display = 'none';
return;
}
var days = Math.floor(diff/(1000*60*60*24)),
hours = Math.floor((diff%(1000*60*60*24))/(1000*60*60)),
mins = Math.floor((diff%(1000*60*60))/(1000*60)),
secs = Math.floor((diff%(1000*60))/1000);
el.textContent = days+"d "+hours+"h "+mins+"m "+secs+"s";
}
update();
setInterval(update,1000);
})();
<?php endif; ?>
});
</script>
<?php
return ob_get_clean();
}
add_shortcode('modal','modal_shortcode');
Instrukcja działania:
Po zamknięciu modala (hide.bs.modal) dla niezalogowanych zapisujemy timestamp w localStorage.modalLastClosed.
Przy każdym wejściu:
jeżeli jesteś adminem/redaktorem → zawsze wyświetlamy modal,
w przeciwnym razie:
• jeśli nie ma wpisu w localStorage → pokazujemy modal,
• jeśli jest i od tej daty minęły ≥ 3 dni → pokazujemy modal,
• w pozostałych przypadkach ukrywamy modal.
<?php
/**
* Shortcode: [modal]
* – Dla adminów/redaktorów (edit_posts) zawsze pokazuje modal
* – Dla pozostałych: pokazuje ponownie dopiero po upływie 3 dni
*/
function modal_shortcode($atts) {
// 1) Atrybuty + ACF
$atts = shortcode_atts([
'header' => 'Domyślny Nagłówek',
'text' => 'Domyślny tekst modala',
'background_image' => '',
'cta_text' => 'Dowiedz się więcej',
'cta_link' => '#'
], $atts, 'modal');
$post_id = 14187; // <- ustaw swoje ID
$header_field = get_field('modal_header', $post_id);
$text_field = get_field('modal_text', $post_id);
$bg_img = get_field('modal_background_image',$post_id);
$cta_text_f = get_field('cta_text', $post_id);
$cta_link_f = get_field('cta_link', $post_id);
$enable_count = get_field('enable_countdown', $post_id);
$countdown_ts = '';
// fallbacky
$header = $header_field ?: $atts['header'];
$text = wp_kses_post( $text_field ?: $atts['text'] );
$cta_text = $cta_text_f ?: $atts['cta_text'];
$cta_link = $cta_link_f ?: $atts['cta_link'];
// background image
if (is_array($bg_img)) { $bg_url = $bg_img['url']; }
elseif (is_numeric($bg_img)) { $bg_url = wp_get_attachment_image_url($bg_img,'full'); }
else { $bg_url = $atts['background_image']; }
// countdown timestamp
if ($enable_count && $enable_count !== '0') {
$end = get_field('modal_countdown_end', $post_id);
if ($end) $countdown_ts = strtotime($end) * 1000;
}
// 2) Czy to admin/redaktor?
$is_admin_js = current_user_can('edit_posts') ? 'true' : 'false';
// 3) Render
ob_start();
?>
<div class="modal fade" id="exampleModal" tabindex="-1"
aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg modal-dialog-centered">
<div class="modal-content position-relative">
<?php if ($bg_url): ?>
<div class="modal-background"
style="background-image:url('<?php echo esc_url($bg_url); ?>')">
</div>
<?php endif; ?>
<div class="modal-header">
<h2 id="exampleModalLabel" class="fw-bold card-title">
<?php echo esc_html($header); ?>
</h2>
<button type="button" class="btn btn-close"
data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body" style="min-height:300px;">
<?php echo $text; ?>
</div>
<div class="modal-footer d-flex justify-content-between align-items-center">
<a href="<?php echo esc_url($cta_link); ?>"
class="btn btn-secondary px-3">
<?php echo esc_html($cta_text); ?>
</a>
<?php if ($enable_count && $countdown_ts): ?>
<div class="countdown-wrapper text-end">
<div class="countdown-label"
style="font-size:1.2rem; font-weight:bold; color:#e35f21;">
Do początku zostało...
</div>
<div id="modal-countdown"
data-end-time="<?php echo esc_attr($countdown_ts); ?>"
style="font-size:1.2rem; font-weight:bold; color:#fff;">
Ładowanie...
</div>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<script>
document.addEventListener("DOMContentLoaded", function(){
var isAdminUser = <?php echo $is_admin_js; ?>; // boolean
var modalEl = document.getElementById("exampleModal");
if (!modalEl || typeof bootstrap === 'undefined') return;
var modal = new bootstrap.Modal(modalEl);
// 1) Zapisujemy timestamp po pełnym ukryciu modala
modalEl.addEventListener('hidden.bs.modal', function(){
if (!isAdminUser) {
localStorage.setItem('wp_modal_last_closed', Date.now().toString());
}
});
// 2) Sprawdzamy, czy od ostatniego zamknięcia minęło ≥ 3 dni
function shouldShowModal(){
if (isAdminUser) {
return true;
}
var last = localStorage.getItem('wp_modal_last_closed');
if (!last) {
return true;
}
var diff = Date.now() - parseInt(last, 10);
var threeD = 3 * 24 * 60 * 60 * 1000;
return diff >= threeD;
}
// 3) Pokaż modal jeśli warunki spełnione
if (shouldShowModal()) {
modal.show();
}
// 4) Countdown (jeśli włączony)
<?php if ($enable_count && $countdown_ts): ?>
(function(){
var el = document.getElementById('modal-countdown'),
endTime = parseInt(el.dataset.endTime, 10);
function update(){
var now = Date.now(),
diff = endTime - now;
if (diff <= 0) {
el.closest('.countdown-wrapper').style.display = 'none';
return;
}
var days = Math.floor(diff / (1000*60*60*24)),
hours = Math.floor((diff % (1000*60*60*24)) / (1000*60*60)),
mins = Math.floor((diff % (1000*60*60)) / (1000*60)),
secs = Math.floor((diff % (1000*60)) / 1000);
el.textContent = days + 'd ' + hours + 'h ' + mins + 'm ' + secs + 's';
}
update();
setInterval(update, 1000);
})();
<?php endif; ?>
});
</script>
<?php
return ob_get_clean();
}
add_shortcode('modal','modal_shortcode');
Uwagi:
Słuchamy hidden.bs.modal (nie hide), czyli po zamknięciu.
Klucz w localStorage to wp_modal_last_closed – możesz go zmienić, by nie kolidować z innymi skryptami.
shouldShowModal() zwraca true dopiero, gdy od ostatniego zapisu upłynęło ≥ 3 dni.
Dla użytkownika z uprawnieniem edit_posts (admin/redaktor) zawsze zwraca true.
Po wgraniu tego kodu w functions.php i wyczyszczeniu cache przeglądarki:
Niezalogowany: modal pojawi się przy pierwszym wejściu i dopiero po 3 dniach od ostatniego zamknięcia.
Zalogowany (admin/redaktor): modal zawsze się wyświetla.
<?php
/**
* Shortcode: [modal]
* – Admin/redaktor: modal zawsze
* – Inni: po pierwszym zamknięciu ciasteczko na 3 dni → potem znika i modal wraca
*/
function modal_shortcode($atts) {
// 1) Pobranie atrybutów i ACF
$atts = shortcode_atts([
'header' => 'Domyślny Nagłówek',
'text' => 'Domyślny tekst modala',
'background_image' => '',
'cta_text' => 'Dowiedz się więcej',
'cta_link' => '#'
], $atts, 'modal');
$post_id = 14187;
$header_field = get_field('modal_header', $post_id);
$text_field = get_field('modal_text', $post_id);
$bg_img = get_field('modal_background_image', $post_id);
$cta_text_f = get_field('cta_text', $post_id);
$cta_link_f = get_field('cta_link', $post_id);
$enable_count = get_field('enable_countdown', $post_id);
$countdown_ts = '';
// fallbacki
$header = $header_field ?: $atts['header'];
$text = wp_kses_post( $text_field ?: $atts['text'] );
$cta_text = $cta_text_f ?: $atts['cta_text'];
$cta_link = $cta_link_f ?: $atts['cta_link'];
// tło
if (is_array($bg_img)) {
$bg_url = $bg_img['url'];
} elseif (is_numeric($bg_img)) {
$bg_url = wp_get_attachment_image_url($bg_img, 'full');
} else {
$bg_url = $atts['background_image'];
}
// countdown
if ($enable_count && $enable_count !== '0') {
$end_date = get_field('modal_countdown_end', $post_id);
if ($end_date) {
$countdown_ts = strtotime($end_date) * 1000;
}
}
// 2) Czy admin/redaktor?
// Jeżeli tak, modal zawsze.
$is_admin = current_user_can('edit_posts') ? 'true' : 'false';
// 3) Generujemy HTML + JS
ob_start();
?>
<div class="modal fade" id="exampleModal" tabindex="-1"
aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg modal-dialog-centered">
<div class="modal-content position-relative">
<?php if ($bg_url): ?>
<div class="modal-background"
style="background-image:url('<?php echo esc_url($bg_url); ?>')"></div>
<?php endif; ?>
<div class="modal-header">
<h2 id="exampleModalLabel" class="fw-bold card-title">
<?php echo esc_html($header); ?>
</h2>
<button type="button" class="btn btn-close"
data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body" style="min-height:300px;">
<?php echo $text; ?>
</div>
<div class="modal-footer d-flex justify-content-between align-items-center">
<a href="<?php echo esc_url($cta_link); ?>"
class="btn btn-secondary px-3">
<?php echo esc_html($cta_text); ?>
</a>
<?php if ($enable_count && $countdown_ts): ?>
<div class="countdown-wrapper text-end">
<div class="countdown-label"
style="font-size:1.2rem; font-weight:bold; color:#e35f21;">
Do początku zostało...
</div>
<div id="modal-countdown"
data-end-time="<?php echo esc_attr($countdown_ts); ?>"
style="font-size:1.2rem; font-weight:bold; color:#fff;">
Ładowanie...
</div>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<script>
(function(){
var isAdmin = <?php echo $is_admin; ?>; // true|false
if (typeof bootstrap === 'undefined') return;
var modalEl = document.getElementById("exampleModal");
if (!modalEl) return;
var modal = new bootstrap.Modal(modalEl);
// helpery do ciasteczek
function setCookie(name, value, days) {
var d = new Date();
d.setTime(d.getTime() + (days*24*60*60*1000));
document.cookie = name + "=" + value + ";expires=" + d.toUTCString() + ";path=/";
}
function getCookie(name) {
var match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
return match ? match[2] : null;
}
// 1) Po zamknięciu modala (klik, ESC, tło) – ustaw cookie na 3 dni
modalEl.addEventListener('hide.bs.modal', function(){
if (!isAdmin) {
setCookie("modal_closed", "1", 3);
}
});
// 2) Decyzja: czy pokazać?
function shouldShow() {
if (isAdmin) {
return true;
}
// jeżeli cookie istnieje → ukryj
return getCookie("modal_closed") === null;
}
// 3) Pokaż, jeżeli OK
if (shouldShow()) {
modal.show();
}
// 4) Countdown (jeśli włączony)
<?php if ($enable_count && $countdown_ts): ?>
(function(){
var el = document.getElementById("modal-countdown");
var endTime = parseInt(el.dataset.endTime,10);
function update(){
var diff = endTime - Date.now();
if (diff <= 0) {
el.closest('.countdown-wrapper').style.display = 'none';
return;
}
var d = Math.floor(diff/86400000),
h = Math.floor((diff%86400000)/3600000),
m = Math.floor((diff%3600000)/60000),
s = Math.floor((diff%60000)/1000);
el.textContent = d+"d "+h+"h "+m+"m "+s+"s";
}
update();
setInterval(update,1000);
})();
<?php endif; ?>
})();
</script>
<?php
return ob_get_clean();
}
add_shortcode('modal','modal_shortcode');
Jak to działa?
– Po dowolnym zamknięciu (hide.bs.modal) dla zwykłego użytkownika zapisujemy ciasteczko modal_closed=1; max-age=3 dni.
– Przy każdym wejściu:
• jeśli jesteś adminem (isAdmin===true) → zawsze pokazujemy modal,
• w przeciwnym razie sprawdzamy, czy ciasteczko już wygasło (usunięte przez przeglądarkę) → jeśli TAK, modal znowu się pojawi.
Usuń wszystkie dotychczasowe wpisy w localStorage i cookies, odśwież stronę i przetestuj. Po zamknięciu modala zwykły użytkownik nie zobaczy go ponownie aż do momentu, gdy ciasteczko wygaśnie (3 dni). Administracja widzi go zawsze.
<?php
/**
* Shortcode: [modal]
* – Dla adminów/redaktorów modal zawsze
* – Dla pozostałych: po pierwszym zamknięciu ciasteczko na 3 dni,
* potem modal wraca dopiero gdy ciasteczko wygaśnie
*/
function modal_shortcode($atts) {
// 1) Parametry shortcode + pobranie pól ACF
$atts = shortcode_atts([
'header' => 'Domyślny Nagłówek',
'text' => 'Domyślny tekst modala',
'background_image' => '',
'cta_text' => 'Dowiedz się więcej',
'cta_link' => '#'
], $atts, 'modal');
$post_id = 14187; // tu ustaw swoje ID
$header_field = get_field('modal_header', $post_id);
$text_field = get_field('modal_text', $post_id);
$bg_img = get_field('modal_background_image', $post_id);
$cta_text_f = get_field('cta_text', $post_id);
$cta_link_f = get_field('cta_link', $post_id);
$enable_count = get_field('enable_countdown', $post_id);
$countdown_ts = '';
// fallbacki z shortcode
$header = $header_field ?: $atts['header'];
$text = wp_kses_post( $text_field ?: $atts['text'] );
$cta_text = $cta_text_f ?: $atts['cta_text'];
$cta_link = $cta_link_f ?: $atts['cta_link'];
// tło modala
if (is_array($bg_img)) {
$bg_url = $bg_img['url'];
} elseif (is_numeric($bg_img)) {
$bg_url = wp_get_attachment_image_url($bg_img, 'full');
} else {
$bg_url = $atts['background_image'];
}
// countdown timestamp (ms)
if ($enable_count && $enable_count !== '0') {
$end_date = get_field('modal_countdown_end', $post_id);
if ($end_date) {
$countdown_ts = strtotime($end_date) * 1000;
}
}
// 2) Czy użytkownik ma prawo edycji postów? (admin/redaktor)
$is_admin = current_user_can('edit_posts') ? 'true' : 'false';
// 3) Generowanie HTML + JS
ob_start();
?>
<div class="modal fade" id="exampleModal" tabindex="-1"
aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg modal-dialog-centered">
<div class="modal-content position-relative">
<?php if ($bg_url): ?>
<div class="modal-background"
style="background-image:url('<?php echo esc_url($bg_url); ?>')"></div>
<?php endif; ?>
<div class="modal-header">
<h2 id="exampleModalLabel" class="fw-bold card-title">
<?php echo esc_html($header); ?>
</h2>
<button type="button" class="btn btn-close"
data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body" style="min-height:300px;">
<?php echo $text; ?>
</div>
<div class="modal-footer d-flex justify-content-between align-items-center">
<a href="<?php echo esc_url($cta_link); ?>"
class="btn btn-secondary px-3">
<?php echo esc_html($cta_text); ?>
</a>
<?php if ($enable_count && $countdown_ts): ?>
<div class="countdown-wrapper text-end">
<div class="countdown-label"
style="font-size:1.2rem; font-weight:bold; color:#e35f21;">
Do początku zostało...
</div>
<div id="modal-countdown"
data-end-time="<?php echo esc_attr($countdown_ts); ?>"
style="font-size:1.2rem; font-weight:bold; color:#fff;">
Ładowanie...
</div>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<script>
(function(){
var isAdmin = <?php echo $is_admin; ?>; // true|false
if (typeof bootstrap === 'undefined') return;
var modalEl = document.getElementById("exampleModal");
if (!modalEl) return;
var modal = new bootstrap.Modal(modalEl);
// funkcje do ciasteczek
function setCookie(name, value, days) {
var d = new Date();
d.setTime(d.getTime() + days*24*60*60*1000);
document.cookie = name + "=" + value + ";expires=" + d.toUTCString() + ";path=/";
}
function getCookie(name) {
var re = new RegExp('(^| )' + name + '=([^;]+)');
var m = document.cookie.match(re);
return m ? m[2] : null;
}
// 1) Po zamknięciu modala – ustaw ciasteczko na 3 dni
modalEl.addEventListener('hide.bs.modal', function(){
if (!isAdmin) {
setCookie("modal_closed", "1", 3);
}
});
// 2) Decyzja, czy pokazać modal
function shouldShow() {
if (isAdmin) return true;
return getCookie("modal_closed") === null;
}
// 3) Pokaż, jeśli warunki OK
if (shouldShow()) {
modal.show();
}
// 4) Countdown (jeśli włączony)
<?php if ($enable_count && $countdown_ts): ?>
(function(){
var el = document.getElementById("modal-countdown");
var endTime = parseInt(el.dataset.endTime,10);
function update(){
var diff = endTime - Date.now();
if (diff <= 0) {
el.closest('.countdown-wrapper').style.display = 'none';
return;
}
var d = Math.floor(diff/86400000),
h = Math.floor((diff%86400000)/3600000),
m = Math.floor((diff%3600000)/60000),
s = Math.floor((diff%60000)/1000);
el.textContent = d+"d "+h+"h "+m+"m "+s+"s";
}
update();
setInterval(update,1000);
})();
<?php endif; ?>
})();
</script>
<?php
return ob_get_clean();
}
add_shortcode('modal','modal_shortcode');
Instrukcja instalacji:
Wklej powyższy kod do pliku functions.php motywu lub własnej wtyczki.
Upewnij się, że na stronie ładowany jest Bootstrap 5 (JS+CSS).
W treści wpisu/strony wstaw shortcode: [modal]
Gotowe! Modal dla niezalogowanych zniknie na 3 dni po zamknięciu, dla adminów/redaktorów pojawia się zawsze.