Skip to content

Instantly share code, notes, and snippets.

@drblue
Last active June 17, 2021 08:00
Show Gist options
  • Save drblue/9d1a6763a9f773ad7b87ac1eb7ec8cd9 to your computer and use it in GitHub Desktop.
Save drblue/9d1a6763a9f773ad7b87ac1eb7ec8cd9 to your computer and use it in GitHub Desktop.

Revisions

  1. drblue revised this gist Jun 17, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion class.MyWidgetSkeleton.php
    Original file line number Diff line number Diff line change
    @@ -44,7 +44,7 @@ public function form($instance) {
    // do we have a title set? if so, use it, otherwise set empty title
    $title = isset($instance['title'])
    ? $instance['title']
    : get_option('ww_default_title', __('Current Weather', 'wcm20-weather'));
    : __('A skeleton title', 'textdomain');

    ?>
    <!-- title -->
  2. drblue created this gist Jun 17, 2021.
    83 changes: 83 additions & 0 deletions class.MyWidgetSkeleton.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,83 @@
    <?php

    class MyWidgetSkeleton extends WP_Widget {

    /**
    * Construct a new widget instance.
    */
    public function __construct() {
    parent::__construct(
    'my-widget-skeleton', // Base ID
    'Skeleton Widget 💀', // Name
    [
    'description' => 'A widget skeleton.',
    ]
    );
    }

    /**
    * Front-end display of the widget.
    *
    * @see WP_Widget::widget()
    *
    * @param array $args Widget arguments.
    * @param array $instance Saved option values for this specific instance of the widget.
    * @return void
    */
    public function widget($args, $instance) {
    // start widget
    echo $args['before_widget'];

    // end widget
    echo $args['after_widget'];
    }

    /**
    * Back-end widget form
    *
    * @see WP_Widget::form()
    *
    * @param array $instance Current saved values for this instance of the widget.
    * @return void
    */
    public function form($instance) {
    // do we have a title set? if so, use it, otherwise set empty title
    $title = isset($instance['title'])
    ? $instance['title']
    : get_option('ww_default_title', __('Current Weather', 'wcm20-weather'));

    ?>
    <!-- title -->
    <p>
    <label for="<?php echo $this->get_field_id('title') ?>">Title:</label>

    <input
    class="widefat"
    id="<?php echo $this->get_field_id('title') ?>"
    name="<?php echo $this->get_field_name('title') ?>"
    type="text"
    value="<?php echo $title; ?>"
    >
    </p>
    <?php
    }

    /**
    * Sanitize widget form data before they are saved to the database.
    *
    * @see WP_Widget::update()
    *
    * @param array $new_instance Form values just sent to be saved.
    * @param array $old_instance Currently saved values.
    * @return void
    */
    public function update($new_instance, $old_instance) {
    $instance = [];

    $instance['title'] = (!empty($new_instance['title']))
    ? strip_tags($new_instance['title'])
    : '';

    return $instance;
    }
    }