You are on page 1of 4

Instantly share code, notes, and snippets.

wpscholar / replace-wp-dashboard.php
Last active 4 months ago

Star

Code Revisions 6 Stars 13 Forks 12

Replace the default WordPress dashboard with a custom one

replace-wp-dashboard.php

1 <?php
2
3 /**
4 * Plugin Name: Replace WordPress Dashboard
5 * Description: Replaces the default WordPress dashboard with a custom one.
6 * Author: Micah Wood
7 * Author URI: http://micahwood.me
8 * Version: 0.1
9 * License: GPL3
10 */
11
12 /**
13 * This plugin offers a starting point for replacing the WordPress dashboard. If you are
14 * programming, just subclass and overwrite the set_title() and page_content() methods. Ot
15 * set_title() and page_content() functions as needed.
16 *
17 * Customize which users are redirected to the custom dashboard by changing the capability
18 *
19 * If you don't want this plugin to be deactivated, just drop this file in the mu-plugins
20 * directory. If you don't have an mu-plugins folder, just create one.
21 */
22
23 class Replace_WP_Dashboard {
24
25 protected $capability = 'read';
26
27 protected $title;
28
29 final public function __construct() {

30 if( is_admin() ) {
31 add_action( 'init', array( $this, 'init' ) );
32 }
33 }
34
35 final public function init() {
36 if( current_user_can( $this->capability ) ) {
37 $this->set_title();
38 add_filter( 'admin_title', array( $this, 'admin_title' ), 10, 2 );
39 add_action( 'admin_menu', array( $this, 'admin_menu' ) );
40 add_action( 'current_screen', array( $this, 'current_screen' ) );
41 }
42 }
43
44 /**
45 * Sets the page title for your custom dashboard
46 */
47 function set_title() {
48 if( ! isset( $this->title ) ) {
49 $this->title = __( 'Dashboard' );
50 }
51 }
52
53 /**
54 * Output the content for your custom dashboard
55 */
56 function page_content() {
57 $content = __( 'Welcome to your new dashboard!' );
58 echo <<<HTML
59 <div class="wrap">
60 <h2>{$this->title}</h2>
61 <p>{$content}</p>
62 </div>
63 HTML;
64 }
65
66 /**
67 * Fixes the page title in the browser.
68 *
69 * @param string $admin_title
70 * @param string $title
71 * @return string $admin_title
72 */
73 final public function admin_title( $admin_title, $title ) {
74 global $pagenow;
75 if( 'admin.php' == $pagenow && isset( $_GET['page'] ) && 'custom-page' == $_GET['p
76 $admin_title = $this->title . $admin_title;
77 }
78 return $admin_title;
79 }
80
81 final public function admin_menu() {
82
83 /**
84 * Adds a custom page to WordPress
85 */
86 add_menu_page( $this->title, '', $this->capability, 'custom-page', array( $this, '
87
88 /**
89 * Remove the custom page from the admin menu
90 */
91 remove_menu_page('custom-page');
92
93 /**
94 * Make dashboard menu item the active item
95 */
96 global $parent_file, $submenu_file;
97 $parent_file = 'index.php';
98 $submenu_file = 'index.php';
99
100 /**
101 * Rename the dashboard menu item
102 */
103 global $menu;
104 $menu[2][0] = $this->title;
105
106 /**
107 * Rename the dashboard submenu item
108 */
109 global $submenu;
110 $submenu['index.php'][0][0] = $this->title;
111
112 }
113
114 /**
115 * Redirect users from the normal dashboard to your custom dashboard
116 */
117 final public function current_screen( $screen ) {
118 if( 'dashboard' == $screen->id ) {
119 wp_safe_redirect( admin_url('admin.php?page=custom-page') );
120 exit;
121 }
122 }
123
124 }
125
126 new Replace_WP_Dashboard();

yasherkrut commented on 31 Jan 2017


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 commented on 9 Jan 2018

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 commented on 27 Jul 2018 • edited

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);
}

You might also like