Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Replace the default WordPress dashboard with a custom one
<?php
/**
* Plugin Name: Replace WordPress Dashboard
* Description: Replaces the default WordPress dashboard with a custom one.
* Author: Micah Wood
* Author URI: http://micahwood.me
* Version: 0.1
* License: GPL3
*/
/**
* This plugin offers a starting point for replacing the WordPress dashboard. If you are familiar with object oriented
* programming, just subclass and overwrite the set_title() and page_content() methods. Otherwise, just alter the
* set_title() and page_content() functions as needed.
*
* Customize which users are redirected to the custom dashboard by changing the capability property.
*
* If you don't want this plugin to be deactivated, just drop this file in the mu-plugins folder in the wp-content
* directory. If you don't have an mu-plugins folder, just create one.
*/
class Replace_WP_Dashboard {
protected $capability = 'read';
protected $title;
final public function __construct() {
if( is_admin() ) {
add_action( 'init', array( $this, 'init' ) );
}
}
final public function init() {
if( current_user_can( $this->capability ) ) {
$this->set_title();
add_filter( 'admin_title', array( $this, 'admin_title' ), 10, 2 );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'current_screen', array( $this, 'current_screen' ) );
}
}
/**
* Sets the page title for your custom dashboard
*/
function set_title() {
if( ! isset( $this->title ) ) {
$this->title = __( 'Dashboard' );
}
}
/**
* Output the content for your custom dashboard
*/
function page_content() {
$content = __( 'Welcome to your new dashboard!' );
echo <<<HTML
<div class="wrap">
<h2>{$this->title}</h2>
<p>{$content}</p>
</div>
HTML;
}
/**
* Fixes the page title in the browser.
*
* @param string $admin_title
* @param string $title
* @return string $admin_title
*/
final public function admin_title( $admin_title, $title ) {
global $pagenow;
if( 'admin.php' == $pagenow && isset( $_GET['page'] ) && 'custom-page' == $_GET['page'] ) {
$admin_title = $this->title . $admin_title;
}
return $admin_title;
}
final public function admin_menu() {
/**
* Adds a custom page to WordPress
*/
add_menu_page( $this->title, '', $this->capability, 'custom-page', array( $this, 'page_content' ) );
/**
* Remove the custom page from the admin menu
*/
remove_menu_page('custom-page');
/**
* Make dashboard menu item the active item
*/
global $parent_file, $submenu_file;
$parent_file = 'index.php';
$submenu_file = 'index.php';
/**
* Rename the dashboard menu item
*/
global $menu;
$menu[2][0] = $this->title;
/**
* Rename the dashboard submenu item
*/
global $submenu;
$submenu['index.php'][0][0] = $this->title;
}
/**
* Redirect users from the normal dashboard to your custom dashboard
*/
final public function current_screen( $screen ) {
if( 'dashboard' == $screen->id ) {
wp_safe_redirect( admin_url('admin.php?page=custom-page') );
exit;
}
}
}
new Replace_WP_Dashboard();
@yasherkrut
Copy link

Will be very grateful if you give an advice how to edit this code to make this panel visible on other users (not only administrator, but also authors, readers, for example). Because when I enter to admin panel as author I see "unable to load custom-page". Thank you a lot!!!

@jonbarratt
Copy link

To resolve the capability issue:

add_menu_page( $this->title, '', 'manage_options', 'custom-page', array( $this, 'page_content' ) );

should be changed to -

add_menu_page( $this->title, '', $this->capability, 'custom-page', array( $this, 'page_content' ) );

@kevindees
Copy link

kevindees commented Jul 27, 2018

Hey @wpscholar

Check this file -> https://gist.github.com/kevindees/9e32e9e4dc036a3107466c5faf50416a/revisions#diff-959d55dbd40dafbebdb263bc9ca1e6b4

I added a fix for the update submenu link; it was not being set to current. My patch fixes the issue.

/**
* Make dashboard menu item the active item
*/
global $pagenow, $plugin_page;
if(in_array($pagenow, ['update-core.php'])) {
    add_filter('parent_file', function($v) { return 'index.php'; }, 9999);
    add_filter('submenu_file',function($v) use ($pagenow) { return $pagenow;  }, 9999);
} elseif($plugin_page == 'custom-page') {
    add_filter('parent_file', function($v) { return 'index.php'; }, 9999);
    add_filter('submenu_file',function($v) { return 'index.php'; }, 9999);
}

@effizo
Copy link

effizo commented Feb 16, 2022

Thank you so much for this.
Please I would like to add custom page links and additional items into the dashboard page like the below image. Please how can I do this? I really appreciate your help in advance. Thank you so much.
custom-dashboard

@wpscholar
Copy link
Author

@effizo Replace the content on this line with your custom dashboard markup: https://gist.github.com/wpscholar/4744033#file-replace-wp-dashboard-php-L61

@effizo
Copy link

effizo commented Feb 17, 2022

Thank you so much sir for your prompt response. I really appreciate your time and support. Thanks so much.

@effizo
Copy link

effizo commented Mar 2, 2022

Hello,
Please this question is unrelated to the project. I just wanted to ask if you have done any project on job scrapping for job boards. I mean something that can scrape jobs that are posted all over the web, and compiles them all in your own job board.
Thank you very much.

@wpscholar
Copy link
Author

@effizo No, I have not.

@effizo
Copy link

effizo commented Mar 2, 2022

Thank you for your response. In case you do the project in future or know anyone who has done it, please let me know.
Once again, I am grateful for your prompt response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment