Skip to content

Instantly share code, notes, and snippets.

@humayunahmed8
Last active December 7, 2017 09:42
Show Gist options
  • Save humayunahmed8/1ddde5307c0ecdba1db22ba89c5669e0 to your computer and use it in GitHub Desktop.
Save humayunahmed8/1ddde5307c0ecdba1db22ba89c5669e0 to your computer and use it in GitHub Desktop.
Register custom widget and sidebar
<?php
// Register right sidebar
function footer_widget(){
register_sidebar(array(
'name'=>'Footer Sidebar',
'id'=>'footer_widget',
'description'=>'This is our footer widget',
'before_widget'=>'<div class="col-md-4"><div class="widget">',
'after_widget'=>'</div></div>',
'before_title'=>'<h3 class="widget-title">',
'after_title'=>'</h3>',
));
}
add_action('widgets_init', 'footer_widget');
// Register custom widget options
class my_contact_widget extends WP_Widget {
function __construct() {
// Instantiate the parent object
parent::__construct(
'contact_widget',//id
'Contact Widget'//name
);
}
function form( $instance ) {
// Output admin widget options form
// stucture: condition ? positive : nagetive ( here, ? = ternary operator )
$title = ! empty ($instance['title']) ? $instance['title'] : esc_html__( 'Type your title', 'text_domain' );
$icon = ! empty ($instance['icon']) ? $instance['icon'] : esc_html__( 'Address Icon', 'text_domain' );
$address = ! empty ($instance['address']) ? $instance['address'] : esc_html__( 'Type your company address', 'text_domain' );
$another_icon = ! empty ($instance['another_icon']) ? $instance['another_icon'] : esc_html__( 'Another Icon', 'text_domain' );
$mobile = ! empty ($instance['mobile_num']) ? $instance['mobile_num'] : esc_html__( 'Type Contact', 'text_domain' );
$email_icon = ! empty ($instance['email_icon']) ? $instance['email_icon'] : esc_html__( 'Email Icon', 'text_domain' );
$email = ! empty ($instance['email']) ? $instance['email'] : esc_html__( 'Email Address', 'text_domain' );
// esc_html__( 'Type your title', 'text_domain' );
// esc_html__( 'Type your icon name', 'text_domain' );
?>
<p>
<label>Title</label>
<input type="text" name="<?php echo esc_attr( $this->get_field_name('title') ); ?>" value="<?php echo $title; ?>">
</p>
<p>
<label>Address Icon</label>
<input type="text" name="<?php echo esc_attr( $this->get_field_name('icon') ); ?>" value="<?php echo $icon; ?>">
</p>
<p>
<label>Address</label>
<input type="text" name="<?php echo esc_attr( $this->get_field_name('address') ); ?>" value="<?php echo $address; ?>">
</p>
<p>
<label>Mobile Icon</label>
<input type="text" name="<?php echo esc_attr( $this->get_field_name('another_icon') ); ?>" value="<?php echo $another_icon; ?>">
</p>
<p>
<label>Mobile Number</label>
<input type="text" name="<?php echo esc_attr( $this->get_field_name('mobile_num') ); ?>" value="<?php echo $mobile; ?>">
</p>
<p>
<label>Email Icon</label>
<input type="text" name="<?php echo esc_attr( $this->get_field_name('email_icon') ); ?>" value="<?php echo $email_icon; ?>">
</p>
<p>
<label>Email Address</label>
<input type="text" name="<?php echo esc_attr( $this->get_field_name('email') ); ?>" value="<?php echo $email; ?>">
</p>
<?php
}
function update( $new_instance, $old_instance ) {
// Save widget options
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['icon'] = ( ! empty( $new_instance['icon'] ) ) ? strip_tags( $new_instance['icon'] ) : '';
$instance['address'] = ( ! empty( $new_instance['address'] ) ) ? strip_tags( $new_instance['address'] ) : '';
$instance['another_icon'] = ( ! empty( $new_instance['another_icon'] ) ) ? strip_tags( $new_instance['another_icon'] ) : '';
$instance['mobile_num'] = ( ! empty( $new_instance['mobile_num'] ) ) ? strip_tags( $new_instance['mobile_num'] ) : '';
$instance['email_icon'] = ( ! empty( $new_instance['email_icon'] ) ) ? strip_tags( $new_instance['email_icon'] ) : '';
$instance['email'] = ( ! empty( $new_instance['email'] ) ) ? strip_tags( $new_instance['email'] ) : '';
return $instance;
}
function widget( $args, $instance ) {
// Widget output
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) && ($instance['icon']) && ($instance['address']) && ($instance['another_icon']) && ($instance['email_icon']) && ($instance['email']) )
?>
<div class="widget">
<h3 class="widget-title">
<?php echo $instance['title']; ?>
</h3>
<div class="address">
<p>
<span class="icon"><i class="fa fa-<?php echo $instance['icon']; ?>"></i></span>
<span class="txt"><?php echo $instance['address']; ?></span>
</p>
<p>
<span class="icon"><i class="fa fa-<?php echo $instance['another_icon']; ?>"></i></span>
<span class="txt"><?php echo $instance['mobile_num']; ?></span>
</p>
<p>
<span class="icon"><i class="fa fa-<?php echo $instance['email_icon']; ?>"></i></span>
<span class="txt"><?php echo $instance['email']; ?></span>
</p>
</div>
<!-- /.address -->
</div>
<!-- /.widget -->
<?php
echo $args['after_widget'];
}
}
function contact_widget() {
register_widget( 'my_contact_widget' );
}
add_action( 'widgets_init', 'contact_widget' );
Usage Example:
==============
<?php dynamic_sidebar('footer_widget'); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment