0

I'm building a plugin that it needs to create a widget with some custom fields. I'm not an expert, this is my first plugin, but I'm interested to learn on how to develop more plugins.

My problem is in the printing process, the back end fields are ok, i've created some fields with ACF, then exported them in PHP and integrated in my custom plugin, than in the front end area I've made the custom loop that is needed to print the ACF loop.

The custom loop in front-end widget cannot see the the loop data and so it won't print anything.

I can't recognize why!

class GallerySponsor_Widget extends WP_Widget {

public function __construct() {

    parent::__construct(
        'gallerysponsor_widget',
        __( 'Aside Sponsor Gallery', 'gallerysponsordomain' ),
        array(
            'classname'   => 'gallerysponsor_widget',
            'description' => __( 'Create a small slider in your sidebar! :D', 'gallerysponsordomain' )
            )
    );

    load_plugin_textdomain( 'gallerysponsordomain', false, basename( dirname( __FILE__ ) ) . '/languages' );

}

/**  
 * 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 ) {    

    extract( $args );

    $title      = apply_filters( 'widget_title', $instance['title'] );

    echo $before_widget;

    echo $before_title . $title . $after_title; 

    if ( have_rows('sponsors') ): ?>

        <div class="slider">
            <div class="slide-wrap">
                <div id="sponsor" class="flexslider">
                    <ul class="slides">

                        <?php while( have_rows('sponsors') ): the_row();

                            $image = get_sub_field('immagine-sponsor');
                            $link = get_sub_field('link-sponsor');
                            ?>

                        <a href="<?php echo $link ?>">
                            <li style="background-image:url(<?php echo $image; ?>);" class="slide"></li>
                        </a>

                        <?php endwhile; ?>

                    </ul>
                </div>
            </div>
            </div>

    <?php endif; 

    echo $message;
    echo $after_widget;

}


/**
  * 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 = $old_instance;

    $instance['title'] = strip_tags( $new_instance['title'] );
    $instance['slider'] = strip_tags( $new_instance['slider'] );

    return $instance;

}

/**
  * Back-end widget form.
  *
  * @see WP_Widget::form()
  *
  * @param array $instance Previously saved values from database.
  */
public function form( $instance ) {    

    $title      = esc_attr( $instance['title'] );
    $slider    = esc_attr( $instance['slider'] );
    ?>

    <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 $title; ?>" />
    </p>

    <?php if( function_exists('acf_add_local_field_group') ):

    acf_add_local_field_group(array(
        'key' => 'group_5c05480e7e77e',
        'title' => 'Widget sponsor',
        'fields' => array(
            array(
                'key' => 'field_5c05486bb0d1e',
                'label' => 'Sponsors',
                'name' => 'sponsors',
                'type' => 'repeater',
                'instructions' => '',
                'required' => 0,
                'conditional_logic' => 0,
                'wrapper' => array(
                    'width' => '',
                    'class' => '',
                    'id' => '',
                ),
                'collapsed' => '',
                'min' => 0,
                'max' => 0,
                'layout' => 'table',
                'button_label' => '',
                'sub_fields' => array(
                    array(
                        'key' => 'field_5c054821b0d1d',
                        'label' => 'immagine-sponsor',
                        'name' => 'immagine-sponsor',
                        'type' => 'image',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'return_format' => 'url',
                        'preview_size' => 'thumbnail',
                        'library' => 'all',
                        'min_width' => '',
                        'min_height' => '',
                        'min_size' => '',
                        'max_width' => '',
                        'max_height' => '',
                        'max_size' => '',
                        'mime_types' => '',
                    ),
                    array(
                        'key' => 'field_5c054888b0d1f',
                        'label' => 'link-sponsor',
                        'name' => 'link-sponsor',
                        'type' => 'url',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'default_value' => '',
                        'placeholder' => '',
                    ),
                ),
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'widget',
                    'operator' => '==',
                    'value' => 'gallerysponsor_widget',
                ),
            ),
        ),
        'menu_order' => 0,
        'position' => 'normal',
        'style' => 'default',
        'label_placement' => 'top',
        'instruction_placement' => 'label',
        'hide_on_screen' => '',
        'active' => 1,
        'description' => '',
    ));

    endif; 
}

}

/* Register the widget */
add_action( 'widgets_init', function(){
     register_widget( 'GallerySponsor_Widget' );
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.