需要标签的项目的 Wordpress 小部件更新实例
Wordpress widget update instances for items that need tags
最近我意识到在开发主题侧边栏时我没有充分利用 WordPress 中的小部件,所以我花了最近几天研究如何正确开发它们。在查看了大量教程后,我发现其中许多关于自定义构建小部件的教程已经过时。我确实看到我应该在哪里使用构造:
function __construct() {
parent::__construct(
// Base ID of your widget
'foobar_widget',
// Widget name will appear in UI
__('Give them foo Widget', 'foobar_widget_domain'),
// Widget description
array( 'description' => __( 'Development widget for testing', 'foobar_widget_domain' ), )
);
}
codex is very minimal when it comes to custom widgets. After browsing SO's tags wordpress-widget and widget 如果文本区域需要标签,我在为小部件调用更新时没有看到解决方案。大量人显示调用标题实例为:
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
在我的函数 form()
中,我需要一个文本区域来接收用户输入代码,例如复制的 Google Adsense 广告。以下有效,但我不确定是否有更好的方法来接受来自表单的输入:
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['foo1'] = $new_instance['foo1'];
return $instance;
}
}
当您需要标签而不使用 PHP 的 strip_tags()
时,是否有更好的方法 return $instance
?
我使用以下很棒的代码片段来注册一个自定义小部件,它接受任何 HTML
add_action('widgets_init', create_function('', 'register_widget("clean_markup_widget");'));
class Clean_Markup_Widget extends WP_Widget {
function __construct() {
parent::WP_Widget('clean_markup_widget', 'ASR Custom Text Widget', array('description'=>'Simple widget for well-formatted markup & text'));
}
function widget($args, $instance) {
extract($args);
$markup = $instance['markup'];
//echo $before_widget;
if ($markup) echo $markup;
//echo $after_widget;
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['markup'] = $new_instance['markup'];
return $instance;
}
function form($instance) {
if ($instance) $markup = esc_attr($instance['markup']);
else $markup = __('<p>Clean, well-formatted markup.</p>', 'markup_widget'); ?>
<p>
<label for="<?php echo $this->get_field_id('markup'); ?>"><?php _e('Markup/text'); ?></label><br />
<textarea class="widefat" id="<?php echo $this->get_field_id('markup'); ?>" name="<?php echo $this->get_field_name('markup'); ?>" type="text" rows="16" cols="20" value="<?php echo $markup; ?>"><?php echo $markup; ?></textarea>
</p>
<?php }
}
Is there a better way to return the $instance
when you need tags without using PHP's strip_tags()
?
是的! WordPress 有很多方法来验证和清理用户数据。
对于你的情况 wp_kses_post()
应该工作得很好。
它的描述是:为 post 内容允许的 HTML 标签清理内容。
它使用默认的允许标签列表,您可以在 wp-includes/kses.php
中找到它。
如果您希望只允许特定标签,最安全的方法是使用 wp_kses()
而不是 wp_kses_post()
。
区别在于 wp_kses()
需要 $allowed_html
的第二个参数,而不是使用 $allowedposttags
全局变量。
有一些方法可以过滤或覆盖 $allowedposttags
全局变量,但在这种情况下可能不是最好的主意,因为它们会影响其他 HTML,例如 post 内容,添加到您的小部件。
您使用 wp_kses_post()
更新函数将如下所示:
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['foo1'] = wp_kses_post( $new_instance['foo1'] );
return $instance;
}
最近我意识到在开发主题侧边栏时我没有充分利用 WordPress 中的小部件,所以我花了最近几天研究如何正确开发它们。在查看了大量教程后,我发现其中许多关于自定义构建小部件的教程已经过时。我确实看到我应该在哪里使用构造:
function __construct() {
parent::__construct(
// Base ID of your widget
'foobar_widget',
// Widget name will appear in UI
__('Give them foo Widget', 'foobar_widget_domain'),
// Widget description
array( 'description' => __( 'Development widget for testing', 'foobar_widget_domain' ), )
);
}
codex is very minimal when it comes to custom widgets. After browsing SO's tags wordpress-widget and widget 如果文本区域需要标签,我在为小部件调用更新时没有看到解决方案。大量人显示调用标题实例为:
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
在我的函数 form()
中,我需要一个文本区域来接收用户输入代码,例如复制的 Google Adsense 广告。以下有效,但我不确定是否有更好的方法来接受来自表单的输入:
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['foo1'] = $new_instance['foo1'];
return $instance;
}
}
当您需要标签而不使用 PHP 的 strip_tags()
时,是否有更好的方法 return $instance
?
我使用以下很棒的代码片段来注册一个自定义小部件,它接受任何 HTML
add_action('widgets_init', create_function('', 'register_widget("clean_markup_widget");'));
class Clean_Markup_Widget extends WP_Widget {
function __construct() {
parent::WP_Widget('clean_markup_widget', 'ASR Custom Text Widget', array('description'=>'Simple widget for well-formatted markup & text'));
}
function widget($args, $instance) {
extract($args);
$markup = $instance['markup'];
//echo $before_widget;
if ($markup) echo $markup;
//echo $after_widget;
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['markup'] = $new_instance['markup'];
return $instance;
}
function form($instance) {
if ($instance) $markup = esc_attr($instance['markup']);
else $markup = __('<p>Clean, well-formatted markup.</p>', 'markup_widget'); ?>
<p>
<label for="<?php echo $this->get_field_id('markup'); ?>"><?php _e('Markup/text'); ?></label><br />
<textarea class="widefat" id="<?php echo $this->get_field_id('markup'); ?>" name="<?php echo $this->get_field_name('markup'); ?>" type="text" rows="16" cols="20" value="<?php echo $markup; ?>"><?php echo $markup; ?></textarea>
</p>
<?php }
}
Is there a better way to return the
$instance
when you need tags without using PHP'sstrip_tags()
?
是的! WordPress 有很多方法来验证和清理用户数据。
对于你的情况 wp_kses_post()
应该工作得很好。
它的描述是:为 post 内容允许的 HTML 标签清理内容。
它使用默认的允许标签列表,您可以在 wp-includes/kses.php
中找到它。
如果您希望只允许特定标签,最安全的方法是使用 wp_kses()
而不是 wp_kses_post()
。
区别在于 wp_kses()
需要 $allowed_html
的第二个参数,而不是使用 $allowedposttags
全局变量。
有一些方法可以过滤或覆盖 $allowedposttags
全局变量,但在这种情况下可能不是最好的主意,因为它们会影响其他 HTML,例如 post 内容,添加到您的小部件。
您使用 wp_kses_post()
更新函数将如下所示:
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['foo1'] = wp_kses_post( $new_instance['foo1'] );
return $instance;
}