Widget not working on 4.3 version

Thanks for reply I am very grateful Kārlis Millers, dingo-d now my widget it looks how.

<?php
    class ultimos_productos_widget_1 extends WP_Widget {

    function __construct() {

    parent::__construct(
        'ultimos_productos_widget', __( 'ultimos_productos_widget_1', 'tutsplustextdomain' ),
        array(
            'classname'   => 'ultimos_productos_widget_1',
            'description' => __( 'A basic text widget to demo the Tutsplus series on creating your own widgets.', 'tutsplustextdomain' )
            )
        );
    }

    public function widget($args,$instance){

        extract( $args );

        echo $before_widget = '<div class="sidebar woocommerce">' ;    
        ?>
        <h3 class="widget-title"> <?php _e('Ultimate Products', 'pruebadesarrollo'); ?></h3>
        <ul class="products">
            <?php
                $args = array( 
                    'post_type' => 'product', 
                    'posts_per_page' => 5, 
                    'product_cat' => 'marble',
                    'orderby' => 'ASC', 
                    /*'tax_query' => array(
                                array(
                                    'taxonomy' => 'product_cat',
                                    'field'    => 'slug',
                                    'terms'    => 'marble',
                                ),
                            ),*/
                    );
                $loop = new WP_Query( $args );
                while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
                    <li class="product">    
                        <a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
                            <?php woocommerce_show_product_sale_flash( $post, $product ); ?>
                            <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
                            <h3><?php the_title(); ?></h3>
                            <span class="price"><?php echo $product->get_price_html(); ?></span>                    
                        </a>
                        <?php if(is_user_logged_in() ){ ?>
                            <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
                        <?php } ?>
                    </li>
            <?php endwhile; ?>
            <?php wp_reset_postdata() ?>
        </ul><!--/.products-->
        <?php
        echo $after_widget='</div>';
    }

    public function update($new_instance, $old_instance){
        // Función de guardado de opciones   
    }

    public function form($instance){
        // Formulario de opciones del Widget, que aparece cuando añadimos el Widget a una Sidebar
    }    
} 


function myplugin_register_widgets() {
    register_widget( 'ultimos_productos_widget_1' );
}

add_action( 'widgets_init', 'myplugin_register_widgets' );
?>

It was working before of update a to wordpress 4.3.

Read More

In wp_debug show me this message:

Notice: The called constructor method for WP_Widget is deprecated since version 4.3.0! Use

Internet research I found that I should change the next line.

$this->WP_Widget('ultimos_productos_widget', "ultimos 5 proyectos", $widget_ops);

for

parent::__construct('ultimos_productos_widget', "ultimos 5 proyectos", $widget_ops);

I dont know what happened?

Related posts

2 comments

  1. Widget need register after you created it
    Just put after your class:

    register_widget('ultimos_productosaluminum_widget');
    

    Or in wordpress style:

    function register_ultimos_widget() {
            register_widget('ultimos_productosaluminum_widget');
    }
    add_action( 'widgets_init', 'register_ultimos_widget' );
    

    See more at CODEX


  2. Thanks for your comments,

    I modified the code with the suggestions y this is my custom widget.

    class ultimos_productos_widget extends WP_Widget {
    
        function __construct() {
            $widget_ops = array('classname' => 'ultimos_productos_widget', 
                                'description' => "5 ultimos 5 productos por categoría" );
            parent::__construct('ultimos_productos_widget', "ultimos 5 productos por categoría", $widget_ops);
        }
    
        public function widget($args,$instance){
            extract( $args );
            $title = apply_filters( 'widget_title', $instance['title']);
            $ids   = $instance['ids'];
            $query_args = array(
                'post_status'    => 'publish',
                'post_type'      => 'product',
                'posts_per_page'  => 5,
                'orderby' => 'ASC',
                'tax_query' => array(
                            array(
                                'taxonomy' => 'product_cat',
                                'field'    => 'slug',
                                'terms'    => $ids,
                            ),
                        ),
            ); 
    
            $r = new WP_Query( $query_args );
            if ( $r->have_posts() ) {
    
                echo $before_widget;
    
                if ( $title )
                    echo $before_title . $title . $after_title;
    
                echo '<ul class="product_list_widget">';
    
                while ( $r->have_posts()) {
                    $r->the_post();
                    global $product;
                     ?>
                        <li>
                            <a href="<?php echo esc_url( get_permalink( $product->id ) ); ?>" title="<?php echo esc_attr( $product->get_title() ); ?>">
                                <?php echo $product->get_image(); ?>
                                <?php echo $product->get_title(); ?>
                            </a>
                            <?php if ( ! empty( $show_rating ) ) echo $product->get_rating_html(); ?>
                            <?php if(is_user_logged_in() ){ ?>  
                                <?php echo $product->get_price_html(); ?>
                            <?php } ?>
                        </li>
                    <?php
                }
    
                echo '</ul>';
    
                echo $after_widget;
            }
    
            wp_reset_postdata();
    
            echo $content;
    
        }
    
        public function form($instance){
            $title = (isset($instance['title'])) ? $instance['title'] : __( 'Products', 'ndb' );
            $ids = $instance['ids'];
            ?>
            <p>
                <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('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('ids'); ?>"><?php _e('Category :'); ?></label>
                <input class="widefat" id="<?php echo $this->get_field_id('ids'); ?>"  name="<?php echo $this->get_field_name( 'ids' ); ?>" type="text" value="<?php echo esc_attr( $ids ); ?>" />
            </p>
        <?php
    
        }
    
        public function update( $new_instance, $old_instance ) {
            $instance = array();
            $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
            $instance['ids'] = ( ! empty( $new_instance['ids'] ) ) ? strip_tags( $new_instance['ids'] ) : '';
            return $instance;
        }    
    }
    
    function ultimos_productos() {
        register_widget( 'ultimos_productos_widget' );
    }
    
    add_action( 'widgets_init', 'ultimos_productos' );
    

    This a plugin and works perfectly.

    Thanks.

Comments are closed.