Skip to content

Commit

Permalink
change default value frontend directory option
Browse files Browse the repository at this point in the history
  • Loading branch information
PieterT2000 committed Jun 5, 2020
1 parent 29b8f07 commit 526a10b
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => "<span style='opacity: 0'>" . get_option('frontend-dir') . '</span>'
]));
}

add_action('admin_footer', 'clickHandlerRebuildButton');
Expand All @@ -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",
})
Expand Down Expand Up @@ -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', '<label for="frontend-dir">' . __('Frontend Directory', 'frontend-dir') . '</label>', '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 '<input type="text" id="frontend-dir" name="frontend-dir" value="' . $value . '" />';
}

0 comments on commit 526a10b

Please sign in to comment.