是的,你做得对。任何文本小部件都可以使用 javascript - 如果你想摆脱 div 和 p 标签和类 - 你的代码应该足够了。
您还可以使用一些过滤器来清洁其他东西。
实际上,我有这样一个小部件的代码 - 我不记得我是从哪个网站获取的(一些片段存储库),但它基本上是复制了一些小改动的文本小部件。
<?php
/*
Plugin Name: Text widget for Javascript
Plugin URI: Unknown
Description: Adds a Text widget with Javascript support - basically empty all divs classes etc.
Version: 3.1
Author: Unknown (code source unknown. compiled by Obmerk99)
Author URI: Unknown
License: GPL2
Network: true
*/
/**********************************************
* Text widget class with small twicks for JS *
**********************************************/
class WP_Widget_Text_For_JS extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'widget_text', 'description' => __('Javascript Text Widget'));
$control_ops = array('width' => 400, 'height' => 350);
parent::__construct('text', __('Text'), $widget_ops, $control_ops);
}
function widget( $args, $instance ) {
extract($args);
$title = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);
$text = apply_filters( 'widget_text', $instance['text'], $instance );
$before_widget = ''; $after_widget =''; $before_title=''; $after_title='';$after_widget=''; //empty all
echo $before_widget;
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
<div class="textwidget"><?php echo $instance['filter'] ? wpautop($text) : $text; ?></div>
<?php
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['text'] = $new_instance['text'];
$instance['filter'] = isset($new_instance['filter']);
return $instance;
}
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
$title = strip_tags($instance['title']);
$text = esc_textarea($instance['text']);
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<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); ?>" /></p>
<textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea>
<p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox" <?php checked(isset($instance['filter']) ? $instance['filter'] : 0); ?> /> <label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
<?php
}
}
function wp_o99_javascript_text_widget(){
// unregister_widget('WP_Widget_Text'); // Only if you really want to .
register_widget('WP_Widget_Text_For_JS');
}
add_action('widgets_init', 'wp_o99_javascript_text_widget', 1);
?>