Support

Account

Home Forums General Issues ACF and Widgets

Solved

ACF and Widgets

  • Hi,

    I was hoping someone could clear something up for me.

    I’d like to create some custom widgets to use within the WordPress Widgets section. I would want them to work just like regular widgets whereas, you can add as many as you like, in whichever ‘sidebar’ widget areas as you like.

    Is this possible with ACF? I understand that I may need to create WP Widget code and include the ACF stuff, if that is possible?

    Any pointers would be most welcome.

  • I don’t know exactly what you want but here is what i think it is:

    You can assign the custom forms to widgets, unfortunately it’s only possible on ACF5.

    First step is creating your widget:

    class CustomWidget extends WP_Widget {
    	function __construct() {
    		parent::__construct(
    			'custom_widget', // Base ID
    			__('CustomWidget', 'text_domain'), // Name
    			array( 'description' => __( 'Some CustomWidget', 'text_domain' ), ) 
    		);
    	}
    }
     
    // register Foo_Widget widget
    function register_foo_widget() {
        register_widget( 'CustomWidget' );
    }
    add_action( 'widgets_init', 'register_foo_widget' );

    After that, on the menu go Custom Fields > Custom Fields > Your Field > At the Location tab (Rules) choose on the first select “Widget” after that select “is equal to” and the name of your widget, in this case: “CustomWidget”.

    After that save your field, go to theme > widget and drag and drop your widget on the area that you want.

    I’m sending 2 pictures, on of the configuration field, and the other is the widget panel.

    Hope this helps you! =D

  • That’s awesome.

    I am going to update to V5 very very soon. This will solve my problem.

    Thank you

  • This is a great feature! I don’t understand how to retrieve the saved values and display them on the front-end. I tried get_field in the widget function of the widget class but that doesn’t work. Anyone has an idea how to retrieve the acf data saved to widgets?

    EDIT:

    Well, I think I found a solution. This is how I did it in the widget function of the widget class:

    $test_value = get_field('test', 'widget_' . $args['widget_id']);

  • Dear @wwdboer ,

    you just officially saved my day – thank you so much <3!

  • God bless Google and @wwdboer
    Thank you!

  • Widget added, fields added. Only problem, it doesn’t seem to save any value. What’s missing?

  • There is no save button. More code is needed for widget logic. There has to be some field to be saved. Otherwise you cant save…

  • You are right and also not right.

    This is correct:
    “There is no save button.”

    This is not true:
    There has to be some field to be saved. Otherwise you cant save…

    Solution:
    Your widget class must implement not only __construct (and widget(...) for display), but also the minimum version of method form($instance) that returns the provided $instance:

    public function form($instance){ return $instance; }

Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘ACF and Widgets’ is closed to new replies.