When it comes to widgets in WordPress, you probably think of
the widgets that appear on your site. However, the widget I mentioned in this
article is not for displaying outside the website but as a widget within the
admin page, which is located in the Dashboard as shown below.
And in this tutorial I will show you how to delete the
default widgets as well as create a new widget in the admin page to use it to
display some of the information you want.
How to delete the
default widget in the admin page
If you are like me, the widgets in the admin page almost do
not use it a long time because it is too little information, just as we should
delete it to avoid seeing it again.
To do this, we will create a function and hook it into the
action hook named wp_dashboard_setup.
01
02
03
04
05
06
07
|
function tp_remove_default_admin_widget() {
}
add_action( 'wp_dashboard_setup' , 'tp_remove_default_admin_widget' );
|
| |
And then in this function we use the remove_meta_box
function to delete the widget here. We use this function because these widgets
are meta boxes created for page type called dashboard. For example, if you want
to remove the WordPress News widget, you have the following:
01
02
03
04
05
06
07
08
09
|
Delete the default widget in the admin page
function tp_remove_default_admin_widget() {
remove_meta_box( 'dashboard_primary' , 'dashboard' , 'side' );
}
add_action( 'wp_dashboard_setup' , 'tp_remove_default_admin_widget' );
|
| |
Similarly, you can see a list of the default dashboard names
here and want to delete which one then copy that into your function.
In case you want to remove the welcome display in the
Dashboard, you must use remove_action (), and place the code outside of your
function.
01
|
remove_action( 'welcome_panel' , 'wp_welcome_panel' );
|
How to create a
widget in the admin page
Usually when creating a website for a customer, we may need
to use the Dashboard to post a reminder notice, or advanced features such as
getting the latest news on our site, or updating the price of gold. Something,
... depending on the level of your code.
In this tutorial I will teach you how to create a simple
widget and a widget to display the latest posts on your page via RSS.
We first create a separate function and hook it into the
action hook named wp_dashboard_setup. And in this function we use the function
wp_add_dashboard_widget () to create a new widget.
01
02
03
04
05
06
07
|
function tp_create_admin_widget_notice() {
wp_add_dashboard_widget( 'tp_notice' ,
'Reminder notes' , 'tp_create_admin_widget_notice_callback' );
}
add_action( 'wp_dashboard_setup' ,
'tp_create_admin_widget_notice' );
|
In the above paragraph, we have:
01
|
wp_add_dashboard_widget( 'tp_notice' , 'Reminder notes' ,
'tp_create_admin_widget_notice_callback' );
|
Inside:
tp_notice: ID of the widget, the ID must not be the same as
the other widgets.
Reminder notes: The title of the widget
tp_create_admin_widget_notice_callback: The name of the
function that it will execute in this widget, where you can understand what
this function contains which is displayed in the widget whose ID is tp_notice.
Okay, now let's create the function tp_create_admin_widget_notice_callback() that we have declared above to display the content of the generated widget.
In this function we temporarily display a simple text.
01
02
03
04
05
06
07
|
function tp_create_admin_widget_notice_callback() {
echo '
This is sample content in the tutorial on how to create a
simple widget in the admin page.
';
}
|
Save and the results we have:
If you want to create a widget to display the list of the
latest articles on your website or any website, see the instructions in the
article Build Your Own WordPress Dashboard Widget for Any RSS Feed.
Since you have full access to the PHP code in this section,
you can create your own content here.
The mechanism of use is just that. But hopefully once you
know the simplest things, you'll be able to easily find better ways to use
widgets in the more useful administration page.