最近,我意识到在开发主题中的侧边栏时,我没有充分利用 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' ), )
);
}
The codex https://codex.wordpress.org/Widgets_API当涉及到自定义小部件时,这是非常小的。浏览完SO的标签后wordpress 小部件 /questions/tagged/wordpress-widget and widget /questions/tagged/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;
}
}
有没有更好的方法返回$instance
当您需要标签而不使用 PHP 时strip_tags()
?