Console Widget Development. Widget API WordPress

If you have basic programming knowledge, then it will not be difficult for you to learn how to create your own WordPress widgets. This article will be useful to you if you are preparing to create your own WordPress plugins.. I won't pull, just start making your widgets.

Building a simple plugin

Let's create a simple plugin first., for this we need to create in the plugins folder /wp-content/plugins/ file test-plugin.php. In the created file, create the code:

I will not describe the plugin lines now., think, for this it is worth devoting a separate article, but this simple code will allow us to display our plugin in the admin panel, you can activate it immediately.

This is how the plugin will look in the WordPress admin panel in the plugins section.

widget api wordpress на русском

After activating the plugin, we can continue to add it. The main tool for adding a widget is the function wp_add_dashboard_widget () which adds a widget to the WordPress dashboard. Consider the function input:

  • $widget_id (integer) – required parameter, identifier, will be used as the CSS class and key in the widgets array of the WordPress Dashboard.
  • $widget_name (line) – required parameter, widget name, which will be displayed in the header of the widget.
  • $callback (line) – required parameter, function name, which will display the contents of the Console widget.
  • $control_callback (line) – optional parameter, callback, function name, which will handle the form of the new Console widget. For the time being, we will not work with this function..

 

Example and order of entering parameters for the wp_add_dashboard_widget function

Widget add function code

Running Widgets in the Console implements an action wp_dashboard_setup which calls the function add_dashboard_widgets. In our example, the function add_dashboard_widgets responsible for displaying information in the widget block.

As a result of using this code in our plugin, the widget will be shown in the CMS console.

Widget output example

create widget example

This example can be used to output all sorts of important information to the console (online store sales statistics, contacts of the studio that developed the site and maintains it, articles that were published by users with a certain role or even by a separate user). Information can be displayed in various.

You probably noticed, that some widgets have additional settings, for example, the fresh comments widget can be configured and specify how many of them will be displayed in the console.

setting up widget api widgets

Let us consider in more detail the methods for implementing this functionality.. Firstly, for those who do not know in WordPress, intermediate options and parameters are saved using a special options mechanism. You can practice working with options in one of my articles called “Storing data in wordpress“.

If you are familiar with the options mechanism or you have read my articles and understood everything, then try to replace the old code of our plugin with this one:

I have commented on the given code example., to make it clearer what is happening in the code. Think, if you have carefully reviewed the material presented above, then there should be no questions about this code. If there are still questions, that you can ask questions in the comments at the end of the article.

 

Removing unnecessary widgets from the console

There are two approaches. The first approach is to clear the global array that contains the list of widgets like this:

This example removes the widget Quick Publication.

Other widgets can be removed if necessary, slightly changing the last nested key in the array:

Removes a widget “Plugins”

$wp_meta_boxes[‘dashboard’][‘normal’][‘core’][‘dashboard_plugins’]

Removes “Fresh drafts”

$wp_meta_boxes[‘dashboard’][‘side’][‘core’][‘dashboard_recent_drafts’]

Removes “Fresh comments”

$wp_meta_boxes[‘dashboard’][‘normal’][‘core’][‘dashboard_recent_comments’]

Removes a widget “Incoming links”

$wp_meta_boxes[‘dashboard’][‘normal’][‘core’][‘dashboard_incoming_links’]

Removes a widget “Right now”

$wp_meta_boxes[‘dashboard’][‘normal’][‘core’][‘dashboard_right_now’]

To remove all blocks of standard widgets from the WordPress Dashboard use the following code:

 

Also the second approach to remove, this is the use of special functions of the CMS core. Here is an example of the WordPress Codex.

If you don't need to remove widget blocks, but your widget after installing the plugin got to the end, it can be put at the beginning by default by rearranging the global array with all widgets (global $wp_meta_boxes;). I don’t write about array sorting and rearrangement here, these are php questions.

For more advanced work with widgets, you can always see how they were programmed in the very core of WordPress., in file:

/wp-admin/includes/dashboard.php

Read more about the Widget API

Code to display all options standard widgets.

To display one widget option you can use the function:

To update widget options WordPress codex offers the following function:

For a more complete study of the material, you may need articles:

For today, I think you will have enough material that you can study and test in your plugins. Good luck in developing quality sites.


Buy WordPress hosting
WordPress tutor
Online tutoring services. List of courses I teach
  • Basic web design course;
  • Site layout;
  • General course on CMS WordPress and continuation of the course on template development;
  • Website development in PHP.
Read more on the page WordPress tutor
*/

Development of console widgets. Widget API WordPress

If you have basic programming knowledge, then it is not difficult for you to learn how to create your own WordPress widgets. This article will be useful to you, if you are getting ready to build your WordPress plugins. I will not drag, just start making your widgets.
(more…)

Nikolaenko Maxim

Director of web studies ProGrafika. I am developing, website design and promotion. Always glad to new blog readers and good clients.


You may also like...

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Templates for WordPress
The best hosting in Ukraine
Stable hosting for Drupal