-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom-wp-dashboard.php
177 lines (144 loc) · 5.31 KB
/
custom-wp-dashboard.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* Plugin Name: Custom WordPress Welcome Admin Dashboard
* Plugin URI: https://gist.github.com/wpfresher/8d3680d4d9d6d86f22f7f6cb1662ab1a
* Description: This plugin will replace the default WordPress admin dashboard with a new custom menu page. It works when the user has a 'Read' capability & does not has a 'Manage_Options' capability
* Version: 1.0.0
* Author: Kawsar Ahmed
* Author URI: http://wpfresher.com/
* Developer: Kawsar Ahmed
* Developer URI: http://wpfresher.com/
* Contributors: Kawsar Ahmed
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Requires at least: 5.2
* Tested up to: 6.3
* Requires PHP: 7.0
*/
/**
*
* This plugin provides an initial foundation for replacing the default WordPress admin dashboard. If you have a background in object-oriented programming,
* you can create a subclass to customize and override the set_the_title() and page_content_callback_fn() methods.
* Alternatively, if you're not familiar with object-oriented programming, you can modify the set_the_title() and page_content_callback_fn() functions according to your requirements.
*
* Modify the user redirection to the custom dashboard by adjusting the capability property for specific users.
* And add the capability property if you want to remove this custom dashboard and back to the default WordPress dashboard
*
* Remember: To ensure the uninterrupted functionality of this plugin, simply place this file into the "mu-plugins" folder within the "wp-content" directory.
* If the "mu-plugins" folder is not present, you can easily create one yourself.
*/
/**
* Exit if accessed directly
*/
defined( 'ABSPATH' ) || exit;
/**
* Custom WordPress Welcome Admin Dashboard
* Class executes when the user has 'Read' capability
* Not execute when the user has no 'Manage_Options' capability
*
* @since version 1.0.0
*/
class Custom_WP_Dashboard {
protected $menu_page_name = 'Dashboard';
protected $menu_page_slug = 'dashboard';
protected $capability = 'read';
protected $no_capability = 'manage_options';
protected $title;
/**
* Here "final" prefix is used, It's because we do not want to override these methods by other sub-classes or sub methods
*/
final public function __construct() {
if( is_admin() ) {
add_action( 'init', array( $this, 'init_callback_fn' ) );
}
}
final public function init_callback_fn() {
if( current_user_can( $this->capability) && !current_user_can( $this->no_capability ) ) {
$this->set_the_title();
add_action( 'admin_menu', array( $this, 'admin_menu_callback_fn' ) );
add_filter( 'admin_title', array( $this, 'admin_title_callback_fn' ), 10, 2 );
add_action( 'current_screen', array( $this, 'current_screen_callback_fn' ) );
}
}
/**
* Set the page title for your custom dashboard
*
* @since version 1.0.0
*/
function set_the_title() {
if( ! isset( $this->title ) ) {
$this->title = __( 'Dashboard' );
}
}
/**
* Output the content for your custom dashboard
*
* @since version 1.0.0
*/
function page_content_callback_fn() {
$content = __( 'Hi! Welcome to your new dashboard!' );
echo <<<HTML
<div class="wrap">
<h2>{$this->title}</h2>
<p>{$content}</p>
</div>
HTML;
}
/**
* Adding a custom menu page
* Removing the WordPress default Dashboard menu page
*
* @since version 1.0.0
*/
final public function admin_menu_callback_fn() {
/**
* Adds a custom page to WordPress
*
* @since version 1.0.0
* @reference https://developer.wordpress.org/reference/functions/add_menu_page/
*/
add_menu_page( $this->title, $this->menu_page_name, $this->capability, $this->menu_page_slug, array( $this, 'page_content_callback_fn' ), 'dashicons-dashboard', 0);
/**
* Remove the WordPress default Dashboard menu page
*
* @since version 1.0.0
* @reference https://developer.wordpress.org/reference/functions/remove_menu_page/
*/
remove_menu_page( 'index.php' );
}
/**
* Fixing the page title.
*
* @param string $admin_title
* @param string $title
* @return string $admin_title
*
* @since version 1.0.0
*/
public function admin_title_callback_fn( $admin_title, $title ) {
global $pagenow;
if( 'admin.php' == $pagenow && isset( $_GET['page'] ) && $this->menu_page_slug == $_GET['page'] ) {
$admin_title = $this->title . ' → ' . $admin_title;
}
return $admin_title;
}
/**
* Redirect users from the WordPress default welcome dashboard to your custom dashboard
*
* @since version 1.0.0
*/
function current_screen_callback_fn( $screen ) {
if( $screen->id == 'dashboard' ) {
wp_safe_redirect( admin_url( 'admin.php?page='. $this->menu_page_slug ) );
exit;
}
}
}
/**
* Custom WordPress Welcome Admin Dashboard
* Class executes when the user has 'Read' capability
* Not execute when the user has no 'Manage_Options' capability
*
* @since version 1.0.0
*/
new Custom_WP_Dashboard();