Support

Account

Home Forums General Issues Custom widget not displaying ACF value

Unread

Custom widget not displaying ACF value

  • I’ve created a custom widget based on this code here. However when I try to display the ACF values in the front-end and they aren’t displaying. Am I supposed to be editing something in the ‘sanitize widget’ section?

    /**
     * Adds custom widget.
     */
    
    class Custom_Widget extends WP_Widget {
    
    //Register widget with WordPress.
    function __construct() {
        parent::__construct(
          'custom_widget', // Base ID
          __('Social Links', 'text_domain'), // Name
          array( 'description' => __( 'Social links for menu', 'text_domain' ), ) // Args
        );
    }
    
    //Front-end display of widget.
    public function widget( $args, $instance ) {
        echo $args['before_widget'];?>
        <ul>
            <li><a href="<?php the_field('twitter_link', 'widget_' . $widget_id); ?>">twitter</a></li>
            <li></li>
        </ul>
        <?php echo $args['after_widget'];
    }
    
    //Back-end widget form.
    public function form( $instance ) {
    
    }
    
    //Sanitize widget form values as they are saved.
    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
    
        return $instance;
        }
    }
    
    // register Custom_Widget widget
    add_action( 'widgets_init', function(){
        register_widget( 'Custom_Widget' );
    });

    I’m adding my values using ACF, which you can see here:

    Here is my widget with the data saved:

Viewing 1 post (of 1 total)

The topic ‘Custom widget not displaying ACF value’ is closed to new replies.