...
Create a custom WordPress Dashboard for your clients. Video link available in Tip Content.

Add the following snippets to your website. Run PHP in ‘Admin Only” and JS in “Admin Area Header”.
———————————————-
PHP Snippet:

<?php
// Centralize config variables for maintainability
$dashboard_widget_slug = ‘dashboard-widget’;
$allowed_roles = [‘administrator’, ‘editor’, ‘author’];

/**
* Check if current user has any of the allowed roles.
*/
function user_has_allowed_role(array $allowed_roles): bool
{
if (!is_user_logged_in()) {
return false;
}

$user = wp_get_current_user();
return (bool) array_intersect($user->roles, $allowed_roles);
}

// Enqueue iframe script on Dashboard only for allowed roles
add_action(‘admin_enqueue_scripts’, function ($hook_suffix) use ($allowed_roles, $dashboard_widget_slug) {

if ($hook_suffix !== ‘index.php’) {
return;
}

if (!user_has_allowed_role($allowed_roles)) {
return;
}

$iframe_url = home_url(“/{$dashboard_widget_slug}/”);

wp_enqueue_script(
‘custom-dashboard-iframe’,
get_stylesheet_directory_uri() . ‘/js/custom-dashboard-iframe.js’,
[],
‘1.0’,
true
);

wp_localize_script(‘custom-dashboard-iframe’, ‘customDashboardIframeData’, [
‘url’ => esc_url_raw($iframe_url),
]);

}, 1);

// Restrict access to the dashboard-widget page only to allowed roles
add_action(‘template_redirect’, function () use ($allowed_roles, $dashboard_widget_slug) {

if (!is_page($dashboard_widget_slug)) {
return;
}

if (!user_has_allowed_role($allowed_roles)) {
wp_redirect(home_url());
exit;
}

// Hide admin bar when viewing iframe page
show_admin_bar(false);

});

// Hide all default dashboard widgets (clean dashboard)
add_filter(‘default_hidden_meta_boxes’, function ($hidden, $screen) {

if ($screen->base !== ‘dashboard’) {
return $hidden;
}

global $wp_meta_boxes;

if (empty($wp_meta_boxes[‘dashboard’])) {
return $hidden;
}

foreach ($wp_meta_boxes[‘dashboard’] as $context) {
foreach ($context as $priority) {
foreach ($priority as $id => $widget) {
$hidden[] = $id;
}
}
}

return array_unique($hidden);

}, 10, 2);


 

Java Script Snippet:

document.addEventListener(‘DOMContentLoaded’, function () {

// Load the Elementor dashboard page inside iframe
const iframe = document.createElement(‘iframe’);
iframe.src = customDashboardIframeData.url;
iframe.style.width = ‘100%’;
iframe.style.height = ‘100vh’;
iframe.style.border = ‘0’;
iframe.id = ‘custom-dashboard-iframe’;

const dashboardWrap = document.getElementById(‘wpbody-content’);
if (dashboardWrap) {
dashboardWrap.innerHTML = ”;
dashboardWrap.appendChild(iframe);
}

// Force ALL wp-admin links inside iframe to open in NEW TAB
iframe.addEventListener(‘load’, function () {

try {
const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;

iframeDoc.addEventListener(‘click’, function (e) {
const link = e.target.closest(‘a’);
if (!link) return;

if (link.href && link.href.includes(‘/wp-admin/’)) {
e.preventDefault();
window.open(link.href, ‘_blank’); // open in new tab
}
});

} catch (err) {
console.warn(‘Iframe link handler blocked by browser:’, err);
}

});

});


 

After you add the snippet, create a new page and name it “Dashboard Widget”. What ever you design will show up in the dashboard.

Additional Instructions/Information
Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.