I followed a guide to create a WordPress plugin, which shows a youtube sub button on your page.
In the guide it works perfectly, but for some reason it doesn’t work for me.
This is my problem: When I var_dump($ instance) in widget() it prints out an empty array, but when I try to print_r($ instance) inside of my form() it prints out the correct information
Here is the widget file:
<?php /** * Adds Youtube_Subs widget. */ class Youtube_Subs_Widget extends WP_Widget { /** * Register widget with WordPress. */ function __construct() { parent::__construct( 'youtubesubs_widget', // Base ID esc_html__( 'YouTube Subs', 'mp_domain' ), // Name array( 'description' => esc_html__( 'Widget to display YouTube subs', 'mp_domain' ), ) // Args ); } /** * 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 ) { echo $ args['before_widget']; // Whatever you want to display before widget (<div>, etc) if ( ! empty( $ instance['title'] ) ) { echo $ args['before_title'] . apply_filters( 'widget_title', $ instance['title'] ) . $ args['after_title']; } // Widget Content Output var_dump($ instance); // echo '<div class="g-ytsubscribe" data-channel="'.$ instance['channel'].'" data-layout="'.$ instance['layout'].'" data-count="'.$ instance['count'].'"></div>'; echo $ args['after_widget']; // Whatever you want to display after widget (</div>, etc) } /** * 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'] : esc_attr( 'YouTube Subs', 'mp_domain' )); $ channel = (! empty( $ instance['channel'] ) ? $ instance['channel'] : esc_attr( 'techguyweb', 'mp_domain' )); $ layout = (! empty( $ instance['layout'] ) ? $ instance['layout'] : esc_attr( 'default', 'mp_domain' )); $ count = (! empty( $ instance['count'] ) ? $ instance['count'] : esc_attr( 'default', 'mp_domain' )); ?> <!-- TITLE --> <p> <label for="<?php echo esc_attr( $ this->get_field_id( 'title' ) ); ?>"> <?php esc_attr_e( 'Title:', 'mp_domain' ); ?> </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> <!-- CHANNEL --> <p> <label for="<?php echo esc_attr( $ this->get_field_id( 'channel' ) ); ?>"> <?php esc_attr_e( 'Channel:', 'mp_domain' ); ?> </label> <input class="widefat" id="<?php echo esc_attr( $ this->get_field_id( 'channel' ) ); ?>" name="<?php echo esc_attr( $ this->get_field_name( 'channel' ) ); ?>" type="text" value="<?php echo esc_attr( $ channel ); ?>"> </p> <!-- LAYOUT --> <p> <label for="<?php echo esc_attr( $ this->get_field_id( 'layout' ) ); ?>"> <?php esc_attr_e( 'Layout:', 'mp_domain' ); ?> </label> <select class="widefat" id="<?php echo esc_attr( $ this->get_field_id( 'layout' ) ); ?>" name="<?php echo esc_attr( $ this->get_field_name( 'layout' ) ); ?>"> <option value="default" <?php echo ($ layout == 'default') ? 'selected' : ''; ?>> Default </option> <option value="full" <?php echo ($ layout == 'full') ? 'selected' : ''; ?>> Full </option> </select> </p> <!-- COUNT --> <p> <label for="<?php echo esc_attr( $ this->get_field_id( 'count' ) ); ?>"> <?php esc_attr_e( 'Count:', 'mp_domain' ); ?> </label> <select class="widefat" id="<?php echo esc_attr( $ this->get_field_id( 'count' ) ); ?>" name="<?php echo esc_attr( $ this->get_field_name( 'count' ) ); ?>"> <option value="default" <?php echo ($ count == 'default') ? 'selected' : ''; ?>> Default </option> <option value="hidden" <?php echo ($ count == 'hidden') ? 'selected' : ''; ?>> Hidden </option> </select> </p> <?php print_r($ instance); } /** * 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'] = ( ! empty( $ new_instance['title'] ) ) ? strip_tags( $ new_instance['title'] ) : ''; $ instance['channel'] = ( ! empty( $ new_instance['channel'] ) ) ? strip_tags( $ new_instance['channel'] ) : ''; $ instance['layout'] = ( ! empty( $ new_instance['layout'] ) ) ? strip_tags( $ new_instance['layout'] ) : ''; $ instance['count'] = ( ! empty( $ new_instance['count'] ) ) ? strip_tags( $ new_instance['count'] ) : ''; return $ instance; } }
I have this hooked up with my plugin file in where I register the widget:
// Register Widget function register_myplugin() { register_widget('Youtube_Subs_Widget'); } // Hook in function add_action('widgets_init', 'register_myplugin');
I hope someone knows what is wrong and can help me correct my mistake, thanks in advance