Skip to content

Instantly share code, notes, and snippets.

@mlbd
Created February 28, 2018 11:12
Show Gist options
  • Save mlbd/c7be047d0cb427ae6c3b39abed39beaf to your computer and use it in GitHub Desktop.
Save mlbd/c7be047d0cb427ae6c3b39abed39beaf to your computer and use it in GitHub Desktop.
# PHP 5.3+ anonymous function
add_action( 'widgets_init', function() {
register_widget( 'Sample_Widget_SO_19246434' );
});
class Sample_Widget_SO_19246434 extends WP_Widget
{
function __construct() {
parent::__construct(
'hottopics',
esc_html__( 'Hot Topics', 'seoleader' ),
array(
'name' => 'Hot Topics',
'classname' => 'widget-hot-topics',
'description' => __( "Just Recommended" )
)
);
// Register seoleader Widget
add_action( 'admin_enqueue_scripts', array( $this, 'seoleader_scripts' ));
}
public function postsoption($type, $group){
$post_exists = post_type_exists( $type );
$whdf = array(82,80,1178,1177);
if ( $post_exists ) {
$get_posts = get_posts( array(
'post_type' => $type,
'posts_status' =>'publish',
'posts_per_page'=> -1
));
echo '<optgroup label="' . esc_html__( $group, 'seoleader' ) . '">';
foreach( $get_posts as $post )
{
printf(
'<option value="%1$s" %2$s>%3$s</option>',
$post->ID,
in_array( $post->ID, $whdf) ? 'selected="selected"' : '',
$post->post_title
);
}
echo '</optgroup>';
}
}
public function form( $instance )
{
$select = isset( $instance['select'] ) ? $instance['select'] : '';
$limon = $select;
$post_exists = post_type_exists( 'post' );
$casestudy_exists = post_type_exists( 'case-study' );
$service_exists = post_type_exists( 'service' );
echo '<p>';
echo '<pre>';
print_r($limon);
echo '</pre>';
if( ($post_exists) || ($casestudy_exists) || ($service_exists) )
{
printf(
'<select multiple="multiple" name="%s[]" id="%s" class="widefat seoleader_select2">',
$this->get_field_name('select'),
$this->get_field_id('select')
);
$post_exists = post_type_exists( 'post' );
$whdf = array(82,80,1178,1177);
if ( $post_exists ) {
$get_posts = get_posts( array(
'post_type' => 'post',
'posts_status' =>'publish',
'posts_per_page'=> -1
));
echo '<optgroup label="' . esc_html__( 'Post', 'seoleader' ) . '">';
foreach( $get_posts as $post )
{
printf(
'<option value="%1$s" %2$s>%3$s</option>',
$post->ID,
in_array( $post->ID, $limon) ? 'selected="selected"' : '',
$post->post_title
);
}
echo '</optgroup>';
}
echo '</select>';
}
echo '</p>'; ?>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('.seoleader_select2').select2({
width: '100%'
});
});
</script>
<?php
}
public function seoleader_scripts(){
wp_enqueue_script(
'seoleader_recommended_select',
get_template_directory_uri() . '/assets/js/select2.min.js',
array('jquery'),
'',
true
);
wp_enqueue_style(
'seoleader_recommended_select2',
get_template_directory_uri() . '/assets/css/select2.min.css'
);
}
function update( $new_instance, $old_instance )
{
$instance = $old_instance;
$instance['select'] = esc_sql( $new_instance['select'] );
return $instance;
}
function widget( $args, $instance )
{
echo 'Hello world';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment