Get the Official WPSaviour App Now!

BlogTutorials

Make A WordPress Dashboard Widget Configurable

wp_add_dashboard_widget() tutorial

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_configurableThe 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:

/**
 * Register all dashboard metaboxes
 *
 * @entry      public
 * @since       1.0 
 * @return      void
*/

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() ).

/**
 * Creates the RSS metabox configuration settings
 *
 * @entry      public
 * @since       1.0 
 * @return      void
*/
function rc_mdm_configure_my_rss_box( $widget_id ) {

	// Get widget selections
	if ( !$rc_mdm_widget_options = get_option( 'rc_mdm_dashboard_widget_options' ) )
		$rc_mdm_widget_options = array();

	// Substitute widget selections
	if ( 'POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['rc_mdm_widget_post']) ) {
		update_option( 'rc_mdm_dashboard_widget_options', $_POST['rc_mdm_widget'] );
	}
	
	// Retrieve feed URLs
	$url_1 = $rc_mdm_widget_options['url_1'];
	$url_2 = $rc_mdm_widget_options['url_2'];
	$url_3 = $rc_mdm_widget_options['url_3']; ?>

	<p>
		<label for="rc_mdm_url_1-"><?php _e('Enter the RSS feed URL #1:', 'rc_mdm'); ?></label>
		<enter class="widefat" id="rc_mdm_url_1" title="rc_mdm_widget[url_1]" type="textual content material" value="<?php if( isset($url_1) ) echo $url_1; ?>" />
	</p>
	
	<p>
		<label for="rc_mdm_url_2"><?php _e('Enter the RSS feed URL #2:', 'rc_mdm'); ?></label>
		<enter class="widefat" id="rc_mdm_url_2" title="rc_mdm_widget[url_2]" type="textual content material" value="<?php if( isset($url_2) ) echo $url_2; ?>" />
	</p>
	
	<p>
		<label for="rc_mdm_url_3"><?php _e('Enter the RSS feed URL #3:', 'rc_mdm'); ?></label>
		<enter class="widefat" id="rc_mdm_url_3" title="rc_mdm_widget[url_3]" type="textual content material" value="<?php if( isset($url_3) ) echo $url_3; ?>" />
	</p>
	
	<enter title="rc_mdm_widget_post" type="hidden" value="1" />
	<?php
} ?>

Now you may reload your dashboard, and for those who occur to position your cursor on the the widget metabox you’ll see a “configure” hyperlink displaying on the best correct. When you click on on on it the alternatives kind is loaded. And when filling the fields and saving the feed URLs are retailer in our selections SQL desk.

Step 3: Retrieving Selections

The ultimate step is to modify the feed URLs we laborious coded throughout the first part of the tutorial. From line 14 to 18 in rc_mdm_create_my_rss_box() we added two URLs. Merely trade it by:

// My feeds guidelines
if ( !$my_feeds = get_option( 'rc_mdm_dashboard_widget_options' ) )
    $my_feeds = array();

And that’s it! We managed in order so as to add personalized selections to a dashboard widget quiet merely.

Download The WPSaviour App Now

Related posts
Beginner’s GuideBlogShowcase

7 Best Shopify Alternatives in 2020 (Cheaper and More Powerful)

Beginner’s GuideBest WordPress PluginsBlog

14 Best AI Chatbots Software for Your Website (Comparison)

Beginner’s GuideBlogTips

Magento vs WooCommerce – Which one is Better? (Comparison)

Beginner’s GuideBlogTips

12 Best Calculator Plugins for Your WordPress Site

Sign up for our Newsletter and
stay informed

Fear Of Missing Out?

Sign up with your email address to receive WordPress tips and updates
SUBSCRIBE NOW
Terms and Conditions apply
close-link
Click Me