wp_add_dashboard_widget() tutorial | WP – Saviour

By WP Saviour •  Updated: 03/04/21 •  3 min read

A few weeks up to now we seen  how to add a custom RSS dashboard metabox to the administration. At current, I’d wish to level out you the fitting method so as to add configurable selections to this dashboard metabox.

Proper right here is the outcomes of what we will do:

make_a_widget_configurable-8125554The very very first thing to do is to obviously study the first part of this tutorial to have the ability to create the metabox.

An essential function on this tutorial is wp_add_dashboard_widget(). It’s the WordPress function that registers a dashboard widget. We already seen that registering the widget and the function that outputs the widget on the dashboard is pretty easy, nevertheless we had not lined the ultimate parameter of this function. Primarily, wp_add_dashboard_widget() has 4 parameters:

$widget_id
(integer) (required) an determining slug in your widget. This may seemingly be used as its css class and its key throughout the array of widgets.
Default: None

$widget_name
(string) (required) that’s the title your widget will present in its heading.
Default: None

$callback
(string) (required) The title of a function you create that may present the exact contents of your widget.
Default: None

$control_callback
(string) (optionally accessible) The title of a function you create that may take care of submission of widget selections (configuration) varieties, and likewise will present the form components.
Default: None

As you presumably can see, the fourth parameter is the one accountable for the “configure” selections. When filling this parameter you tells WordPress to load a specific function to configure the widget. It routinely create a “configure” hyperlink throughout the metabox title when you place the cursor over it.

Step 1: Register The Configure Selections Carry out

So, the first step is to vary the wp_add_dashboard_widget()  title and add the title of the configure selections options throughout the last parameter. Your rc_mdm_register_widgets() options, ought to vary into:

 function rc_mdm_register_widgets() { world $wp_meta_boxes; wp_add_dashboard_widget('widget_custom_rss', __('My RSS Feeds', 'rc_mdm'), 'rc_mdm_create_my_rss_box', 'rc_mdm_configure_my_rss_box');
}
add_action('wp_dashboard_setup', 'rc_mdm_register_widgets');

We merely added “rc_mdm_configure_my_rss_box” to the ultimate parameter.

Step 2: Create The Configure Selections Carry out

This step isn’t tough. All we have to do is to create kind fields saved into an array. To take motion, we’ll use the update_option(). We don’t must create a full kind as WordPress creates it for us. We merely must register the fields. A superb degree to say, is that WordPress routinely gives to the form a nonce that makes the form secure and stay away from potential issues of safety. Proper right here is the code of our rc_mdm_configure_my_rss_box() function ( keep in mind? It’s the 4th parameter we outlined in wp_add_dashboard_widget() ).


function rc_mdm_configure_my_rss_box( $widget_id ) { if ( !$rc_mdm_widget_options = get_option( 'rc_mdm_dashboard_widget_options' ) ) $rc_mdm_widget_options = array(); if ( 'POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['rc_mdm_widget_post']) ) { update_option( 'rc_mdm_dashboard_widget_options', $_POST['rc_mdm_widget'] ); } $url_1 = $rc_mdm_widget_options['url_1']; $url_2 = $rc_mdm_widget_options['url_2']; $url_3 = $rc_mdm_widget_options['url_3']; ?> 

WP Saviour