$this->description, 'classname' => $this->classname ); parent::WP_Widget($this->id_base, $this->widget_name, $widget_ops); } /** * Self-registering widget method. * * This can be called statically. * * @author Eddie Moya * @return void */ public function register_widget() { add_action('widgets_init', create_function( '', 'register_widget("' . __CLASS__ . '");' )); } /** * The front end of the widget. * * Do not call directly, this is called internally to render the widget. * * @author [Widget Author Name] * * @param array $args [Required] Automatically passed by WordPress - Settings defined when registering the sidebar of a theme * @param array $instance [Required] Automatically passed by WordPress - Current saved data for the widget options. * @return void */ public function widget( $args, $instance ){ extract($args); extract($instance); echo $before_title . $bp_title . $after_title; ?>
Check box '' was checked
Check box '' was checked
The chosen select option is:
} /** * Data validation. * * Do not call directly, this is called internally to render the widget * * @author [Widget Author Name] * * @uses esc_attr() http://codex.wordpress.org/Function_Reference/esc_attr * * @param array $new_instance [Required] Automatically passed by WordPress * @param array $old_instance [Required] Automatically passed by WordPress * @return array|bool Final result of newly input data. False if update is rejected. */ public function update($new_instance, $old_instance){ /* Lets inherit the existing settings */ $instance = $old_instance; /** * Sanitize each option - be careful, if not all simple text fields, * then make use of other WordPress sanitization functions, but also * make use of PHP functions, and use logic to return false to reject * the entire update. * * @see http://codex.wordpress.org/Function_Reference/esc_attr */ foreach($new_instance as $key => $value){ $instance[$key] = esc_attr($value); } /** * This handles checkboxes. **/ foreach($instance as $key => $value){ if($value == 'on' && !isset($new_instance[$key])){ unset($instance[$key]); } } return $instance; } /** * Generates the form for this widget, in the WordPress admin area. * * The use of the helper functions form_field() and form_fields() is not * neccessary, and may sometimes be inhibitive or restrictive. * * @author Eddie Moya * * @uses wp_parse_args() http://codex.wordpress.org/Function_Reference/wp_parse_args * @uses self::form_field() * @uses self::form_fields() * * @param array $instance [Required] Automatically passed by WordPress * @return void */ public function form($instance){ /* Setup default values for form fields - associtive array, keys are the field_id's */ $defaults = array('bp_title' => 'Default Value of Text Field', 'bp_select' => 'option1'); /* Merge saved input values with default values */ $instance = wp_parse_args((array) $instance, $defaults); /* Example of multiple inputs at once. */ $fields = array( array( 'field_id' => 'bp_title', 'type' => 'text', 'label' => 'Enter Title' ), array( 'field_id' => 'bp_select', 'type' => 'select', 'label' => 'Select an Option', 'options' => array( /* These option associative keys can be whatever you want, * no spaces or funny characters though. */ 'option1' => 'Option 1', 'option2' => 'Option 2' ) ) ); /* Builds a series of inputs based on the $fields array created above. */ $this->form_fields($fields, $instance); /* Examples of input fields one at a time. */ $this->form_field('bp_string', 'text', 'Enter String', $instance); $this->form_field('bp_checkbox', 'checkbox', 'Choice 1', $instance); $this->form_field('bp_checkbox_2', 'checkbox', 'Choice 2', $instance); $this->form_field('bp_textarea', 'textarea', 'Enter Lots of Text', $instance); $this->form_field('bp_checkbox_3', 'checkbox', 'Choice 3', $instance); } /** * Helper function - does not need to be part of widgets, this is custom, but * is helpful in generating multiple input fields for the admin form at once. * * This is a wrapper for the singular form_field() function. * * @author Eddie Moya * * @uses self::form_fields() * * @param array $fields [Required] Nested array of field settings * @param array $instance [Required] Current instance of widget option values. * @return void */ private function form_fields($fields, $instance, $group = false){ if($group) { echo ""; } foreach($fields as $field){ extract($field); $label = (!isset($label)) ? null : $label; $options = (!isset($options)) ? null : $options; $this->form_field($field_id, $type, $label, $instance, $options, $group); } if($group){ echo "
"; } } /** * Helper function - does not need to be part of widgets, this is custom, but * is helpful in generating single input fields for the admin form at once. * * @author Eddie Moya * * @uses get_field_id() (No Codex Documentation) * @uses get_field_name() http://codex.wordpress.org/Function_Reference/get_field_name * * @param string $field_id [Required] This will be the CSS id for the input, but also will be used internally by wordpress to identify it. Use these in the form() function to set detaults. * @param string $type [Required] The type of input to generate (text, textarea, select, checkbox] * @param string $label [Required] Text to show next to input as its label. * @param array $instance [Required] Current instance of widget option values. * @param array $options [Optional] Associative array of values and labels for html Option elements. * * @return void */ private function form_field($field_id, $type, $label, $instance, $options = array(), $group = false){ if(!$group) echo ""; $input_value = (isset($instance[$field_id])) ? $instance[$field_id] : ''; switch ($type){ case 'text': ?> /> "; } } Boilerplate_Widget::register_widget();