1. Home
  2. Docs
  3. WordPress Studio Framework
  4. Framework Locations
  5. Widget Options

Widget Options

The WordPress Studio Framework (WPSF) can also be used to create widgets that can be used in a WordPress site.

Widget Options Arguments

NameTypeDefaultDescription
titlestring Title of the widget in the backend
descriptionstring Description of widget in the backend
classnamestring CSS classes (space separated) to append to the front-end widget display.
widthnumber250Width of the fully expanded control form
defaultsarray Sets all default values from an external array (optional)
classstring Extra CSS classes (space separated) to append to the main framework.

Widget Options Framework Example

// Check to see if the WPSF class is loaded
if ( class_exists( 'WPSF' ) ){

  //Create a Widget
  WPSF::createWidget( 'custom_widget_example', array(
    'title' => 'Example Widget',
    'classname' => 'custom-widget-classname',
    'description' => 'A description for the Example Widget',
    'fields' => array(
      array(
        'id' => 'title',
        'type' => 'text',
        'title' => 'Title',
      ),
      array(
        'id' => 'example-text',
        'type' => 'text',
        'title' => 'Example Text',
        'default' => 'Default Text in the Field',
      ),
      array(
        'id' => 'example-switcher',
        'type' => 'switcher',
        'title' => 'Example Switcher',
      ),
      array(
        'id' => 'example-textarea',
        'type' => 'textarea',
        'title' => 'Example Textarea',
        'help' => 'The help text of the field.',
      ),
    )
  ));

  // Frontend display of the example widget
  if ( ! function_exists( 'custom_widget_example' ) ){
    function custom_widget_example( $args, $instance ){
      echo $args['before_widget'];
      if ( ! empty( $instance['title'] ) ){
        echo $args['before_title'] . apply_filters('widget_title', $instance['title'] ) . $args['after_title'];
      }
      //var_dump($args); // Widget Arguments
      //var_dump($instance); // Saved values from database
      echo $instance['title'];
      echo $instance['example-text'];
      echo $instance['example-switcher'];
      echo $instance['example-textarea'];

      echo $args['after_widget'];
    }
  }
}

 

Was this article helpful to you? Yes No

How can we help?