-
-
Save ScottPhillips/4587050 to your computer and use it in GitHub Desktop.
class Widget_Setup | |
{ | |
public $widget_class = ''; | |
public $admin_styles = array(); | |
public $admin_scripts = array(); | |
public $front_styles = array(); | |
public $front_scripts = array(); | |
public $script_defaults = array( | |
'handle' => '', | |
'src' => '', | |
'deps' => array(), | |
'version' => false, | |
'in_footer' => false | |
); | |
public $style_defaults = array( | |
'handle' => '', | |
'src' => '', | |
'deps' => array(), | |
'version' => false, | |
'media' => 'all' | |
); | |
public function __construct( $widget_class = '', $admin_styles = array(), $admin_scripts = array(), $front_styles = array(), $front_scripts = array() ) { | |
$this->widget_class = $widget_class; | |
$this->admin_styles = $admin_styles; | |
$this->admin_scripts = $admin_scripts; | |
$this->front_styles = $front_styles; | |
$this->front_scripts = $front_scripts; | |
add_action( 'admin_print_styles', array( $this, 'add_styles' ) ); | |
add_action( 'admin_enqueue_scripts', array( $this, 'add_scripts' ) ); | |
add_action( 'wp_enqueue_scripts', array( $this, 'add_styles' ) ); | |
add_action( 'wp_enqueue_scripts', array( $this, 'add_scripts' ) ); | |
if( ! empty( $this->widget_class ) ) | |
add_action( 'widgets_init', array( $this, 'register_widget' ) ); | |
} | |
public function register_widget() { | |
register_widget( $this->widget_class ); | |
return true; | |
} | |
public function add_styles() { | |
$styles = ( is_admin() ) ? | |
$this->admin_styles : $this->front_styles; | |
if( empty( $styles ) ) | |
return false; | |
foreach( $styles as $style ) { | |
$style = wp_parse_args( $style, $this->style_defaults ); | |
wp_enqueue_style( | |
$style['handle'], | |
$style['src'], | |
$style['deps'], | |
$style['version'], | |
$style['media'] | |
); | |
} | |
return true; | |
} | |
public function add_scripts() { | |
$scripts = ( is_admin() ) ? | |
$this->admin_scripts : $this->front_scripts; | |
if( empty( $scripts ) ) | |
return false; | |
foreach( $scripts as $script ) { | |
$script = wp_parse_args( $script, $this->script_defaults ); | |
wp_enqueue_script( | |
$script['handle'], | |
$script['src'], | |
$script['deps'], | |
$script['version'], | |
$script['media'] | |
); | |
} | |
return true; | |
} | |
} |
class MyAwesomeWidget extends WP_Widget { | |
const TEXTDOMAIN = 'widget_textdomain'; | |
public function __construct() { | |
parent::__construct( | |
'widget-name-id', | |
__( 'Widget Name', self::TEXTDOMAIN ), | |
array( | |
'classname' => __CLASS__, | |
'description' => __( 'Short description.', self::TEXTDOMAIN ) | |
) | |
); | |
$admin_style = array( | |
array( 'handle' => 'somehandle', 'src' => 'path/to/source' ), | |
array( 'handle' => 'someotherhandle', 'src' => 'path/to/source' ), | |
); | |
$admin_scripts = array( | |
array( 'handle' => 'scrpthandle', 'src' => 'path/to/source', 'deps' => array( 'jquery') ), | |
); | |
$front_styles = array( | |
array( 'handle' => 'frontstyle', 'src' => 'path/to/src' ), | |
); | |
new Widget_Setup( __CLASS__, $admin_style, $admin_scripts, $front_styles ); | |
} | |
public function widget( $instance, $args ) {} | |
public function update( $new_instance, $old_instance ) {} | |
public function form( $instance ) {} | |
} |
I’m not super technical, but this looks fantastic. I recently used WordPress for a site about the Sonic Drive-In menu, keeping it simple with a clean template. If you're curious, visit site. Nice work on this widget
Great work—this script is incredibly useful for converting raster to vector polygons with GDAL. It’s interesting how similar geospatial processing and data validation workflows are to systems we use in other fields. For example, in digital ID card status checking, we also rely on precise boundary logic and attribute extraction—just like polygonizing raster data, but applied to structured personal data. Whether it's spatial boundaries or identity verification, clarity and accuracy in data handling are critical.
Thanks for this helpful gist! While working on similar projects, I often have to manage my time between coding and quick commutes. If anyone’s in Istanbul, checking halkalı marmaray durakları really helps streamline travel plans and avoid delays.
Nice example of using OOP to set up widgets in WordPress! This approach helps keep things modular and organized, especially when managing both admin and frontend assets. If you're working on more advanced projects or looking for services to support your development workflow, you might find APDM Pro helpful for its robust solutions