Cmb2: Can't use cmb2 in a widget

Created on 14 Jun 2016  路  13Comments  路  Source: CMB2/CMB2

I've attempted using cmb2 in a widget since I'm building a site with modular blocks with site-origin's page-builder plugin. I know that the widget itself has to handle the updating of the values so I've tried abstracting that away and only use CMB2 to render the fields itself.

I've come so far so that I can render the fields but I can't get any of the JS work, like the file field, and it's really hacky. I must manually include the cmb2 files since their not accessible when the widget_init hook runs, and if I load the CMB2 class file then no metaboxes gets loaded on the admin page. Is there any documentation that explains how to only use the fields without the metabox functionality of the plugin? From what I've read online it seems that this is not supported, which is why I created the issue.

Any help explaining how this can be achieved would be greatly appreciated. Thanks!

Most helpful comment

I have a working implementation now which is quite clean. It just needs to reinitialize CMB2 on the widget after values have been saved, which is easily done with wordpress events, but that's the only thing on client side. I can probably publish it later. Busy with work at the moment.

All 13 comments

@nsrosenqvist Do you have any luck? I am also curious about this, I've tried rendering CMB2 field on the widget, but still no success. I only need the CMB2 field to be able to render on the Widget areas without any post processing.

@jtsternberg Cool!, Missed that one :D

Closing unless we have some detail that's actionable for inclusion in CMB2 core.

Was this issue fixed? Because I still cannot use widget example in widget area. Widget with form was rendering but colorpicker and image chooser was not work.

@rockerzz just in case you didn't see it, did you look at https://github.com/WebDevStudios/CMB2-Snippet-Library/blob/master/widgets/widget-example.php ?

Other than that, chances are nothing has been done on this since August. Hard to call a bug because it's not something we state is supported, things just don't necessarily have the support built into core and ready for at this point.

@tw2113 Any suggestions on how I can get the fields saved by the widget? Widget-example.php doesn't return any fields. I'm not sure how to fetch the fields. Are they saved as an option? Any help much appreciated :)

I don't personally have an answer as I've never implemented CMB2 in widgets.

I have a working implementation now which is quite clean. It just needs to reinitialize CMB2 on the widget after values have been saved, which is easily done with wordpress events, but that's the only thing on client side. I can probably publish it later. Busy with work at the moment.

Has anyone managed to get this working, and if so would you consider sharing your secrets? Thank you! @nsrosenqvist hi there, any chance you can share your work? Thanks in advance!

@applesapples1 I've made a MIT licensed package that I'll probably publish within the next month, that way it's easy to integrate and you can basically do whatever you want with it. Will post a link to it when I do

@nsrosenqvist would be waiting for an update from you :) Thanks

@manzoorwanijk @rockerzz @applesapples1 @alexanderdejong Ok, here we go. Sorry for the delay. It's quite hacky but it works well enough for my use case. If you can improve it then I gladly accept your contributions.

The widget class extends the normal WP_Widget class and has the fields defined in the $fields property. Implement your own widget function, the fields you define should be provided in the $instance variable.

https://github.com/nsrosenqvist/cmb2-widget

I install it via Composer and load it into a custom framework, but I also provided a regular file for normal Wordpress plugin installation. I haven't tried if it works enabling it as a regular plugin but I'm pretty sure it should work. I also managed to integrate CMB2 into navigation menus. You can find the package for that here:

https://github.com/nsrosenqvist/cmb2-nav-menus

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abuyoyo picture abuyoyo  路  7Comments

mathetos picture mathetos  路  8Comments

gareth-gillman picture gareth-gillman  路  6Comments

angelorocha picture angelorocha  路  6Comments

stabilimenta picture stabilimenta  路  8Comments