The following fields are optional. You can insert text and/or HTML code before or after the entire ad group and each individual ad.
Datafeedr Random Ads Message: ' . $msg . '';
}
return '';
}
// Display the ads from a template function
function dfrads($group_id = false) {
if (!$group_id) {
return dfrads_display_admin_error('A group ID is required.');
}
$dfrads = get_option('dfrads');
if (!is_array($dfrads[$group_id])) {
return dfrads_display_admin_error('The ad group "' . $group_id . '" does not exist.');
}
$ads = explode('[DFRADS]', $dfrads[$group_id]['ads']);
$num_ads = count($ads);
$ad_id = mt_rand(1, $num_ads);
$ad = $ads[($ad_id - 1)];
return $dfrads[$group_id]['before_ad'] . $ad . $dfrads[$group_id]['after_ad'];
}
/**
* Add function to widgets_init that'll load our widget.
* @since 0.1
*/
add_action('widgets_init', 'example_load_widgets');
/**
* Register our widget.
* 'DfrAds_Widget' is the widget class used below.
*
* @since 0.1
*/
function example_load_widgets() {
register_widget('DfrAds_Widget');
}
/**
* Example Widget class.
* This class handles everything that needs to be handled with the widget:
* the settings, form, display, and update. Nice!
*
* @since 0.1
*/
class DfrAds_Widget extends WP_Widget {
/**
* Widget setup.
*/
function __construct() {
/* Widget settings. */
$widget_ops = array('classname' => 'dfrads', 'description' => __('Display your rotating ads in the sidebar.', 'dfrads'));
/* Widget control settings. */
$control_ops = array('id_base' => 'dfrads-widget');
/* Create the widget. */
parent::__construct('dfrads-widget', __('Datafeedr Random Ads', 'dfrads'), $widget_ops, $control_ops);
}
/**
* How to display the widget on the screen.
*/
function widget($args, $instance) {
extract($args);
/* Our variables from the widget settings. */
$title = apply_filters('widget_title', $instance['title']);
$group_id = $instance['group_id'];
/* Before widget (defined by themes). */
echo $before_widget;
/* Display the widget title if one was input (before and after defined by themes). */
if ($title) {
echo $before_title . $title . $after_title;
}
/* Display name from widget settings if one was input. */
echo dfrads($group_id);
/* After widget (defined by themes). */
echo $after_widget;
}
/**
* Update the widget settings.
*/
function update($new_instance, $old_instance) {
$instance = $old_instance;
/* Strip tags for title and name to remove HTML (important for text inputs). */
$instance['title'] = strip_tags($new_instance['title']);
/* No need to strip tags for sex and show_sex. */
$instance['group_id'] = $new_instance['group_id'];
return $instance;
}
/**
* Displays the widget settings controls on the widget panel.
* Make use of the get_field_id() and get_field_name() function
* when creating your form elements. This handles the confusing stuff.
*/
function form($instance) {
/* Set up some default widget settings. */
$defaults = array('title' => __('Our Sponsors', 'dfrads'));
$instance = wp_parse_args((array) $instance, $defaults);
$dfrads = get_option('dfrads');
?>