Skip to content

Instantly share code, notes, and snippets.

@masumskaib396
Last active August 18, 2019 06:32
Show Gist options
  • Save masumskaib396/172c551a95a53c7b9665f79e860dfd9b to your computer and use it in GitHub Desktop.
Save masumskaib396/172c551a95a53c7b9665f79e860dfd9b to your computer and use it in GitHub Desktop.
Author Porfile Widgets
=====================
php
====
<?php
class prefix_Author_Widget extends WP_Widget
{
public function __construct()
{
parent::__construct(
'author-widget',
__( 'TM: Author Widget', 'web-log' ),
array( 'description' => __( 'Best displayed in Sidebar.', 'web-log' ) )
);
}
public function widget( $args, $instance )
{
extract( $args );
if(!empty($instance))
{
$facebook =$instance['facebook'];
$twitter =$instance['twitter'];
$instagram =$instance['instagram'];
$linkedin =$instance['linkedin'];
$youtube =$instance['youtube'];
$image =$instance['image_uri'];
$title = apply_filters( 'widget_title', !empty( $instance['title'] ) ? $instance['title'] : '', $instance, $this->id_base );
if( !empty($image))
{
?>
<div class="widget">
<?php echo $args['before_title'] . $title . $args['after_title']; ?>
<div class="shape2"></div>
<div class="about-me text-center">
<img src="<?php echo esc_url( $instance['image_uri'] );?>" alt="about me">
<p><?php echo wp_kses_post( $instance['description'] );?></p>
<ul class="social-icons list-inline">
<?php
if ( !empty( $facebook ) ) { ?>
<li>
<a class="img-circle" href="<?php echo esc_url( $facebook ); ?>" data-title="Facebook" target="_blank"><i class="fa fa-facebook"></i></a>
</li>
<?php }
if ( !empty( $twitter ) ) { ?>
<li>
<a class="img-circle" href="<?php echo esc_url( $twitter ); ?>" data-title="Twitter" target="_blank"><i class="fa fa-twitter"></i></a>
</li>
<?php }
if ( !empty( $linkedin ) ) {
?>
<li>
<a class="img-circle" href="<?php echo esc_url( $linkedin ); ?>" data-title="Linkedin" target="_blank"><i class="fa fa-linkedin"></i></a>
</li>
<?php
}
if ( !empty( $instagram) ) {
?>
<li>
<a class="img-circle" href="<?php echo esc_url( $instagram); ?>" data-title="Instagram" target="_blank"><i class="fa fa-instagram"></i></a>
</li>
<?php
}
if ( !empty( $youtube ) ) { ?>
<li>
<a class="img-circle" href="<?php echo esc_url( $youtube ); ?>" data-title="Youtube" target="_blank"><i class="fa fa-youtube"></i></a>
</li>
<?php
}
?>
</ul>
</div><!-- .about-me -->
</div><!-- .widget -->
<?php
}
}
}
public function update( $new_instance, $old_instance ){
$instance = $old_instance;
$instance['title'] = sanitize_text_field( $new_instance['title'] );
$instance['description'] = wp_kses_post( $new_instance['description'] );
$instance['image_uri'] = esc_url_raw( $new_instance['image_uri'] );
$instance['facebook'] = esc_url_raw( $new_instance['facebook'] );
$instance['twitter'] = esc_url_raw( $new_instance['twitter'] );
$instance['googleplus'] = esc_url_raw( $new_instance['googleplus'] );
$instance['instagram'] = esc_url_raw( $new_instance['instagram'] );
$instance['linkedin'] = esc_url_raw( $new_instance['linkedin'] );
$instance['youtube'] = esc_url_raw( $new_instance['youtube'] );
return $instance;
}
public function form($instance ){
?>
<p>
<label for="<?php echo $this->get_field_id('image_uri'); ?>">
<?php _e( 'Image', 'web-log' ); ?>
</label>
<br />
<?php
if (isset($instance['image_uri']) && $instance['image_uri'] != '' ) :
echo '<img class="custom_media_image" src="' . esc_url( $instance['image_uri'] ) . '" style="margin:0;padding:0;max-width:100px;float:left;display:inline-block" /><br />';
endif;
?>
<input type="text" class="widefat custom_media_url" name="<?php echo $this->get_field_name('image_uri'); ?>" id="<?php echo $this->get_field_id('image_uri'); ?>" value="<?php
if (isset($instance['image_uri']) && $instance['image_uri'] != '' ) :
echo esc_url( $instance['image_uri'] );
endif;
?>" style="margin-top:5px;">
<input type="button" id="custom_media_button" value="<?php esc_attr_e( 'Upload Image', 'web-log' ); ?>" class="button media-image-upload" data-title="<?php esc_attr_e( 'Select Image','web-log'); ?>" data-button="<?php esc_attr_e( 'Select Image','web-log'); ?>"/>
<input type="button" id="remove_media_button" value="<?php esc_attr_e( 'Remove Image', 'web-log' ); ?>" class="button media-image-remove" />
</p>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title', 'web-log' ); ?></label><br />
<input type="text" name="<?php echo $this->get_field_name('title'); ?>" id="<?php echo $this->get_field_id('title'); ?>" value="<?php
if (isset($instance['title']) && $instance['title'] != '' ) :
echo esc_attr($instance['title']);
endif;
?>" class="widefat" />
</p>
<p>
<label for="<?php echo $this->get_field_id('description'); ?>"><?php _e( 'Description', 'web-log' ); ?></label><br />
<textarea rows="8" name="<?php echo $this->get_field_name('description'); ?>" id="<?php echo $this->get_field_id('description'); ?>" class="widefat" ><?php
if (isset($instance['description']) && $instance['description'] != '' ) :
echo esc_textarea( $instance['description'] );
endif;
?></textarea>
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id('facebook') ); ?>"><?php _e( 'Facebook', 'web-log' ); ?></label><br />
<input type="text" name="<?php echo esc_attr( $this->get_field_name('facebook') ); ?>" id="<?php echo esc_attr( $this->get_field_id('facebook')); ?>" value="<?php
if (isset($instance['facebook']) && $instance['facebook'] != '' ) :
echo esc_attr($instance['facebook']);
endif;
?>" class="widefat" />
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id('twitter') ); ?>"><?php _e( 'Twitter', 'web-log' ); ?></label><br />
<input type="text" name="<?php echo esc_attr( $this->get_field_name('twitter') ); ?>" id="<?php echo esc_attr( $this->get_field_id('twitter')); ?>" value="<?php
if (isset($instance['twitter']) && $instance['twitter'] != '' ) :
echo esc_attr($instance['twitter']);
endif;
?>" class="widefat" />
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id('instagram') ); ?>"><?php _e( 'Instagram', 'web-log' ); ?></label><br />
<input type="text" name="<?php echo esc_attr( $this->get_field_name('instagram') ); ?>" id="<?php echo esc_attr( $this->get_field_id('instagram')); ?>" value="<?php
if (isset($instance['instagram']) && $instance['instagram'] != '' ) :
echo esc_attr($instance['instagram']);
endif;
?>" class="widefat" />
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id('linkedin') ); ?>"><?php _e( 'Linkedin', 'web-log' ); ?></label><br />
<input type="text" name="<?php echo esc_attr( $this->get_field_name('linkedin') ); ?>" id="<?php echo esc_attr( $this->get_field_id('linkedin')); ?>" value="<?php
if (isset($instance['linkedin']) && $instance['linkedin'] != '' ) :
echo esc_attr($instance['linkedin']);
endif;
?>" class="widefat" />
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id('youtube') ); ?>"><?php _e( 'Youtube', 'web-log' ); ?></label><br />
<input type="text" name="<?php echo esc_attr( $this->get_field_name('youtube') ); ?>" id="<?php echo esc_attr( $this->get_field_id('youtube')); ?>" value="<?php
if (isset($instance['youtube']) && $instance['youtube'] != '' ) :
echo esc_attr($instance['youtube']);
endif;
?>" class="widefat" />
</p>
<?php
}
}
add_action( 'widgets_init', 'prefix_author_widget' );
function prefix_author_widget(){
register_widget( 'prefix_Author_Widget' );
}
==========
javascript code
============
var at_document = $(document);
at_document.on('click','.media-image-upload', function(e){
// Prevents the default action from occuring.
e.preventDefault();
var media_image_upload = $(this);
var media_title = $(this).data('title');
var media_button = $(this).data('button');
var media_input_val = $(this).prev();
var media_image_url_value = $(this).prev().prev().children('img');
var media_image_url = $(this).siblings('.img-preview-wrap');
var meta_image_frame = wp.media.frames.meta_image_frame = wp.media({
title: media_title,
button: { text: media_button },
library: { type: 'image' }
});
// Opens the media library frame.
meta_image_frame.open();
// Runs when an image is selected.
meta_image_frame.on('select', function(){
// Grabs the attachment selection and creates a JSON representation of the model.
var media_attachment = meta_image_frame.state().get('selection').first().toJSON();
// Sends the attachment URL to our custom image input field.
media_input_val.val(media_attachment.url);
if( media_image_url_value !== null ){
media_image_url_value.attr( 'src', media_attachment.url );
media_image_url.show();
LATESTVALUE(media_image_upload.closest("p"));
}
});
});
// Runs when the image button is clicked.
jQuery('body').on('click','.media-image-remove', function(e){
$(this).siblings('.img-preview-wrap').hide();
$(this).prev().prev().val('');
});
var LATESTVALUE = function (wrapObject) {
wrapObject.find('[name]').each(function(){
$(this).trigger('change');
});
};
======
Call hooks and only script call desgbord
======
if (!function_exists('prifix_widgets_backend_enqueue')) :
function prifix_widgets_backend_enqueue($hook)
{
if ('widgets.php' != $hook)
{
return;
}
wp_register_script('prifix-custom-widgets', get_template_directory_uri() . '/assets/js/widget.js', array('jquery'), true);
wp_enqueue_media();
wp_enqueue_script('prifix-custom-widgets');
}
add_action('admin_enqueue_scripts', 'prifix_widgets_backend_enqueue');
endif;
====================================== End Author Widget =========================================
====================================== Start Sidebar post widget =========================================
<?php
/**
* Displays latest or category wised posts list.
*
*/
class prefix_Sidebar_Posts extends WP_Widget {
/* Register Widget with WordPress*/
function __construct() {
parent::__construct(
'sidebar_posts', // Base ID
esc_html__( 'prefix: Sidebar Posts', 'prefix' ), // Name
array( 'description' => esc_html__( 'Displays latest posts or posts from a choosen category.Use this widget in the main sidebars.', 'prefix' ), ) // Args
);
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
$defaults = array(
'title' => esc_html__( 'Latest Posts', 'prefix' ),
'category' => 'all',
'number_posts' => 5,
'sticky_posts' => true,
);
$instance = wp_parse_args( (array) $instance, $defaults );
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'prefix' ); ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>"/>
</p>
<p>
<label><?php esc_html_e( 'Select a post category', 'prefix' ); ?></label>
<?php wp_dropdown_categories( array( 'name' => $this->get_field_name('category'), 'selected' => $instance['category'], 'show_option_all' => 'Show all posts' ) ); ?>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'number_posts' ); ?>"><?php esc_html_e( 'Number of posts:', 'prefix' ); ?></label>
<input type="number" id="<?php echo $this->get_field_id( 'number_posts' ); ?>" name="<?php echo $this->get_field_name( 'number_posts' );?>" value="<?php echo absint( $instance['number_posts'] ) ?>" size="3"/>
</p>
<p>
<input type="checkbox" <?php checked( $instance['sticky_posts'], true ) ?> class="checkbox" id="<?php echo $this->get_field_id('sticky_posts'); ?>" name="<?php echo $this->get_field_name('sticky_posts'); ?>" />
<label for="<?php echo $this->get_field_id('sticky_posts'); ?>"><?php esc_html_e( 'Ignore sticky posts.', 'prefix' ); ?></label>
</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 = $old_instance;
$instance[ 'title' ] = sanitize_text_field( $new_instance[ 'title' ] );
$instance[ 'category' ] = absint( $new_instance[ 'category' ] );
$instance[ 'number_posts' ] = (int)$new_instance[ 'number_posts' ];
$instance[ 'sticky_posts' ] = (bool)$new_instance[ 'sticky_posts' ];
return $instance;
}
/**
* 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 ) {
extract($args);
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : '';
$title = apply_filters( 'widget_title', $title , $instance, $this->id_base );
$category = ( ! empty( $instance['category'] ) ) ? absint( $instance['category'] ) : 0;
$number_posts = ( ! empty( $instance['number_posts'] ) ) ? absint( $instance['number_posts'] ) : 5;
$sticky_posts = ( isset( $instance['sticky_posts'] ) ) ? $instance['sticky_posts'] : false;
// Latest Posts
$latest_posts = new WP_Query(
array(
'cat' => $category,
'posts_per_page' => $number_posts,
'ignore_sticky_posts' => $sticky_posts
)
);
echo $before_widget; ?>
<div class="prefix-category-posts">
<?php
if ( $title ) {
echo $before_title . $title . $after_title;
}
?>
<?php if( $latest_posts -> have_posts() ) : ?>
<?php while ( $latest_posts -> have_posts() ) : $latest_posts -> the_post(); ?>
<div class="bms-post clearfix">
<?php if ( has_post_thumbnail() ) { ?>
<div class="bms-thumb">
<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail( 'prefix-small' ); ?>
</a>
</div>
<?php } ?>
<div class="bms-details">
<?php the_title( sprintf( '<h3 class="bms-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h3>' ); ?>
<div class="entry-meta"><?php prefix_posted_on(); ?></div>
</div>
</div><!-- .bms-post -->
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div><!-- .prefix-category-posts -->
<?php
echo $after_widget;
}
}
// Register single category posts widget
function prefix_register_sidebar_posts() {
register_widget( 'prefix_Sidebar_Posts' );
}
add_action( 'widgets_init', 'prefix_register_sidebar_posts' );
====================================== End Sidebar post widget =========================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment