Schedule cron event from widget

wp_schedule_event takes a hook as parameter, not a function. Try: wp_schedule_event(time(), ‘daily’, ‘my_daily_event’); add_action(‘my_daily_event’, array(&$this, ‘my_widget_cron’)); if ( !wp_next_scheduled( ‘my_daily_event’ ) ) { wp_schedule_event(time(), ‘hourly’, ‘my_daily_event’) } If you remove the widget from the sidebar, the cron will still continue to run. You can run the following code (outside of the widget class) to clear … Read more

Creating your own widgets: using cache?

take a look at WordPress Transients API which offers a simple and standardized way of storing cached data in the database temporarily by giving it a custom name and a timeframe after which it will expire and be deleted. The transients API is very similar to the Options API but with the added feature of … Read more

How to limit wp_get_archives to show months for the X years only

If using “regular” query in theme file (sidebar.php for example), this code will help do the trick: <?php wp_get_archives(‘type=monthly&limit=12’); ?> The above (and below) will only show the last 12 months. However if using a widget, then add this code to your functions.php file: function my_limit_archives( $args ) { $args[‘limit’] = 12; return $args; } … Read more

Image picker widget

Try this code. class Image_Picker extends WP_Widget { function Image_Picker() { $widget_ops = array(‘classname’ => ‘Image_Picker’, ‘description’ => ‘Pick 2 images from media library’); $this->WP_Widget(‘Image_Picker’, ‘Image Picker’, $widget_ops); } function form($instance) { //WIDGET BACK-END SETTINGS $instance = wp_parse_args((array) $instance, array(‘link1’ => ”, ‘link2’ => ”)); $link1 = $instance[‘link1’]; $link2 = $instance[‘link2’]; $images = new WP_Query( … Read more

Setting up widgets in wordpress with a unique ID for the after_title argument

Looking at widget.php in trac sprintf() only runs on before_widget (Line 884): $params[0][‘before_widget’] = sprintf($params[0][‘before_widget’], $id, $classname_); Hence, it won’t work for any of the other arguments. I wondered whether you could use the dynamic_sidebar_params filter, but the $id and $classname_ arguments aren’t passed on to the filter. Outside of hacking core, I’m not sure … Read more

How do I register a stylesheet inside a WordPress widget?

wp_enqueue_scripts is called way before WordPress processes the widget content, so just as in this post, you’ve missed the boat :). Instead, just call wp_enqueue_style directly: function widget($args, $instance) { wp_enqueue_style( ‘myprefix-style’, plugins_url(‘mystyle.css’, __FILE__) ); //Widget content } (no need to register if you’re just going to enqueue it straight after). Same works for wp_enqueue_script.

How to insert widget areas specific to certain pages (or posts, etc.)?

Use Conditional Tags to show content only if a certain condition is met. In your case, you’re probably looking to use is_front_page(). <aside> <ul> <?php if ( function_exists( ‘dynamic_sidebar’ ) ) { if ( is_front_page() ) { if ( ! dynamic_sidebar( ‘frontpage-widget-area’ ) ) { echo ‘<li>No sidebars for the frontpage.</li>’; // some default output … Read more