From 526a10b37e927714bc42e860ce6ea814da730e56 Mon Sep 17 00:00:00 2001 From: PieterT2000 Date: Fri, 5 Jun 2020 18:29:56 +0200 Subject: [PATCH] change default value frontend directory option --- functions.php | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/functions.php b/functions.php index 3814524..e261b35 100644 --- a/functions.php +++ b/functions.php @@ -28,7 +28,9 @@ function get_logo_src($object, $field_name, $request) function add_item($admin_bar) { global $pagenow; - $admin_bar->add_menu(array('id' => 'rebuild-frontend', 'title' => 'Rebuild Frontend', 'href' => '#')); + $admin_bar->add_menu(array('id' => 'rebuild-frontend', 'title' => 'Rebuild Frontend', 'href' => '#', 'meta' => [ + 'html' => "" . get_option('frontend-dir') . '' + ])); } add_action('admin_footer', 'clickHandlerRebuildButton'); @@ -52,7 +54,8 @@ function clickHandlerRebuildButton() let originalText = rebuildBtn.textContent; rebuildBtn.textContent = "Rebuilding..."; - let name = window.location.host.split(".")[0]; + // reads frontend directory name from hidden span next to it. + let name = rebuildBtn.nextElementSibling.textContent; fetch(`https://rebuilds.codebirds-apiserver.nl/${name}`, { method: "POST", }) @@ -136,3 +139,21 @@ function headless_mode_disable_front_end() exit; } } + +// Add custom settings to general settings +add_filter('admin_init', 'register_fields'); + +function register_fields() +{ + register_setting('general', 'frontend-dir', 'esc_attr'); + add_settings_field('frontend-dir', '', 'print_custom_field', 'general'); +} + +function print_custom_field() +{ + $host = explode(".", $_SERVER['HTTP_HOST']); + $defaultValue = $host[count($host) - 2]; // get the second-last item in the array to strip of also the TLD + + $value = get_option('frontend-dir', $defaultValue); + echo ''; +}