这并不能回答你所有的问题,但当我学习这些小部件时,我很难找到一个好的、可靠的样本来学习。这是我用来参考的一个enigma
可以这么说;)
//CONSTRUCT Class
class my_custom_widget extends WP_Widget {
function my_custom_widget() {
$widget_ops = array(\'classname\' => \'text_plus_widget\', \'description\' => \'Title Bar, Text Area, and Link URL\' );
//WIDGET Name
$this->WP_Widget(\'hs_text_plus\', \'Text Plus Widget\', $widget_ops);
}
//WIDGET Args
function widget($args, $instance) {
extract($args, EXTR_SKIP);
echo $before_widget;
//WIDGET Database Checks
$title = empty($instance[\'title\']) ? \' \' : apply_filters(\'widget_hs_text_plus_title\', $instance[\'title\']);
$text_area = empty($instance[\'text_area\']) ? \' \' : apply_filters(\'widget_hs_text_plus_text_area\', $instance[\'text_area\']);
$link_url = empty($instance[\'link_url\']) ? \' \' : apply_filters(\'widget_hs_text_plus_link_url\', $instance[\'link_url\']);
?>
<li>
<?php if ( !empty( $title ) && (strlen($title) > 1 )) { echo $before_title . $title . $after_title; }; ?>
<?php if ( !empty( $text_area ) && (strlen($text_area) > 1 )) { echo \'<p>\' . $text_area . \'</p>\'; }; ?>
<?php if ( !empty( $link_url ) && (strlen($link_url) > 1 )) { echo \'<a href="\' . $link_url . \'">Click Here to Learn More</a>\'; }; ?>
<div class="clr" style="display: block; height: 20px;"></div>
</li>
<?php
echo $after_widget;
}
//WIDGET Save
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance[\'title\'] = strip_tags($new_instance[\'title\']);
$instance[\'text_area\'] = strip_tags($new_instance[\'text_area\']);
$instance[\'link_url\'] = strip_tags($new_instance[\'link_url\']);
return $instance;
}
//WIDGET Admin Form
function form($instance) {
//set your params, $instance and then any additional variables needed below
$instance = wp_parse_args( (array) $instance, array( \'title\' => \'\', \'text_area\' => \'\', \'link_url\' => \'\' ) );
$title = strip_tags($instance[\'title\']);
$text_area = strip_tags($instance[\'text_area\']);
$link_url = strip_tags($instance[\'link_url\']);
?>
<p><label for="<?php echo $this->get_field_id(\'title\'); ?>">Widget Title: <input class="widefat" id="<?php echo $this->get_field_id(\'title\'); ?>" name="<?php echo $this->get_field_name(\'title\'); ?>" type="text" value="<?php echo esc_attr__($title); ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id(\'text_area\'); ?>">Widget Text: <textarea class="widefat" id="<?php echo $this->get_field_id(\'text_area\'); ?>" name="<?php echo $this->get_field_name(\'text_area\'); ?>" ><?php echo esc_attr__($text_area); ?></textarea></label></p>
<p><label for="<?php echo $this->get_field_id(\'link_url\'); ?>">Link URL: <input class="widefat" id="<?php echo $this->get_field_id(\'link_url\'); ?>" name="<?php echo $this->get_field_name(\'link_url\'); ?>" type="text" value="<?php echo esc_attr__($link_url); ?>" /></label></p>
<?php
}
}
//CREATE Widget
add_action( \'widgets_init\', create_function(\'\', \'return register_widget("my_custom_widget");\') );
这不是PHP5。3语法,但如果需要,它很容易移植到该语法。干杯