0

I have a custom widget file in my theme but there is no custom widget in Appearance > Widgets. Here is the code for the custom widget:

<?php
class box1 extends WP_Widget {
    function __construct() {
        parent::__construct(
            'yiw_pro_widget',
            __('ابزارک اختصاصی باکس مطالب مدل اول', 'wordpress'),
            array( 'description' => __( 'ابزارک نمایش مطالب دسته ای خاص با استایل متفاوت مدل اول', 'wordpress' ), )
        );
    }
    public function widget( $args, $instance ) {
        $name = apply_filters( 'widget_title', $instance['name'] );
        $link = apply_filters( 'widget_title', $instance['link'] );
        $cat = apply_filters( 'widget_title', $instance['cat'] );
        $color = apply_filters( 'widget_title', $instance['color'] );
?>      
        <section id="box" class="box1">
                        <h5 style="border-bottom:2px solid <?php echo $color ?>;"><a style="background:<?php echo $color ?>;" href="<?php echo $link ?>"><?php echo $name ?></a></h5>
                        <div class="col-md-6 col-xs-12">
                        <?php
                                        $portfolio = new WP_Query(array(
                                        'post_status' =>'publish',
                                        'post_type' =>'post',
                                        'cat' =>$cat,
                                        'posts_per_page' =>'1'      
                                        ));
                            if($portfolio->have_posts()) : while($portfolio->have_posts()) : $portfolio->the_post(); ?>
                            <article class="first">
                                <figure><?php the_post_thumbnail('box1'); ?></figure>
                                <h2><a href="<?php the_permalink() ?>"><?php the_title();?></a></h2>
                                <ul><li><i class="glyphicon glyphicon-calendar"></i><a><?php the_time('d M Y');?></a></li><li><i class="glyphicon glyphicon-comment"></i><a><?php comments_number();?></a></li></ul>
                                <p><?php the_excerpt(); ?></p>
                            </article>
                            <?php endwhile; endif;  wp_reset_query(); ?>
                        </div>
                        <div class="col-md-6 col-xs-12">
                        <?php
                                        $portfolio = new WP_Query(array(
                                        'post_status' =>'publish',
                                        'post_type' =>'post',
                                        'cat' =>$cat,
                                        'offset' => '1',
                                        'posts_per_page' =>'4'      
                                        ));
                            if($portfolio->have_posts()) : while($portfolio->have_posts()) : $portfolio->the_post(); ?>
                            <article class="first">
                                <figure><?php the_post_thumbnail('box12'); ?></figure>
                                <h2><a href="<?php the_permalink()?>"><?php the_title(); ?></a></h2>
                                <ul><li><i class="glyphicon glyphicon-calendar"></i><a><?php the_time('d M Y');?></a></li><li><i class="glyphicon glyphicon-comment"></i><a><?php comments_number(); ?></a></li></ul>
                            </article>
                            <?php endwhile; endif;  wp_reset_query(); ?>
                        </div>
                        <div class="clear"></div>
                    </section>
                    <?php
        echo $args['after_widget'];
    }
    public function form( $instance ) {
        $name   = ( isset( $instance[ 'name' ] ) ) ? $instance[ 'name' ] : '';
        $link   = ( isset( $instance[ 'link' ] ) ) ? $instance[ 'link' ] : '';
        $color  = ( isset( $instance[ 'color' ] ) ) ? $instance[ 'color' ] : '';
        $cat    = ( isset( $instance[ 'cat' ] ) ) ? $instance[ 'cat' ] : '';
        ?>

            <p>
                <label for="<?php echo $this->get_field_id( 'color' ); ?>"><?php _e( 'رنگ باکس مطالب:' ); ?></label>
                <input class="widefat" id="<?php echo $this->get_field_id( 'color' ); ?>" name="<?php echo $this->get_field_name( 'color' ); ?>" type="text" value="<?php echo esc_attr( $color ); ?>" placeholder="مثال : #CCC , #dd3333 , black , blue" />
            </p>
            <p>
                <label for="<?php echo $this->get_field_id( 'name' ); ?>"><?php _e( 'عنوان دسته بندی:' ); ?></label>
                <input class="widefat" id="<?php echo $this->get_field_id( 'name' ); ?>" name="<?php echo $this->get_field_name( 'name' ); ?>" type="text" value="<?php echo esc_attr( $name ); ?>" />
            </p>
            <p>
                <label for="<?php echo $this->get_field_id( 'link' ); ?>"><?php _e( 'لینک آرشیو دسته بندی:' ); ?></label>
                <input class="widefat" id="<?php echo $this->get_field_id( 'link' ); ?>" name="<?php echo $this->get_field_name( 'link' ); ?>" type="text" value="<?php echo esc_attr( $link ); ?>" />
            </p>
            <p>
                <label for="<?php echo $this->get_field_id( 'cat' ); ?>"><?php _e( 'آی دی دسته بندی:' ); ?></label>
                <input class="widefat" id="<?php echo $this->get_field_id( 'cat' ); ?>" name="<?php echo $this->get_field_name( 'cat' ); ?>" type="text" value="<?php echo esc_attr( $cat ); ?>" placeholder="پس از نصب افزونه wp_show_id از بخش نوشته ها/دسته ها آی دی دسته بندی مورد نظر را وارد کنید" />
            </p>

        <?php
    }
    public function update( $new_instance, $old_instance ) {
        $instance = array();

        $instance['name'] = ( ! empty( $new_instance['name'] ) ) ? strip_tags( $new_instance['name'] ) : '';
        $instance['link'] = ( ! empty( $new_instance['link'] ) ) ? strip_tags( $new_instance['link'] ) : '';
        $instance['color'] = ( ! empty( $new_instance['color'] ) ) ? strip_tags( $new_instance['color'] ) : '';
        $instance['cat'] = ( ! empty( $new_instance['cat'] ) ) ? strip_tags( $new_instance['cat'] ) : '';
        return $instance;
    }
}
function load_box1() {
    register_widget( 'box1' );
}
add_action( 'widgets_init', 'load_box1' );
?>
1
  • How are you including your widget code? Is it in a separate file, or is the code you've posted straight out of your functions.php file? If it's an include, please edit your question to show that code too. Oct 9, 2016 at 21:40

1 Answer 1

1

Your code worked for me; The widget appears in the admin area under Appearance > Widgets and the widget does show output on the front end.

Your widget() method is missing the output for $args['before_widget']. echo $args['before_widget']; should be added above <section id="box" class="box1">.

This would not cause the issue reported, where the widget does not appear in the admin area, but it will cause problems with the HTML output on the front end.

Widget in admin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.