<?php
/*
Plugin Name: my_css_styler
Description: Плагин, подключающий CSS файл находящийся по пути "папка_плагина_/my_css_styler.css"
Author: Александр Соболев
Author URI: http://александрсоболев.рф
*/
/* Подключение CSS */
function my_css_styler() {
wp_enqueue_style( 'my_css__styler', plugin_dir_url( __FILE__ ). '/my_css_styler.css', false );
}
add_action( 'admin_head', 'my_css_styler' ); // Для админки
add_action( 'wp_head', 'my_css_styler' ); // Для внешней части
?>
<?php
/*
Plugin Name: My Custom Widgets (mcw_)
Plugin URI: http://александрсоболлев.рф
Description: Плагн создающий кастомные виджеты популярных записей и рубрик с разделителем <HR>.
Author: Александр Соболев
Author URI: https://александрсоболев.рф
*/
class mcw_TopPostsWidget extends WP_Widget {
function __construct() {
parent::__construct(
'mcw__top_widget',
'MCW Популярные посты', // заголовок виджета
array( 'description' => 'Позволяет вывести посты, отсортированные по количеству комментариев в них с разделителем <HR>.' ) // описание
);
}
// фронтэнд виджета MCW Популярные посты
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] ); // к заголовку применяем фильтр (необязательно)
$posts_per_page = $instance['posts_per_page'];
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
$q = new WP_Query("posts_per_page=$posts_per_page&orderby=comment_count");
if( $q->have_posts() ):
while( $q->have_posts() ): $q->the_post();
?><a href="<?php the_permalink() ?>"><?php the_title() ?></a><hr><?php
endwhile;
endif;
wp_reset_postdata();
echo $args['after_widget'];
}
// бэкэнд виджета MCW Популярные посты
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
if ( isset( $instance[ 'posts_per_page' ] ) ) {
$posts_per_page = $instance[ 'posts_per_page' ];
}
?>
<p>
<label for="<?php echo $this->get_field_id( '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( 'posts_per_page' ); ?>">Количество постов:</label>
<input id="<?php echo $this->get_field_id( 'posts_per_page' ); ?>" name="<?php echo $this->get_field_name( 'posts_per_page' ); ?>" type="text" value="<?php echo ($posts_per_page) ? esc_attr( $posts_per_page ) : '5'; ?>" size="3" />
</p>
<?php
}
// сохранение настроек виджета MCW Популярные посты
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['posts_per_page'] = ( is_numeric( $new_instance['posts_per_page'] ) ) ? $new_instance['posts_per_page'] : '5'; // по умолчанию выводятся 5 постов
return $instance;
}
}
// регистрация виджета MCW Популярные посты
function mcw__top_posts_widget_load() {
register_widget( 'mcw_TopPostsWidget' );
}
add_action( 'widgets_init', 'mcw__top_posts_widget_load' );
// Создание виджета MCW Рубрики
class mcw_CategoryWidget extends WP_Widget {
function __construct() {
parent::__construct(
'mcw__category_widget',
'MCW Рубрики', // заголовок виджета
array( 'description' => 'Виджет выводит рубрики с разделителем <HR>.' ) // описание
);
}
// фронтэнд виджета MCW Рубрики
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] ); // к заголовку применяем фильтр (необязательно)
$posts_per_page = $instance['posts_per_page'];
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
mcw_list_categories();
echo $args['after_widget'];
}
// бэкэнд виджета MCW Рубрики
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
?>
<p>
<label for="<?php echo $this->get_field_id( '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
}
// сохранение настроек виджета MCW Рубрики
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
}
// регистрация виджета MCW Рубрики
function mcw__category_widget_load() {
register_widget( 'mcw_CategoryWidget' );
}
add_action( 'widgets_init', 'mcw__category_widget_load' );
/* -------------------------------------------------------------------- */
// Создание виджета MCW Свежие записи
class mcw_FreshPostWidget extends WP_Widget {
function __construct() {
parent::__construct(
'mcw__fresh_posts_widget',
'MCW Свежие записи', // заголовок виджета
array( 'description' => 'Виджет выводит свежие записи с разделителем <HR>.' ) // описание
);
}
// фронтэнд виджета MCW Свежие записи
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] ); // к заголовку применяем фильтр (необязательно)
$posts_per_page = $instance['posts_per_page'];
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
last_post();
echo $args['after_widget'];
}
// бэкэнд виджета MCW Свежие записи
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
?>
<p>
<label for="<?php echo $this->get_field_id( '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
}
// сохранение настроек виджета Свежие записи
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
}
// регистрация виджета MCW Свежие записи
function mcw__fresh_post_widget_load() {
register_widget( 'mcw_FreshPostWidget' );
}
add_action( 'widgets_init', 'mcw__fresh_post_widget_load' );
/* Функции бэкэнда */
function mcw_list_categories() {
$categories = get_categories();
if( $categories ){
foreach( $categories as $cat ){
$category_link = get_category_link( $cat->cat_ID );
echo '<a href="'.$category_link.'">'.$cat->name.'</a><hr>';
}
}
}
function last_post(){
$args = array(
'post_status' => 'publish',
);
$result = wp_get_recent_posts($args);
foreach( $result as $p ){ echo'<a href="'.get_permalink($p['ID']).'">'.$p['post_title'].'</a><hr>'; }
}