Come posso aggiungere un widget "Testo" personalizzato al gestore Aspetto da functions.php?

19 gen 2011, 19:38:15
Visualizzazioni: 2.31K
Voti: 1

Sto cercando di aggiungere una replica del widget testo predefinito di WP, con il mio parametro di classe CSS personalizzata, a functions.php in modo che appaia nel gestore dei widget e possa essere aggiunto a una sidebar.

Il mio primo tentativo è mostrato sotto, ma sono certo che ci deve essere un modo più semplice di come lo sto facendo.

Si può fare in maniera più semplice?

In functions.php, ho questo...

$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, ho... (Tutto funziona tranne che il contenuto del campo textarea non viene salvato)

<?php

class My_Widget_Search extends WP_Widget {

    function My_Widget_Search() {
        $widget_ops = array( 
            'classname' => 'widget_search', 
            'description' => __( "Widget placeholder per la ricerca Google Adsense" ) 
        );
        $this->WP_Widget('adsense_search', __('Widget Adsense Search'), $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']);

        // Qui verrebbe il codice per visualizzare il widget
    }

    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 ) {
        // Valori predefiniti
        $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( 'Titolo:' ); ?></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
    }

}
0
Tutte le risposte alla domanda 1
0

Un'idea sarebbe quella di andare nel file dei widget dai file core, COPIARE il codice del widget di testo nel tuo file functions.php e modificare la tua versione come preferisci. Solo un'idea, speriamo che possa risolvere alcuni problemi.

20 gen 2011 01:50:09