/    Sign up×
Community /Pin to ProfileBookmark

Simple PHP script – what have I missed?

Trying to build a simple WordPress widget plugin, I am stuck on a simple select box:

[code=php]<?php
class wp_my_plugin extends WP_Widget {

// constructor
function wp_my_plugin() {
parent::WP_Widget(false, $name = __(‘Select Test’, ‘wp_my_plugin’) );
}

// widget form creation
function form($instance) {

// Check values
if( $instance) {
$select = esc_attr($instance[‘select’]);
} else {
$select = ”;
}
?>

<p>
<label for=”<?php echo $this->get_field_id(‘select’); ?>”><?php _e(‘Select’, ‘wp_widget_plugin’); ?></label>
<select name=”<?php echo $this->get_field_name(‘select’); ?>” id=”<?php echo $this->get_field_id(‘select’); ?>” class=”widefat”>
<?php
$options = array(‘lorem’, ‘ipsum’, ‘dolorem’);
foreach ($options as $option) {
echo ‘<option value=”‘ . $option . ‘” id=”‘ . $option . ‘”‘, $select == $option ? ‘ selected=”selected”‘ : ”, ‘>’, $option, ‘</option>’;
}
?>
</select>
</p>

<?php
}

// update widget
function update($new_instance, $old_instance) {
$instance = $old_instance;
// Fields
$instance[‘select’] = strip_tags($new_instance[‘select’]);
return $instance;
}

// display widget
function widget($args, $instance) {
extract( $args );
// these are the widget options
$select = $select[‘select’];

echo $before_widget;
// Display the widget
echo ‘<div class=”widget-text wp_widget_plugin_box”>’;

// Get $select value
if ( $select == ‘lorem’ ) {
echo ‘Lorem option is Selected’;
} else if ( $select == ‘ipsum’ ) {
echo ‘ipsum option is Selected’;
} else {
echo ‘dolorem option is Selected’;
}

echo ‘</div>’;
echo $after_widget;
}
}
// register widget
add_action(‘widgets_init’, create_function(”, ‘return register_widget(“wp_my_plugin”);’));

?>[/code]

Can anyone see what I have missed? $select is showing blank…

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @jjumpm2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...