Add and remove widgets in WordPress admin page





When it comes to widgets in WordPress, you will probably think of widgets that are visible on the outside of the website. However, the widget I mentioned in this article is not to display outside the website but is the widget inside the admin page, it is located in the Dashboard section as shown below.

admin-widgets-1

And in this article, 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.

Infomation:

Add and Remove Widgets in WordPress Admin Page 15

[alert color=”orange” type=”alert-message-background” title=”Note” size=”small”]In this article, you will write code into the plugin created in post 2 in this series.[/alert]

How to remove default widget in admin page

If you’re like me, the widgets in the admin page are almost unused for a long time because it’s also too little information, we should just delete it so we don’t have to see it anymore.

To do this, we will create a function and hook it into the action hook name wp_dashboard_setup.


/**
* Xóa widget mặc định trong trang quản trị
*/
function tp_remove_default_admin_widget() {

}
add_action( ‘wp_dashboard_setup’, ‘tp_remove_default_admin_widget’ );

Add and Remove Widgets in WordPress Admin Page 15

And then in this function we use the function remove_meta_box to remove widget here. This function is used because these widgets are meta boxes created for a type of page called dashboard. And for example, I want to delete the WordPress News widget, there is the following paragraph:


/**
* Xóa widget mặc định trong trang quản trị
*/
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 default dashboard names here And if you want to delete something, copy it into your function.

Add and Remove Widgets in WordPress Admin Page 15

In case you want to remove the part that displays the welcome information in the Dashboard, you must use remove_action(), and put this code outside your function.

remove_action( ‘welcome_panel’, ‘wp_welcome_panel’ );

How to create a widget in the admin page

Usually when creating a website for a client, we may need to use the Dashboard section to post a reminder note, or advanced functions such as getting the latest news on our site, or updating the gold exchange rate. something….depending on the level of your code.

See more:  [Premium] 5 best themes to make a review / review website

In this tutorial I will show you how to create a simple widget and a widget that displays the latest posts on your site via RSS.

We also first create our own function and hook it to the action hook name wp_dashboard_setup. And in this function we use the function wp_add_dashboard_widget() to create a new widget.

Add and Remove Widgets in WordPress Admin Page 15

/**
* Tạo widget trong trang quản trị
*/
function tp_create_admin_widget_notice() {
wp_add_dashboard_widget( ‘tp_notice’, ‘Ghi chú nhắc nhở’, ‘tp_create_admin_widget_notice_callback’ );
}
add_action( ‘wp_dashboard_setup’, ‘tp_create_admin_widget_notice’ );

In the above paragraph, I have:

wp_add_dashboard_widget( ‘tp_notice’, ‘Ghi chú nhắc nhở’, ‘tp_create_admin_widget_notice_callback’ );

In there:

  • tp_notice: The ID of the widget, the ID must not coincide with other widgets.
  • Ghi chú nhắc nhở: Widget title name
  • tp_create_admin_widget_notice_callback: The name of the function that it will execute in this widget, here you can understand what this function contains and it will display in the widget with the ID tp_notice.

Okay, now let’s create the function tp_create_admin_widget_notice_callback() that we declared above to display the content to the created widget. In this function, I temporarily display a simple text.


function tp_create_admin_widget_notice_callback() {
echo ‘

Đây là nội dung mẫu trong bài hướng dẫn cách tạo một widget đơn giản trong trang quản trị.

Add and Remove Widgets in WordPress Admin Page 15

‘;
}

Save and the result we have:

admin-widget-2

And if you want to create a widget that displays a list of the latest articles on your website or a certain website, see the instructions in this article. Build Your Own WordPress Dashboard Widget for Any RSS Feed Please.

Add and Remove Widgets in WordPress Admin Page 15

Since you have full permission to use the PHP code in this section, you are free to create your own content that can be displayed here.

See more:  [Woocommerce] Template structure of Woocommerce

According to the mechanism of use, it is as simple as that. But hopefully once you know the simple things, it will be easier to find better ways to use the widgets in the admin page more useful.

Rate this content


Source: Add and remove widgets in WordPress admin page
– TechtipsnReview

, , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *