I use the same code to create a widget in WordPress. The code I use extends a class already. It works fine.
Rather than copy/paste this every time I need a new widget it makes sense to make this so I can call it with a number of parameters and get it to work.
I have 4 variables to pass. I'm not familiar with how to do this with classes. If this was a function I would call it like
function function_name($functionname, $classname, $description, $filename)
How can I achieve a similar results when I extend a class. You can see the four variables above. The end result is to create a widget item in WordPress which when loaded adds a file from a specific location.
Although this is used in WordPress I decided to post on stackoverflow and not wp-stackoverflow because this relates to classes more generally.
class exampleWidget extends WP_Widget
{
function exampleWidget()
{
$widget_ops = array('classname' => 'exampleWidget', 'description' => 'Description here...' );
$this->WP_Widget('exampleWidget', 'Description - Adds Switch', $widget_ops);
}
function form($instance)
{
$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
$title = $instance['title'];
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
<?php
}
function update($new_instance, $old_instance)
{
$instance = $old_instance;
$instance['title'] = $new_instance['title'];
return $instance;
}
function widget($args, $instance)
{
extract($args, EXTR_SKIP);
echo $before_widget;
$title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
if (!empty($title))
echo $before_title . $title . $after_title;;
// widget code here
get_template_part( 'includes/widget/widget-name' );
echo $after_widget;
}
}
add_action( 'widgets_init', create_function('', 'return register_widget("exampleWidget");') );
Maybe you need something like this?