需要标签的项目的Wordpress小部件更新实例 [英] Wordpress widget update instances for items that need tags
问题描述
最近我意识到在开发主题侧边栏时我没有充分利用WordPress中的小部件,因此我花了最后几天研究如何正确开发它们。在回顾了很多教程之后,我发现自定义建筑小部件上的许多教程已经过时了。我确实看到了应该在哪里使用构造函数:
Recently I realized I do not utilize widgets in WordPress enough when developing sidebars in themes so I have spent the last couple of days researching how to develop them properly. After reviewing a lot of tutorials I have found a number of them on custom building widgets to be outdated. I did see where I should use the construct:
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 很小。浏览了SO的标签后,wordpress-widget 和 widget 致电时我没有看到解决方案如果文本区域需要标签,则对小部件进行更新。大量的人显示称呼标题实例为:
The codex is very minimal when it comes to custom widgets. After browsing SO's tags wordpress-widget and widget I didn't see a solution when calling an update for the widget if a textarea is needing tags. A large number of people show calling the title instance as:
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
在我的函数中 form()
需要一个需要用户输入代码的文本区域,例如复制的Google Adsense广告。以下工作有效,但我不确定是否有更好的方法来接受来自表单的输入:
In my function form()
I am needing a textarea that will take user input code, like a copied Google Adsense ad. The following works but I am unsure if there is a better approach to accepting input from the form:
// 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;
}
}
是否有更好的方法返回 $ instance
不需要使用PHP的 strip_tags()
吗?
Is there a better way to return the $instance
when you need tags without using PHP's strip_tags()
?
推荐答案
是否有更好的方法在需要标记时返回
$ instance
而不使用PHP的strip_tags()
?
是的! WordPress有许多验证和清除用户数据的方法。
Yes! WordPress has many ways to validate and sanitize user data.
对于您的情况 wp_kses_post()
应该很好用。
For your case wp_kses_post()
should work very well.
它的描述是:为发布内容的允许HTML标签清理内容。
它使用默认的允许标签列表,您可以在 wp-includes / kses.php
。
It uses a default list of allowed tags, which you can find in wp-includes/kses.php
.
如果只希望允许特定标签,最安全的方法是使用 wp_kses()
而不是 wp_kses_post()
。
If you wish to only allow specific tags the safest way to do that is by using wp_kses()
instead of wp_kses_post()
.
区别在于 wp_kses()
需要 $ allowed_html
的第二个参数,而不是使用 $ allowedposttags
全局变量。
The difference being that wp_kses()
requires a second parameter for $allowed_html
instead of using the $allowedposttags
global variable.
有一些方法可以过滤或覆盖 $ allowedposttags
全局变量,但在这种情况下可能不是最好的主意,因为它们会影响除您的小部件之外的其他HTML,例如帖子内容。
There are ways to filter or override the $allowedposttags
global variable but are probably not the best idea in this case since they will effect other HTML, like post content, in addition to your widget.
您可以使用 wp_kses_post()
看起来像这样:
You update function with wp_kses_post()
would look like this:
// 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小部件更新实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!