How to Create a custom widget for wordpress sidebar
How to create a custom widget for your theme or custom plugin , just copy and paste below code and paste it into your theme's functions.php or your custom plugin file
class showtable_widget extends WP_Widget {
function __construct() {
global $wpdb;
parent::__construct(
'showtable_widget', // custom widget slug put slug whatever you like
__('Show xml feed table', 'showtable_widget'), // widget title display in admin side widget area
array( 'description' => __( 'Display feed table in sidebar ', 'showtable_widget' ), ) // description for
); // your widget purpose
}
public function widget( $args, $instance ) { // this function use for display data frontend side
global $wpdb;
$title = apply_filters( 'widget_title', $instance['title'] );
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
// custom code started for display in frontside
echo __( 'Hello, World! Have a great day', 'showtable_widget' );
// custom code end for display in frontside
echo $args['after_widget'];
}
public function form( $instance ) { // this form display in admin side for change title
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'Show Table', 'showtable_widget' );
}
if(isset($instance['noofrow']))
{
$noofrow = $instance['noofrow'];
}else
{
$noofrow = 5;
}
?>
<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><p>
<label for="<?php echo $this->get_field_id( 'noofrow' ); ?>"><?php _e( 'No of Rows:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'noofrow' ); ?>" name="<?php echo $this->get_field_name( 'noofrow' ); ?>" type="text" value="<?php echo esc_attr( $noofrow ); ?>" />
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['noofrow'] = ( ! empty( $new_instance['noofrow'] ) ) ? strip_tags( $new_instance['noofrow'] ) : '';
return $instance;
}
}
function rj_load_widget() { // load widget
register_widget( 'showtable_widget' );
}
add_action( 'widgets_init', 'rj_load_widget' ); // display widget action
class showtable_widget extends WP_Widget {
function __construct() {
global $wpdb;
parent::__construct(
'showtable_widget', // custom widget slug put slug whatever you like
__('Show xml feed table', 'showtable_widget'), // widget title display in admin side widget area
array( 'description' => __( 'Display feed table in sidebar ', 'showtable_widget' ), ) // description for
); // your widget purpose
}
public function widget( $args, $instance ) { // this function use for display data frontend side
global $wpdb;
$title = apply_filters( 'widget_title', $instance['title'] );
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
// custom code started for display in frontside
echo __( 'Hello, World! Have a great day', 'showtable_widget' );
// custom code end for display in frontside
echo $args['after_widget'];
}
public function form( $instance ) { // this form display in admin side for change title
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'Show Table', 'showtable_widget' );
}
if(isset($instance['noofrow']))
{
$noofrow = $instance['noofrow'];
}else
{
$noofrow = 5;
}
?>
<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><p>
<label for="<?php echo $this->get_field_id( 'noofrow' ); ?>"><?php _e( 'No of Rows:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'noofrow' ); ?>" name="<?php echo $this->get_field_name( 'noofrow' ); ?>" type="text" value="<?php echo esc_attr( $noofrow ); ?>" />
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['noofrow'] = ( ! empty( $new_instance['noofrow'] ) ) ? strip_tags( $new_instance['noofrow'] ) : '';
return $instance;
}
}
function rj_load_widget() { // load widget
register_widget( 'showtable_widget' );
}
add_action( 'widgets_init', 'rj_load_widget' ); // display widget action
Comments
Post a Comment