Hi all!
I’m new into plugins building but I love to learn new things!
I have a problem with my first custom plugin, that it’s a widget. in short I need to do a loop of a repeater that have two fields in the back end and this repeater is looped in a flexslider.
The only problem is that the loop in the front-end won’t start, the back-end works well, all the custom fields are working!
<?php
/*
Plugin Name: Aside Sponsor Gallery
Plugin URI: https://skriba.it
Description: Create a small slider in your sidebar! :D
Version: 0.1
Author: PeckPress
Author URI: https://skriba.it
License: Personal
Copyright 2019 Tusca
*/
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['sponsors'] = strip_tags( $new_instance['sponsors'] );
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['sponsors'] );
?>
<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' );
});
Some functions in this context may require a $post_id
passed as param, at least have_rows()
and get_field()
, e.g.:
while ( have_rows( 'sponsors', 'widget_gallerysponsor_widget' ) ) : the_row();
In your case the location is set to widget, so the $post_id
value should be widget_{$widget_id}
. See this documentation page for details.
Hi Ihor and thank you for the support, I had just tried to specify also the right widget ID in have_rows
but it had not worked. Now I’m just trying to add 'widget_' . $widget_id
in all the custom fields but it seems not working… Maybe i need to $istance
something as I did for the plugin title?
I don’t know if I’m missing something in the plugin code or it just won’t read the loop elements so it doesn’t start…
Has anyone had a similar issue with custom plugin?
EDIT ///////////
if i print $widget_id:
it prints it, so the widget_id works
@tusca95 Please provide the full code so I can check it locally.
This is the full code! The one that I wrote on the first message, you should create a new phone file in a folder in the plugins folder and you’re done!
Thanks for your time!
PLease someone can help me to solve this trouble? I can’t figure out the problem..
@tusca95 You’re apparently developing without error reporting turned on. Your code throws multiple (E_NOTICE) Undefined index: notices for $instance[‘title’] and $instance[‘sponsors’] usage.
$instance['sponsors'] = strip_tags( $new_instance['sponsors'] );
In this line, you expect $instance[‘sponsors’], but that’s not how ACF sends the data, I suppose. Please, turn on error reporting and debug the incoming request first.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.