You can simply put your init code within the constructor of the class. For example:
class myWidget extends WP_Widget{
function myWidget(){
// Init code here
}
function widget( $args, $instance ) {
// The widget code
wp_enqueue_script(...);
wp_enqueue_style(...);
}
// Over methods...
}
register_widget('myWidget');
My preference is to actually put the enqueue calls within the shortcode handling function to avoid the overhead and potential conflicts associated with loading JavaScript and stylesheets that aren’t being used on a given page.
Related Posts:
- Has anyone managed to integrate the wp_editor inside a widget?
- Applying OO patterns and principles to plugin development
- Dynamically creating classes for Widget API
- How do I create Widget within plugin that uses its own class?
- Plugin Form Submission Best Practice
- PHP error with shortcode handler from a class
- Custom Widget function in Plugin not working?
- Get page content using slug
- Where to store PHP files created by plugin / themes
- Adding Widget form fields dynamically
- Using a plugin class inside a template
- How to create a flexible abstraction for WP_Query?
- Widget development – Drop down options won’t save
- How to make method from plugin available in theme?
- Calling the widget id of a mult-instance widget from inside the widget?
- Is There A Hook To Process The Content Of The Text Widget?
- Using OOP plugin’s methods throughout the website
- Single Responsibility Principle and WP Classes
- How can I make my custom shortcode work in a Custom HTML Widget?
- How Can I Add a Filter to Class Instance Only?
- The ideal place for storing persistent PHP objects
- Add content to widget title in admin area within the widget?
- wp_localize_script $handle
- Plugin Architecture/Design Pattern – is better to use a private Observer/Mediator Pattern for plugin subclasses or WP add_action?
- How to pass JavaScript variable to PHP in wordpress widget?
- Help adding image upload functionality to widget
- Using preg_replace to clean widget output HTML
- Edit the output of wp_widget_rss_output()
- Script to remove all inactive widgets?
- WordPress widget / plugin….fields not appearing in my widget
- Two-step login process – Is it possible?
- Possible to preset a widget’s contents via a plugin script?
- $wp_filesystem returns NULL. What are the dependencies?
- Multiple entries in get_option results? or why is _multiwidget set to 1?
- How to add multiple copies of a widget from “available widgets”
- Install widget on plugin activation
- OOP Plugin Development. Including external object
- How to store accumulate multiple option values in a single array using Options_API
- Using Ajax call in jQuery doesn’t work in widget
- WordPress select dropdown list in widget
- Using a front controller in a WordPress plugin, any suggestions?
- WP_LOCALIZE_SCRIPT doesn’t work
- Use of global variables within plugin [closed]
- Using $wpdb object in a widget
- Admin-ajax.php appending a status code to ajax response
- Is there a need to do apply_filter(‘widget_title’, $instance[‘title’]) or any other ‘widget_xxx’ filters?
- set_sale_price in WooCommerce [closed]
- Dynamic content in a widget
- How to pass custom options from widget form to widget update callback?
- Broken markup when using the_excerpt() in a widget?
- Tips for targeting widget dragable for WP Pointer on widgets.php page
- How to create custom field in worpdress default widgets?
- WordPress Ajax callback function from plugin – OOP
- Show dialog box in TinyMCE plugin and use WordPress php functions in it
- PHP 7 – Class Method Compatibility Issue
- Widget Admin – Form Submit Event?
- How do you render_callback for register_block_type to a method in another class?
- How to save WP widget instances and options
- wp_localize_script and host/browser cache
- Can I individually style items in the backend widget list?
- Sidebar widget to show popular post not working?
- Widget update function not saving values
- How do I alter the comment form ‘allowed tags’ text in a plugin?
- Add custom element ID depending where the widget place is, sidebar or footer
- How to Create database table when Plugin installed/activated, and delete database when Plugin deleted
- Customizer: widget-synced triggers twice
- Registering and using actions which return results in a Plugin class
- Advice on plugin structure
- Creating a shortcode, but getting tied up with OOP and Classes?
- Add a class to a dynamic sidebar’s wrapper
- Create onClick Event to Re-load a Widget
- Widget is to be compatible with all themes
- Extending the WP_Widget_Text class
- How to localize admin.php only once
- Failed to Open Stream: Permission Denied While Writing to an XML File in a WordPress Plugin
- Plugin Architecture Question: How to avoid using transients
- What is the proper method of using global $post?
- How to make my custom widget appear within WordPress widgets? Plugin development
- Adding widgets to my plugin page instead of WordPress dashboard
- Widget Dropdown doesn’t working
- How do I get the sub categories of the parent when in a sub category?
- How to limit number of number of categories displayed by categories widget
- Input in plugin widget does not allow spaces
- Get draggable widgets on Edit Post page
- Widget options are not updated
- Why does my widget break other widgets?
- Widget HTML Display Problem
- wordpress JSONAPI introspector always limits number at 10?
- CSS not affecting widget output
- Using a multiple element in widget form
- WordPress class, using add_action to call member function does not work
- Problem with saving checkbox values with WordPress Widget API
- Update Data parameter of a wp_localize_script() call
- WordPress Specified file failed upload test
- Why is my custom post type not being activated on plug-in activation?
- Remove an action by extending class and replacing it
- Extend Woocommerce Widget Class – WC_Widget_Product_Categories
- Undefined method class::construct() when creating a plugin
- Hook called before text widget save
- Using OOP inside a Plugin – Is it allowed?