יצירת ווידג'ט וורדפרס
טוב אז ככה, כדי ליצור וידגט צריך להרחיב את הקלאס WP_Widget שנמצא בספריה wp-includes/widgets.php.
את ההרחבה רצוי לעשות בעזרת פלאגין מדריך ליצירת פלאגין אפשר לראות כאן
קוד בסיסי ליצירת וידגט.
יש לשנות שם את השם ואת התיאור של הוידגט למה שאתם בוחרים תחת ה __construct וכן את שם הקלאס My_Widget למשהו אחר.
class My_Widget extends WP_Widget {
/**
* Sets up the widgets name etc
*/
public function __construct() {
$widget_ops = array(
'class_name' => 'my_widget',
'description' => 'My Widget is awesome',
);
parent::__construct( 'my_widget', 'My Widget', $widget_ops );
}
/**
* Outputs the content of the widget
*
* @param array $args
* @param array $instance
*/
public function widget( $args, $instance ) {
// outputs the content of the widget
}
/**
* Outputs the options form on admin
*
* @param array $instance The widget options
*/
public function form( $instance ) {
// outputs the options form on admin
}
/**
* Processing widget options on save
*
* @param array $new_instance The new options
* @param array $old_instance The previous options
*/
public function update( $new_instance, $old_instance ) {
// processes widget options to be saved
}
}
בתוך הפונקציה widget קובעים מה התוכן שאנחנו נדפיס למסך התצוגה של האתר.
תחת הפונקציה form אפשר לקבל נתונים לוידגט מאיזור הוידגטים, זה בדרך כלל שדה ה input או כל שדה אחר שנמצא באיזור הוידגטים.
בתוך הפונקציה update אחנו מעדכנים את הנתונים של הוידגט.
עכשיו צריך להשתמש בהוק widgets_init כדי לטעון את הוידגט.
למי שאינו יודע מזה הוק אני אסביר בקצרה. למערכת וורדפרס יש הוקים שתפקידם בעצם לטעון קודים כאשר קורים דברים מסויימים במערכת ישנם הוקים שהם חלק מהמערכת כמו ההוק הנ"ל, נראה דוגמא של ההוק שדרכו נעשה רגיסטר לוידגט שלנו. הוא פועל כאשר אנחנו נמצאים באיזור הוידגטים.
add_action( 'widgets_init', function(){
register_widget( 'My_Widget' );
});
השדה הראשון אומר מתי אנחנו רוצים להפעיל את ההוק.
השדה השני אומר איזה פונקציה אנחנו רוצים להפעיל.
פה אנחנו מפעילים את הפונקציה register_widget שעושה רגיסטר לוידגט שרשום בתוכה במקרה שלנו My_Widget או כל שם אחר שתבחרו.
דוגמא לוידגט בתוך פלאגין שמוסיף כותרת (לא לשכוח להפעיל את הפלאגין)
<?php
/*
Plugin Name: jos test
Plugin URI: http://support.netjos.com
Description: This is test plugin.
Author: josef arazi
Version: 1
Author URI: http://support.netjos.com
*/
/**
* Adds Foo_Widget widget.
*/
class Foo_Widget extends WP_Widget {
/**
* Register widget with WordPress.
*/
function __construct() {
parent::__construct(
'foo_widget', // Base ID
__( 'Widget Title', 'text_domain' ), // Name
array( 'description' => __( 'A Foo Widget', 'text_domain' ), ) // Args
);
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title'];
}
echo __( 'Hello, World!', 'text_domain' );
echo $args['after_widget'];
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'New title', 'text_domain' );
?>
<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 esc_attr( $title ); ?>">
</p>
<?php
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
} // class Foo_Widget
// register Foo_Widget widget
add_action( 'widgets_init', function(){ register_widget( 'Foo_Widget' ); } );
?>
אנחנו משתמשים בפונקציות
get_field_name()
get_field_id()
כדי לקבל את ה id וה name בפורום.