WordPress 2.8 အတွက် widget ဘယ်လိုရေးမလဲ?

wailynnooJune 5, 20104min780

ဒီ tutorial မှာ widget လေးတစ်ခုဖန်းတီးဖို့ ဘယ်လိုလေ့လာရမယ် ဆိုတာကိုဖော်ပြပေးပါမယ်။ Widget တစ်ခုကို အစအဆုံးရေးပြမှာ မဟုတ်ပါဘူး။ php ရဲ့ oop သဘောတရားတွေကိုတော့နားလည်ထားရပါမယ်။ ကျွန်တော်တို့ widget တစ်ခုကိုဖန်တီးနိုင်ဖို့အတွက် wordpress ရဲ့ မူရင်း class ကိုinherited လုပ်ရပါမယ်။ အဲဒီ class ကတော့ wp-includes/widget.php ဖိုင်မှာပါတဲ့ WP_Widget ဆိုတဲ့ class ဖြစ်ပါတယ်။ လက်ရှိပါလာတဲ့ widget တွေကိုတော့ wp-includes/default-widgets.php မှာရေးထားတာပါ။ ကျွန်တော်တို့ အဲဒီဖိုင်ကို အဓိကလေ့လာရပါမယ်။ ကျွန်တော်တို့ဖန်တီးထားတဲ့ widget ရဲ့ code တွေကိုတော့ ကျွန်တော်တို့အသုံးပြုတဲ့ theme ရဲ့ functions.php ဆိုတဲ့ ဖိုင်ထဲမှာရေးရမှာပါ။ မူရင်း wordpress ဖိုင်ကိုတော့ hack လုပ်ပြီးမထည့်သင့်ပါ။
widget ရဲ့ structure ကဒီလိုပါ။

class My_Widget extends WP_Widget {
function My_Widget() {
//Constructor
}

function widget($args, $instance) {
// prints the widget
}

function update($new_instance, $old_instance) {
//save the widget
}

function form($instance) {
//widgetform in backend
}
}
register_widget(‘My_Widget’);

အဲဒီ class မှာ function 4 ခုပါတာတွေ့မှာပါ။ My_Widget() ကတော့ constructor ပါ။ widget ဆိုတဲ့ function ကတော့ front end မှာပေါ်ဖို့အပိုင်းကို ရေးရမှာပါ။ update function ကတော့ widget ကိုယူသုံးလိုက်တာနဲ့သူရဲ့ data တွေကိုသတ်မှတ်ပေးရမှာပါ။ form function ကတော့ back end မှာပြမဲ့ form ပါ။ အဲဒီ function ကတော့ optional ပါ။ အားလုံးရေးပြီးရင်တော့ register လုပ်ပေးရပါမယ်။ register_widget(‘My_Widget’); ဒီ function လေးကိုထည့်ပေးရပါမယ်။ ဒီအောက်က code တွေကတော့ default-widgets.php မှာရေးထားတဲ့ sidebar မှာ page ဆိုတဲ့ widget ကိုပေါ်စေတဲ့ code ပါ။ အဲဒီကနေ သဘောတရားလေ့လာပြီး ကိုယ်ပိုင် widget လေးတစ်ခုရေးကြည့်ပါ။

<?php

class WP_Widget_Pages extends WP_Widget {

function WP_Widget_Pages() {
$widget_ops = array(‘classname’ => ‘widget_pages’, ‘description’ => __( ‘Your blog’s WordPress Pages’) );
$this->WP_Widget(‘pages’, __(‘Pages’), $widget_ops);
}

function widget( $args, $instance ) {
extract( $args );

$title = apply_filters(‘widget_title’, empty( $instance[‘title’] ) ? __( ‘Pages’ ) : $instance[‘title’]);
$sortby = empty( $instance[‘sortby’] ) ? ‘menu_order’ : $instance[‘sortby’];
$exclude = empty( $instance[‘exclude’] ) ? ” : $instance[‘exclude’];

if ( $sortby == ‘menu_order’ )
$sortby = ‘menu_order, post_title’;

$out = wp_list_pages( apply_filters(‘widget_pages_args’, array(‘title_li’ => ”, ‘echo’ => 0, ‘sort_column’ => $sortby, ‘exclude’ => $exclude) ) );

if ( !empty( $out ) ) {
echo $before_widget;
if ( $title)
echo $before_title . $title . $after_title;
?>
<ul>
<?php echo $out; ?>
</ul>
<?php
echo $after_widget;
}
}

function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[‘title’] = strip_tags($new_instance[‘title’]);
if ( in_array( $new_instance[‘sortby’], array( ‘post_title’, ‘menu_order’, ‘ID’ ) ) ) {
$instance[‘sortby’] = $new_instance[‘sortby’];
} else {
$instance[‘sortby’] = ‘menu_order’;
}

$instance[‘exclude’] = strip_tags( $new_instance[‘exclude’] );

return $instance;
}

function form( $instance ) {
//Defaults
$instance = wp_parse_args( (array) $instance, array( ‘sortby’ => ‘post_title’, ‘title’ => ”, ‘exclude’ => ”) );
$title = esc_attr( $instance[‘title’] );
$exclude = esc_attr( $instance[‘exclude’] );
?>
<p><label for=”<?php echo $this->get_field_id(‘title’); ?>”><?php _e(‘Title:’); ?></label> <input id=”<?php echo $this->get_field_id(‘title’); ?>” name=”<?php echo $this->get_field_name(‘title’); ?>” type=”text” value=”<?php echo $title; ?>” /></p>
<p>
<label for=”<?php echo $this->get_field_id(‘sortby’); ?>”><?php _e( ‘Sort by:’ ); ?></label>
<select name=”<?php echo $this->get_field_name(‘sortby’); ?>” id=”<?php echo $this->get_field_id(‘sortby’); ?>”>
<option value=”post_title”<?php selected( $instance[‘sortby’], ‘post_title’ ); ?>><?php _e(‘Page title’); ?></option>
<option value=”menu_order”<?php selected( $instance[‘sortby’], ‘menu_order’ ); ?>><?php _e(‘Page order’); ?></option>
<option value=”ID”<?php selected( $instance[‘sortby’], ‘ID’ ); ?>><?php _e( ‘Page ID’ ); ?></option>
</select>
</p>
<p>
<label for=”<?php echo $this->get_field_id(‘exclude’); ?>”><?php _e( ‘Exclude:’ ); ?></label> <input type=”text” value=”<?php echo $exclude; ?>” name=”<?php echo $this->get_field_name(‘exclude’); ?>” id=”<?php echo $this->get_field_id(‘exclude’); ?>” />
<br />
<small><?php _e( ‘Page IDs, separated by commas.’ ); ?></small>
</p>
<?php
}

}