Pass variables to extended class

635 views Asked by At

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");') );
1

There are 1 answers

2
igormukhingmailcom On

Maybe you need something like this?

class My_WP_Widget extends WP_Widget
{
    protected $id_base = null; // Will generated automatically in WP_Widget constructor
    protected $classname = null; // Will generated automatically in WP_Widget constructor
    protected $name = 'Default widget name';
    protected $description = 'Default widget description';
    protected $filename;

    public function __construct() {
        $widget_options = array(
            'classname' => $this->classname, 
            'description' => $this->description,
            'filename' => $this->filename
        );

        parent::__construct($this->id_base, $this->name, $widget_options);
    }
}

class FilenameOverriding_My_WP_Widget extends My_WP_Widget
{
    protected $filename = 'overriden/filename.ext';
}

// Usage:
add_action( 'widgets_init', function(){
    return register_widget( 'FilenameOverriding_My_WP_Widget' );
});