wordpress widget categories edit

Solutions on MaxInterview for wordpress widget categories edit by the best coders in the world

showing results for - "wordpress widget categories edit"
Pietro
15 Oct 2019
1// Register our tweaked Category Archives widget
2function myprefix_widgets_init() {
3    register_widget( 'WP_Widget_Categories_custom' );
4}
5add_action( 'widgets_init', 'myprefix_widgets_init' );
6
7/**
8 * Duplicated and tweaked WP core Categories widget class
9 */
10class WP_Widget_Categories_Custom extends WP_Widget {
11
12    function __construct()
13    {
14        $widget_ops = array( 'classname' => 'widget_categories widget_categories_custom', 'description' => __( "A list of categories, with slightly tweaked output.", 'mytextdomain'  ) );
15        parent::__construct( 'categories_custom', __( 'Categories Custom', 'mytextdomain' ), $widget_ops );
16    }
17
18    function widget( $args, $instance )
19    {
20        extract( $args );
21
22        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Categories Custom', 'mytextdomain'  ) : $instance['title'], $instance, $this->id_base);
23
24        echo $before_widget;
25        if ( $title )
26            echo $before_title . $title . $after_title;
27        ?>
28        <ul>
29            <?php
30            // Get the category list, and tweak the output of the markup.
31            $pattern = '#<li([^>]*)><a([^>]*)>(.*?)<\/a>\s*\(([0-9]*)\)\s*<\/li>#i';  // removed ( and )
32
33            // $replacement = '<li$1><a$2>$3</a><span class="cat-count">$4</span>'; // no link on span
34            // $replacement = '<li$1><a$2>$3</a><span class="cat-count"><a$2>$4</a></span>'; // wrap link in span
35            $replacement = '<li$1><a$2><span class="cat-name">$3</span> <span class="cat-count">($4)</span></a>'; // give cat name and count a span, wrap it all in a link
36
37
38        $args = array(
39                'orderby'       => 'name',
40                'order'         => 'ASC',
41                'show_count'    => 1,
42                'title_li'      => '',
43                'exclude'       => '2,5,31',
44                'echo'          => 0,
45                'depth'         => 1,
46        );
47
48            $subject      = wp_list_categories( $args );
49            echo preg_replace( $pattern, $replacement, $subject );
50            ?>
51        </ul>
52        <?php
53        echo $after_widget;
54    }
55
56    function update( $new_instance, $old_instance )
57    {
58        $instance = $old_instance;
59        $instance['title'] = strip_tags( $new_instance['title'] );
60        $instance['count'] = 1;
61        $instance['hierarchical'] = 0;
62        $instance['dropdown'] = 0;
63
64        return $instance;
65    }
66
67    function form( $instance )
68    {
69        //Defaults
70        $instance = wp_parse_args( (array) $instance, array( 'title' => '') );
71        $title = esc_attr( $instance['title'] );
72        $count = true;
73        $hierarchical = false;
74        $dropdown = false;
75        ?>
76        <p>
77            <label for="<?php echo $this->get_field_id('title', 'mytextdomain' ); ?>"><?php _e( 'Title:', 'mytextdomain'  ); ?></label>
78            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
79        </p>
80        <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" <?php checked( $count ); ?> disabled="disabled" />
81        <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e( 'Show post counts', 'mytextdomain'  ); ?></label>
82        <br />
83        <?php
84    }
85}
86