Skip to content

Instantly share code, notes, and snippets.

@nathaningram
Last active December 18, 2024 16:25
Show Gist options
  • Save nathaningram/8b4f44e270fdcf51c7df48d17ba36166 to your computer and use it in GitHub Desktop.
Save nathaningram/8b4f44e270fdcf51c7df48d17ba36166 to your computer and use it in GitHub Desktop.
Starter Site 2024 Course - Brilliant Dashboard
<?php
/*
Plugin Name: Brilliant Dashboard
Description: Custom Dashboard Replacement with iFrame
Version: 2024.12
Author: Brilliant Web Works
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
// Add settings page under 'Settings'
function bww_custom_dashboard_menu() {
add_options_page(
'Brilliant Dashboard Settings',
'Brilliant Dashboard',
'manage_options',
'brilliant-dashboard-settings',
'bww_custom_dashboard_settings_page'
);
}
add_action('admin_menu', 'bww_custom_dashboard_menu');
// Add inline CSS for toggle styling
function bww_custom_dashboard_toggle_styles() {
echo '<style>
.switch {
position: relative;
display: inline-block;
width: 34px;
height: 20px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
transition: .4s;
border-radius: 20px;
}
.slider:before {
position: absolute;
content: "";
height: 14px;
width: 14px;
left: 3px;
bottom: 3px;
background-color: white;
transition: .4s;
border-radius: 50%;
}
input:checked + .slider {
background-color: #4caf50;
}
input:checked + .slider:before {
transform: translateX(14px);
}
</style>';
}
add_action('admin_head', 'bww_custom_dashboard_toggle_styles');
// Create Settings Page Content
function bww_custom_dashboard_settings_page() {
$selected_element = get_option('custom_dashboard_kadence_element', '');
$hide_widgets = get_option('custom_dashboard_hide_widgets', 'off');
$iframe_height = get_option('custom_dashboard_iframe_height', '600'); // Default: 600px
$enable_dashboard = get_option('custom_dashboard_enable', 'on'); // Default: Enabled
if ($_SERVER['REQUEST_METHOD'] == 'POST' && check_admin_referer('update_custom_dashboard_settings')) {
// Update Enable/Disable Dashboard
$enable_dashboard = isset($_POST['custom_dashboard_enable']) && $_POST['custom_dashboard_enable'] === 'on' ? 'on' : 'off';
update_option('custom_dashboard_enable', $enable_dashboard);
// Update Kadence Element selection
if (isset($_POST['custom_dashboard_kadence_element'])) {
$selected_element = sanitize_text_field($_POST['custom_dashboard_kadence_element']);
update_option('custom_dashboard_kadence_element', $selected_element);
}
// Update Hide Dashboard Widgets setting
$hide_widgets = isset($_POST['custom_dashboard_hide_widgets']) && $_POST['custom_dashboard_hide_widgets'] === 'on' ? 'on' : 'off';
update_option('custom_dashboard_hide_widgets', $hide_widgets);
// Update iframe height
if (isset($_POST['custom_dashboard_iframe_height'])) {
$iframe_height = absint($_POST['custom_dashboard_iframe_height']); // Ensure positive integer
update_option('custom_dashboard_iframe_height', $iframe_height);
}
}
// Fetch kadence_element posts
$args = array(
'post_type' => 'kadence_element',
'posts_per_page' => -1
);
$elements = get_posts($args);
echo '<div class="wrap">';
echo '<h1>Brilliant Dashboard Settings</h1>';
echo '<form method="post" action="">';
wp_nonce_field('update_custom_dashboard_settings');
// Enable/Disable Dashboard Toggle
echo '<table class="form-table">';
echo '<tr valign="top">';
echo '<th scope="row">Enable Dashboard iFrame</th>';
echo '<td>';
echo '<label class="switch">';
echo '<input type="checkbox" name="custom_dashboard_enable" value="on"' . checked($enable_dashboard, 'on', false) . '>';
echo '<span class="slider"></span>';
echo '</label>';
echo '</td>';
echo '</tr>';
// Hide Dashboard Widgets Toggle
echo '<tr valign="top">';
echo '<th scope="row">Hide Dashboard Widgets</th>';
echo '<td>';
echo '<label class="switch">';
echo '<input type="checkbox" name="custom_dashboard_hide_widgets" value="on"' . checked($hide_widgets, 'on', false) . '>';
echo '<span class="slider"></span>';
echo '</label>';
echo '</td>';
echo '</tr>';
// Kadence Element Selection
echo '<tr valign="top">';
echo '<th scope="row">Select a Kadence Element</th>';
echo '<td>';
echo '<select name="custom_dashboard_kadence_element" class="regular-text">';
echo '<option value="">-- Select --</option>';
foreach ($elements as $element) {
$selected = selected($selected_element, $element->ID, false);
echo '<option value="' . esc_attr($element->ID) . '"' . $selected . '>' . esc_html($element->post_title) . '</option>';
}
echo '</select>';
// Edit Element Button
if (!empty($selected_element)) {
$edit_link = admin_url('post.php?post=' . esc_attr($selected_element) . '&action=edit');
echo ' <a href="' . esc_url($edit_link) . '" class="button-secondary" target="_blank">Edit Element</a>';
}
echo '</td>';
echo '</tr>';
// Iframe Height Field
echo '<tr valign="top">';
echo '<th scope="row">Dashboard iFrame Height</th>';
echo '<td>';
echo '<input type="number" name="custom_dashboard_iframe_height" value="' . esc_attr($iframe_height) . '" class="small-text" min="100">';
echo '</td>';
echo '</tr>';
echo '</table>';
// Save Changes Button
echo '<p class="submit">';
echo '<input type="submit" name="submit" id="submit" class="button button-primary" value="Save Changes">';
echo '</p>';
echo '</form>';
echo '</div>';
}
// Inject the iframe into the dashboard
function bww_dashboard_iframe() {
global $pagenow;
// Check if we're on the main dashboard page and iframe is enabled
$enable_dashboard = get_option('custom_dashboard_enable', 'on');
if ($pagenow == 'index.php' && empty($_GET) && $enable_dashboard === 'on') {
$selected_element_id = get_option('custom_dashboard_kadence_element', '');
$iframe_height = get_option('custom_dashboard_iframe_height', '600'); // Default: 600px
if (!empty($selected_element_id)) {
$selected_post = get_post($selected_element_id);
if ($selected_post) {
$element_url = home_url() . '/?kadence_element=' . $selected_post->post_name;
echo '<div id="brilliant-dashboard-container" style="margin: 20px 20px 0 0;">';
echo '<iframe id="brilliant-dashboard" src="' . esc_url($element_url) . '" style="width:100%; height:' . esc_attr($iframe_height) . 'px; border:none;"></iframe>';
echo '</div>';
}
}
}
}
add_action('admin_notices', 'bww_dashboard_iframe');
// Optionally hide all dashboard widgets and other elements
function bww_hide_all_dashboard_widgets() {
$hide_widgets = get_option('custom_dashboard_hide_widgets', 'off');
if ($hide_widgets === 'on') {
global $wp_meta_boxes;
$wp_meta_boxes['dashboard']['normal']['core'] = array();
$wp_meta_boxes['dashboard']['side']['core'] = array();
$wp_meta_boxes['dashboard']['advanced']['core'] = array();
// Inline CSS to hide elements including Screen Options and Help toggles
echo '<style>
#wpbody-content > .wrap > h1,
#dashboard-widgets-wrap,
#screen-meta, #screen-meta-links {
display: none;
}
</style>';
}
}
add_action('wp_dashboard_setup', 'bww_hide_all_dashboard_widgets', 100);
// Force-hide the WordPress admin bar in the iframe for Kadence Element content
function bww_disable_admin_bar_for_iframe() {
$selected_element_id = get_option('custom_dashboard_kadence_element', '');
if (!empty($selected_element_id) && isset($_GET['kadence_element']) && $_GET['kadence_element'] === get_post_field('post_name', $selected_element_id)) {
add_filter('show_admin_bar', '__return_false');
}
}
add_action('init', 'bww_disable_admin_bar_for_iframe');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment