Sunday, 25 August 2013

Conditional widget_form_callback

Conditional widget_form_callback

I have made a widget_form_callback with my own custom function. I have
added 2 fields:
Additional classes (id "classes" in a text field) Column Span (id
"colspan" in a select field)
I want the first to be added to all widgets, but only want the second to
be added as an option to the dynamic sidebar with id "footer". How do I go
about this? This is the code I have so far:
function my_widget_form_extend( $instance, $widget ) {
if ( !isset($instance['classes']) )
$instance['classes'] = null;
$row = "<p>\n";
$row .= "\t<label
for='widget-{$widget->id_base}-{$widget->number}-classes'>Additional
Classes <small>(separate with spaces)</small></label>\n";
$row .= "\t<input type='text'
name='widget-{$widget->id_base}[{$widget->number}][classes]'
id='widget-{$widget->id_base}-{$widget->number}-classes'
class='widefat' value='{$instance['classes']}'/>\n";
$row .= "</p>\n";
echo $row;
if ( !isset($instance['colspan']) )
$instance['colspan'] = 3;
echo "<p>\n";
echo "\t<label
for='widget-{$widget->id_base}-{$widget->number}-classes'>Column
Span:</label> \n";
?>
<select name="widget-<?php echo
$widget->id_base.'['.$widget->number.'][colspan]'; ?>"
id="widget-<?php echo $widget->id_base.'-'.$widget->number;
?>-colspan">
<option<?php if ($instance['colspan']=='1') {echo ' selected';} ?>
value="1">1</option>
<option<?php if ($instance['colspan']=='2') {echo ' selected';} ?>
value="2">2</option>
<option<?php if ($instance['colspan']=='3') {echo ' selected';} ?>
value="3">3</option>
<option<?php if ($instance['colspan']=='4') {echo ' selected';} ?>
value="4">4</option>
<option<?php if ($instance['colspan']=='5') {echo ' selected';} ?>
value="5">5</option>
<option<?php if ($instance['colspan']=='6') {echo ' selected';} ?>
value="6">6</option>
<option<?php if ($instance['colspan']=='7') {echo ' selected';} ?>
value="7">7</option>
<option<?php if ($instance['colspan']=='8') {echo ' selected';} ?>
value="8">8</option>
<option<?php if ($instance['colspan']=='9') {echo ' selected';} ?>
value="9">9</option>
<option<?php if ($instance['colspan']=='10') {echo ' selected';} ?>
value="10">10</option>
<option<?php if ($instance['colspan']=='11') {echo ' selected';} ?>
value="11">11</option>
<option<?php if ($instance['colspan']=='12') {echo ' selected';} ?>
value="12">12</option>
</select>
<?php
echo "</p>\n";
return $instance;
}
add_filter('widget_form_callback', 'my_widget_form_extend', 10, 2);

No comments:

Post a Comment