admin管理员组文章数量:1292618
Following a tutorial and it works fine. I just can't understand how update method saves $instance array in the database from Select tag? You can choose default or full, slick save in the admin area and it saves in the db.
public function widget( $args, $instance ) {
echo $args['before_widget'];
echo '<div class="g-ytsubscribe"
data-layout="'.$instance['data_layout'].'"
data-count="default">
</div>';
echo $args['after_widget'];
}
public function form( $instance ) {
$data_layout = ! empty( $instance['data_layout'] ) ? $instance['data_layout'] : esc_html__( 'default', 'yts_domain' );
?>
<select class="widefat"
<option value="default" <?php echo ($data_layout == 'default' ? 'selected' : ''); ?>>Default</option>
<option value="full" <?php echo ($data_layout == 'full' ? 'selected' : ''); ?>>Full</option>
</select>
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['data_layout'] = ( ! empty( $new_instance['data_layout'] ) ) ? sanitize_text_field( $new_instance['data_layout'] ) : '';
return $instance;
}
Following a tutorial and it works fine. I just can't understand how update method saves $instance array in the database from Select tag? You can choose default or full, slick save in the admin area and it saves in the db.
public function widget( $args, $instance ) {
echo $args['before_widget'];
echo '<div class="g-ytsubscribe"
data-layout="'.$instance['data_layout'].'"
data-count="default">
</div>';
echo $args['after_widget'];
}
public function form( $instance ) {
$data_layout = ! empty( $instance['data_layout'] ) ? $instance['data_layout'] : esc_html__( 'default', 'yts_domain' );
?>
<select class="widefat"
<option value="default" <?php echo ($data_layout == 'default' ? 'selected' : ''); ?>>Default</option>
<option value="full" <?php echo ($data_layout == 'full' ? 'selected' : ''); ?>>Full</option>
</select>
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['data_layout'] = ( ! empty( $new_instance['data_layout'] ) ) ? sanitize_text_field( $new_instance['data_layout'] ) : '';
return $instance;
}
Share
Improve this question
edited May 12, 2021 at 18:08
Marina Dunst
asked May 11, 2021 at 20:14
Marina DunstMarina Dunst
1595 bronze badges
1
- So my guess is that it looks at the form and collects values using their Ids. For example I have an input field and select field. It looks at whatever values are present at the time when you click Save, updates $instance array and calls update with new and old data. – Marina Dunst Commented May 12, 2021 at 18:38
1 Answer
Reset to default 0When you create a widget you're extending the WP_Widget
class:
class Foo_Widget extends WP_Widget {
The actual saving of the values in the database is handled by the parent class, in the update_callback()
method.
So all your widget needs to do is tell it the new values, which you do by defining the update()
method.
本文标签: plugin developmentHow does update method in Widget class saves instance array from Select tag
版权声明:本文标题:plugin development - How does update method in Widget class saves $instance array from Select tag? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741558264a2385291.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论