Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to add a replica of the default WP text widget, with my own css class parameter, to functions.php so that it appears in the widgets manager and can be added to a sidebar.

My first attempt is below, but I'm certain there has to be an easier way than how I'm doing it.

Can this be done in a simpler manner?

In functions.php, I've got this...

$google_search = TEMPLATEPATH . "/google_search.php";require_once($google_search);
add_action('widgets_init', create_function('', "register_widget('My_Widget_Search');"));

In google_search.php, I've got... (Everything works except the textarea field contents aren't being saved)

<?php

class My_Widget_Search extends WP_Widget {

    function My_Widget_Search() {
        $widget_ops = array( 'classname' => 'widget_search', 'description' => __( "Google Adsense Search Widget Placeholder" ) );
        $this->WP_Widget('adsense_search', __('Adsense Search Widget'), $widget_ops);
    }

    function widget( $args, $instance ) {
        extract( $args );
        $title = apply_filters('widget_title', empty( $instance['title'] ) ? __( '' ) : $instance['title']);
        $text = apply_filters('widget_text', empty( $instance['text'] ) ? __( '' ) : $instance['text']);

        }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['text'] = $new_instance['text'];

        return $instance;
    }

    function form( $instance ) {
        //Defaults
        $instance = wp_parse_args( (array) $instance, array( 'title' => '') );
        $title = esc_attr( $instance['title'] );
        $text = $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 $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>
<?php
    }

}
share|improve this question

1 Answer 1

up vote 1 down vote accepted

One idea would be to go into the widgets file from the core files, COPY the text widget code into your functions.php file, and edit your version to see fit. Just an idea, hopefully it'd solve some problems.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.