How to set up default values for a plugin?

Wordpress widget registration - Set default options

Set the default state of a checkbox to checked after installing process

Tags:
PHP

Set Widget settings to "checked by default"

If you want to set up default values for your Wordpress Widget, you have to manage some steps in your PHP code. It's more easier to set up with false or no checked options, but there are ways to handle true-presets.
Give a little notice to the Widget initializing, update, UI, hooks, etc. and your Wordpress Widget will get a true or checked option at start-up and after installing process.

After drag'n'drop the Widget to the Widget menu, the Checkbox is initialized and saved with checked (true).

The constructor

Setting up the Widget with the constructor. This is the first function which will be called after registration the Widget. Each time you set it up to a Widget menu the constructor function is called.
The constructor will allow us to set up the Widget default settings.

We'll call the overloaded WP_Widget function in the constructor with the default values in a $control_ops array.


class CategoryPosts extends WP_Widget {

	function __construct() {
		$widget_ops = array( 'classname' => 'cat-post-widget', 'description' => 'List posts' );
		$control_ops = array( 'show_title' => true );
		parent::__construct( 'category-posts', 'Category Posts Widget', $widget_ops, $control_ops );
	} // end __construct

The code is used on GitHub: Category posts Widget

Use the option imidatelly after set-up the Widget on the page

The Widget works in widget() function. Place all code here which should out-putted, displayed and make logical things. Here it means to show the Widget title if the Checkbox is checked or not hide it, if not.

Use the $instance array.


	function widget( $args, $instance ) {
		...
		if( isset ( $instance["show_title"] ) && $instance["show_title"] ) {
			echo $before_title;
			echo $instance["title"];
			echo $after_title;
		}
		...
	} // end widget

Making sure our widget is updated and saved

After using we have to take our Widget settings and save it. Its a pretty simple code for updating the Widget to use the new selected value.

Use the arrays $new_instance und $old_instance to save your Widget settings.


	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['show_title'] = $new_instance['show_title'];
		return $instance;
	} // end update

Use default values in the configuration UI with wp_parse_args

Configure the registration Widget UI setting.
For this use the wp_parse_args() function which overrids not setted values in the $instance array with default values.

Pre-set the option a second time for the form/UI. It's important just as the constructor pre-setting.


	function form( $instance ) {
		$defaults = array( 'show_title' => __( true ) );
		$instance = wp_parse_args( ( array ) $instance, $defaults );
		...

Controls and settings for the Widget configuration UI

For the form use a checkbox, which allow the user to select a true/false option.

Create a checkbox and close the PHP code.


		...
		<p>
			<label for="<?php echo $this->get_field_id("show_title"); ?>">
				<input type="checkbox" class="checkbox" 
					id="<?php echo $this->get_field_id("show_title"); ?>" 
					name="<?php echo $this->get_field_name("show_title"); ?>"
					<?php checked( (bool) $instance["show_title"], true ); ?> />
				<?php _e( 'Show title' ); ?>
			</label>
		</p>
	<?php
	} // end form
} // end CategoryPosts extends WP_Widget
?>

The code is used on GitHub: Category posts Widget

What you need now is good ideas for your own widget.

With this steps you can use default settings, pre-set options to true, store and update Widget values.
I use the code for the Widget I've used in the example aboveery and know that the code can operate.
If you're intetesstet while writing the code the Wordpress version was 4.3 and worked only with the PHP5 constructor.
If you wish you can get me a hint in which Wordpress Widget your code works. That would interest me already. Otherwise give me a hint anyway, leave a question or give me your suggestion below.

© Kometschuh.de | Impressum | GitHub | Facebook |