I copied this pretty much straight from the book and adapted it. It has a simple widget option, but I can't get the setting to save and I've been all through chapter four trying to figure out where I went wrong and I don't see it.
I tried to keep it very simple for my first attempt. The widget works in all ways except it does not have a selected option, the default argument is not passing to the variable, but even when I set it manually with if (!$size)... there is still no option selected. Also after selecting and saving, the options unset on reload.
Does anyone see my error?
Code:
$inm_ccd_file = "catalyst-certified-developer-plugin.php";
register_activation_hook( plugin_dir_path($inm_ccd_file), 'inm_ccd_install' );
function inm_ccd_install() {
if ( version_compare( get_bloginfo( 'version' ), '3.1', '<' ) ) {
deactivate_plugins( basename( plugin_dir_path($inm_ccd_file) ) ); // Deactivate our plugin
}
}
/*
register_deactivation_hook( $inm_ccd_file_path, "inm_ccd_uninstall" );
function inm_ccd_uninstall() {
}
*/
// use widgets_init action hook to execute custom function
add_action( 'widgets_init', 'inm_ccd_image_display_widget_register_widgets' );
//register our widget
function inm_ccd_image_display_widget_register_widgets() {
register_widget( 'inm_ccd_image_display_widget' );
}
// include 'includes/inm-ccd-widget.php';
//inm_ccd_image_display_widget class
class inm_ccd_image_display_widget extends WP_Widget {
//process the new widget
function inm_ccd_image_display_widget() {
$widget_ops = array(
'classname' => 'inm_ccd_image_display_widget_class',
'description' => 'Display a Catalyst Certified Developer badge.'
);
$this->WP_Widget( 'inm_ccd_image_display_widget', 'Catalyst | Cert. Dev. Badge', $widget_ops );
}
//build the widget settings form
function form($instance) {
$image_url = " [path to my images] ";
$defaults = array(
'size' => 'catalyst-certified-70.png'
);
$instance = wp_parse_args( (array) $instance, $defaults );
$size = $instance['size'];
if(!$size) $size = "catalyst-certified-30.png";
?>
<div align="center" style="text-align: left;"><img width="20%" height="20%" src="<?php echo $image_url . $size; ?>" /><br /><br />
<input type="radio" name="size" value="<?php echo $image_url; ?>catalyst-certified-30.png" <?php selected($size, 'catalyst-certified-30.png') ?> > Small (136 x 67)<br />
<input type="radio" name="size" value="<?php echo $image_url; ?>catalyst-certified-70.png" <?php selected($size, 'catalyst-certified-70.png') ?> > Large (316 x 155)<br />
</div>
<?php
}
//save the widget settings
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['size'] = strip_tags($new_instance['size']);
return $instance;
}
//display the widget
function widget($args, $instance) {
$inm_ccd_pre_image = '<img src="';
$inm_ccd_post_image = '" />';
extract($args);
echo $before_widget;
$size = empty( $instance['size'] ) ? ' [path to my images] ' : $instance['size'];
echo $inm_ccd_pre_image.$size.$inm_ccd_post_image;
echo $after_widget;
}
}