Carbon-fields: How to use the_widget with carbon?

Created on 28 Mar 2018  路  6Comments  路  Source: htmlburger/carbon-fields

This is a great library.
I'm using your library on my project WordPress and a have a issue:

  • I used carbon field widget to render setting form -> and jQuery .serialize() to get all data in form -> save to WP Options. Now I want to use this data with "the_widget".

Do you have a solution for me? Thank you so much!

[type] question

All 6 comments

Hi @Huynhhuynh ,

Can you provide more information about your use case or a sample code, please?

Hi, @deqngeorgiev

I'm created a widget (please see image) with class 'Flintotheme_Widget_Branding' and 4 settings:

  1. title - text field,
  2. branding_gallery_data - complex
  3. branding_gallery_column - select

4. open_on_new_window - checkbox

And now i want to use like it or something like that:

the_widget('Flintotheme_Widget_Branding', array(
'title' => '...',
'branding_gallery_data' => array(...),
'branding_gallery_column' => '...',
'open_on_new_window' => '...'
))

I hope it clean for you. Thanks!

You should add a _ at the beginning of the array keys (see the example below). Also, each of the values for the complex should be passed as a separate key => value pair to the array.

the_widget( 'Flintotheme_Widget_Branding', array(
    '_title' => '...',
    '_branding_gallery_data|image|0|0|value' => '', // First item image id
    '_branding_gallery_data|name|0|0|value' => '', // First item name
    '_branding_gallery_data|url|0|0|value' => '', // First item url

    '_branding_gallery_data|image|1|0|value' => '', // Second item image id
    '_branding_gallery_data|name|1|0|value' => '', // Second item name
    '_branding_gallery_data|url|1|0|value' => '', // Second item url

    '_branding_gallery_data|image|2|0|value' => '', // Third item image id
    '_branding_gallery_data|name|2|0|value' => '', // Third item name
    '_branding_gallery_data|url|2|0|value' => '', // Third item url

    '_branding_gallery_column' => '...',
    '_open_on_new_window' => '...'
) );

Thank you so much!, and more.
This library has support a function convert array to like this?

'_branding_gallery_data|image|0|0|value' => '', // First item image id
'_branding_gallery_data|name|0|0|value' => '', // First item name
'_branding_gallery_data|url|0|0|value' => '', // First item url

I believe it would be easier for you to create a custom function which will convert your array to array with these keys.

Okay, thank for your help! 馃殌

Was this page helpful?
0 / 5 - 0 ratings