• Hi, i’m creating a new wordpress widget for outputting images and links in my dynamic sidebar in my theme.

    But the widget prints an number at the end.
    This is my code :

    class text_image_widget extends WP_Widget {
        function __construct() {
            parent::__construct(
                'custom-widget', // Base ID
                __( 'custom-widget', 'text_domain' ), // Name
                array( 'description' => __( 'custom-widget', 'text_domain' ), ) // Args
            );
    
            add_action('admin_enqueue_scripts', array($this, 'upload_scripts'));
        }
    
        /**
         * Upload the Javascripts for the media uploader
         */
        public function upload_scripts()
        {
            wp_enqueue_script('media-upload');
            wp_enqueue_script('thickbox');
            wp_enqueue_script('upload_media_widget', plugin_dir_url(__FILE__) . 'upload-media.js', array('jquery'));
    
            wp_enqueue_style('thickbox');
        }
    
        /**
         * Front-end display of widget.
         *
         * @see WP_Widget::widget()
         *
         * @param array $args     Widget arguments.
         * @param array $instance Saved values from database.
         */
        public function widget( $args, $instance ) {
        ?>
            <img src="<?= $instance['image'] ?>" alt="" class="img-responsive center-block">
            <h3><?= $instance['title']?></h3>
            <p>
                <?= $instance['body'] ?>
            </p>
            <a href="<?= $instance['link']?>" class="btn btn-white-border">
                Lees meer
            </a>
        <?php
        }
    
        /**
         * Back-end widget form.
         *
         * @see WP_Widget::form()
         *
         * @param array $instance Previously saved values from database.
         */
        public function form( $instance ) {
            $title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'New title', 'text_domain' );
            ?>
            <p>
                <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( esc_attr( 'Title:' ) ); ?></label>
                <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
            </p>
            <?php
    
            $body = ! empty( $instance['body'] ) ? $instance['body'] : __( 'New body', 'text_domain' );
            ?>
            <p>
                <label for="<?php echo esc_attr( $this->get_field_id( 'body' ) ); ?>"><?php _e( esc_attr( 'Body:' ) ); ?></label>
                <textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'body' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'body' ) ); ?>" rows="10"><?php echo esc_attr( $body ); ?></textarea>
            </p>
            <?php
    
            $image = '';
            if(isset($instance['image']))
            {
                $image = $instance['image'];
            }
            ?>
            <p>
                <label for="<?php echo $this->get_field_name( 'image' ); ?>"><?php _e( 'Image:' ); ?></label>
                <input name="<?php echo $this->get_field_name( 'image' ); ?>" id="<?php echo $this->get_field_id( 'image' ); ?>" class="widefat" type="text" size="36"  value="<?php echo esc_url( $image ); ?>" />
                <input class="upload_image_button button button-primary" type="button" value="Upload Image" />
            </p>
            <?php
    
            $link = ! empty( $instance['link'] ) ? $instance['link'] : __( 'New link', 'text_domain' );
            ?>
            <p>
                <label for="<?php echo esc_attr( $this->get_field_id( 'link' ) ); ?>"><?php _e( esc_attr( 'Lees meer link:' ) ); ?></label>
                <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'link' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'link' ) ); ?>" type="text" value="<?php echo esc_attr( $link ); ?>">
            </p>
            <?php
        }
    
        /**
         * Sanitize widget form values as they are saved.
         *
         * @see WP_Widget::update()
         *
         * @param array $new_instance Values just sent to be saved.
         * @param array $old_instance Previously saved values from database.
         *
         * @return array Updated safe values to be saved.
         */
        public function update( $new_instance, $old_instance ) {
            $instance = array();
            $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
            $instance['body'] = ( ! empty( $new_instance['body'] ) ) ? strip_tags( $new_instance['body'] ) : '';
            $instance['image'] = ( ! empty( $new_instance['image'] ) ) ? strip_tags( $new_instance['image'] ) : '';
            $instance['link'] = ( ! empty( $new_instance['link'] ) ) ? strip_tags( $new_instance['link'] ) : '';
    
            return $instance;
        }

    Where i call it:

    <article class="services services-1">
    				<?= dynamic_sidebar('dienst1')?>
    			</article>

    What it prints:

    <article class="services services-1">
    				        <img src="https://localhost:8888/-----/wordpress/wp-content/uploads/2016/08/message.png" alt="" class="img-responsive center-block">
            <h3>Cursus</h3>
            <p>
                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras congue, metus non tincidunt ullamcorper, dolor liberdso egestas augue, vel placerat lectus nisi in sapien.        </p>
            <a href="https://localhost:8888/-------------/wordpress/voorbeeld-pagina/" class="btn btn-white-border">
                Lees meer
            </a>
        1			</article>
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Widget Pugin prints an extra unwanted number’ is closed to new replies.