Last active
October 17, 2020 11:00
-
-
Save kisildev/02adb86809eeff2681dfbbefa08fb484 to your computer and use it in GitHub Desktop.
WP widget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* new WordPress Widget format | |
* Wordpress 2.8 and above | |
* @see http://codex.wordpress.org/Widgets_API#Developing_Widgets | |
*/ | |
class PREFIX_Name_Widget extends WP_Widget { | |
/** | |
* Constructor | |
* | |
* @return void | |
**/ | |
function PREFIX_Name_Widget() { | |
$widget_ops = array( 'classname' => 'CSS class name', 'description' => 'Description' ); | |
$this->WP_Widget( 'CSS class name', 'Title', $widget_ops ); | |
} | |
/** | |
* Outputs the HTML for this widget. | |
* | |
* @param array An array of standard parameters for widgets in this theme | |
* @param array An array of settings for this widget instance | |
* @return void Echoes it's output | |
**/ | |
function widget( $args, $instance ) { | |
extract( $args, EXTR_SKIP ); | |
echo $before_widget; | |
echo $before_title; | |
echo 'Title'; // Can set this with a widget option, or omit altogether | |
echo $after_title; | |
// | |
// Widget display logic goes here | |
// | |
echo $after_widget; | |
} | |
/** | |
* Deals with the settings when they are saved by the admin. Here is | |
* where any validation should be dealt with. | |
* | |
* @param array An array of new settings as submitted by the admin | |
* @param array An array of the previous settings | |
* @return array The validated and (if necessary) amended settings | |
**/ | |
function update( $new_instance, $old_instance ) { | |
// update logic goes here | |
$updated_instance = $new_instance; | |
return $updated_instance; | |
} | |
/** | |
* Displays the form for this widget on the Widgets page of the WP Admin area. | |
* | |
* @param array An array of the current settings for this widget | |
* @return void Echoes it's output | |
**/ | |
function form( $instance ) { | |
$instance = wp_parse_args( (array) $instance, array( array of option_name => value pairs ) ); | |
// display field names here using: | |
// $this->get_field_id( 'option_name' ) - the CSS ID | |
// $this->get_field_name( 'option_name' ) - the HTML name | |
// $instance['option_name'] - the option value | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @package Kinsta_widget | |
* @version 1.0 | |
*/ | |
/* | |
Plugin Name: Kinsta Widget | |
Plugin URI: http://wordpress.org/extend/plugins/# | |
Description: This is an example plugin | |
Author: Your Name | |
Version: 1.0 | |
Author URI: http://example.com/ | |
*/ | |
class Kinsta_Widget extends WP_Widget { | |
/** | |
* Sets up the widgets name etc | |
* | |
* @link https://developer.wordpress.org/reference/classes/wp_widget/__construct/ | |
* @see https://developer.wordpress.org/reference/functions/wp_register_sidebar_widget/ | |
* | |
*/ | |
public function __construct() {} | |
/** | |
* Outputs the content of the widget on front-end | |
* | |
* @param array $args Widget arguments | |
* @param array $instance | |
* | |
* @link https://developer.wordpress.org/reference/classes/wp_widget/widget/ | |
*/ | |
public function widget( $args, $instance ) {} | |
/** | |
* Outputs the options form on admin | |
* | |
* @param array $instance The widget options | |
* | |
* @link https://developer.wordpress.org/reference/classes/wp_widget/form/ | |
*/ | |
public function form( $instance ) {} | |
/** | |
* Processing widget options on save | |
* | |
* @param array $new_instance The new options | |
* @param array $old_instance The previous options | |
* | |
* @link https://developer.wordpress.org/reference/classes/wp_widget/update/ | |
*/ | |
public function update( $new_instance, $old_instance ) {} | |
} | |
// register Kinsta_Widget | |
add_action( 'widgets_init', function(){ | |
register_widget( 'Kinsta_Widget' ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment