Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
615 views
in Technique[技术] by (71.8m points)

wordpress - How can I modify my custom widgets to support qtranslate?

I have a website where I have to modify my widgets to work with the qTranslate plugin. Can someone please point me at the right direction ?

Basically I would like to have my input fields show for every language, so that the user easily knows where to enter the information instead of enteric in the cryptic sting like

[:en]English only[:fr]Francais only

Thanks for the help

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

AFAIK, you'd have to replicate the Widgets that you want with custom translation fields.

The following is an adaptation of the Search Widget, copied from /wp-includes/default-widgets.php.

Note that you can optimize all FIELD_language option values, doing a foreach in all available languages.

add_action( 'widgets_init', 'qtrans_search_widget_so_14303711' );

function qtrans_search_widget_so_14303711()
{
    unregister_widget( 'WP_Widget_Search' );
    register_widget( 'QT_Widget_Search' );
}

class QT_Widget_Search extends WP_Widget {

    function __construct() {
        $widget_ops = array('classname' => 'widget_search', 'description' => __( "A search form for your site") );
        parent::__construct('search', __('Search'), $widget_ops);
    }

    function widget( $args, $instance ) {
        extract($args);
        $title_en = empty( $instance['title_en'] ) ? '' : $instance['title_en'] ;
        $title_fr = empty( $instance['title_fr'] ) ? '' : $instance['title_fr'] ;

        // THIS PART CAN BE GREATLY OPTIMIZED
        $lingo = qtrans_getLanguage();
        if ( 'en' == $lingo )
            $title = $title_en;
        else
            $title = $title_fr;

        echo $before_widget;
        if ( $title )
            echo $before_title . $title . $after_title;

        // Use current theme search form if it exists
        get_search_form();

        echo $after_widget;
    }

    function form( $instance ) {
        $instance = wp_parse_args( (array) $instance, array( 'title_en' => '', 'title_fr' => '') );
        $title_en = $instance['title_en'];
        $title_fr = $instance['title_fr'];
?>
        <p><label for="<?php echo $this->get_field_id('title_en'); ?>"><?php _e('Title English:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title_en'); ?>" name="<?php echo $this->get_field_name('title_en'); ?>" type="text" value="<?php echo esc_attr($title_en); ?>" /></label></p>
    <p><label for="<?php echo $this->get_field_id('title_fr'); ?>"><?php _e('Title Fran?ais:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title_fr'); ?>" name="<?php echo $this->get_field_name('title_fr'); ?>" type="text" value="<?php echo esc_attr($title_fr); ?>" /></label></p>
<?php
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $new_instance = wp_parse_args((array) $new_instance, array( 'title_en' => '', 'title_fr' => ''));
        $instance['title_en'] = strip_tags($new_instance['title_en']);
        $instance['title_fr'] = strip_tags($new_instance['title_fr']);
        return $instance;
    }

}

It renders:

custom search widget for qTranslate


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...