ဒီ 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
}

}

About wailynnoo

wailynn oo has written 57 post in this Website..

I am a blogger.