How to use fw-storage in Theme Options and Customizer
Hi!
Sorry for this questions, but it is still not clear how to use fw-storage to make same options in customizer and theme options screen
https://github.com/ThemeFuse/Unyson/issues/2170 https://github.com/ThemeFuse/Unyson/issues/1388
i'm trying to save Customizer and Theme Options in same location, my option has following code:
'top_bar_enabled' => array(
'type' => 'multi-picker',
'label' => false,
'fw-storage' => array(
'type' => 'wp-option',
'wp-option' => 'fw_theme_settings_options:'. fw()->theme->manifest->get_id(),
'key' => 'top_bar_enabled',
),
'picker' => array(
'enabled' => array(
'label' => esc_html__( 'Enable Top Bar', 'mytheme' ),
'type' => 'switch',
'left-choice' => array(
'value' => 'no',
'color' => '#ccc',
'label' => esc_html__( 'No', 'mytheme' )
),
'right-choice' => array(
'value' => 'yes',
'label' => esc_html__( 'Yes', 'mytheme' )
),
'value' => 'no',
)
),
'choices' => array(
'yes' => array(
'top_bar_content' => array(
'type' => 'text',
'label' => esc_html__('Content', 'mytheme'),
'value' => '',
),
)
)
),
this saves Theme options, but doesn't affect an option in Customizer, i always get Top Bar disabled in customizer but it enabled in Theme Options
Also i have a question: how to use fw-storage for Addable Popup option type, should i use parent_option/current option schema for key? Currently it doesn't work for me at all
'fw-storage' => array(
'type' => 'wp-option',
'wp-option' => 'fw_theme_settings_options:'. fw()->theme->manifest->get_id(),
'key' => 'parent_option/current_option',
),
please could you help with that?
One more question, if i'll use fw-storage > wp option for customizer options, does it mean that i can't use fw_get_db_customizer_option() to get an option saved in this way?
p.s. i'm using Multisite, if this affects any thing
this saves Theme options, but doesn't affect an option in Customizer
- Add in Theme Options
'top_bar_enabled' => array(
'type' => 'multi-picker',
'label' => false,
// ...
(no fw-storage here)
- Add in Customizer
'top_bar_enabled' => array(
'type' => 'multi-picker',
'label' => false,
'fw-storage' => array(
'type' => 'wp-option',
'wp-option' => 'fw_theme_settings_options:'. fw()->theme->manifest->get_id(),
'key' => 'top_bar_enabled',
),
// ...
Currently it doesn't work for me at all
How fw-storage works:
-- Dear Unyson, please save (and load) this option's value in a separate wp_option.
-- Ok grand master WP developer, please tell me in which wp_option to save it. Also would you like to overwrite the wp_option or just save the value in a separate array key?
'key' => 'parent_option/current_option',
What is parent and current?
One more question, if i'll use fw-storage > wp option for customizer options, does it mean that i can't use fw_get_db_customizer_option() to get an option saved in this way?
All fw_get_db_..._option() are aware of the fw-storage parameter, so it will work fine, this is the intended way.
Sorry, @moldcraft but it doesn't work for me. Please look at attached theme (it contains just one theme option and one customizer option), try to save this option in customizer and then go to Theme options and check
Test theme https://drive.google.com/open?id=0Bz6XVdLE4r1fYlhtTEI0bjBiMTQ


Customizer.php code:
<?php
$options = array(
'header' => array(
'title' => esc_html__('Header', 'mytheme'),
'type' => 'tab',
'options' => array(
'top_bar' => array(
'title' => esc_html__('Top bar', 'mytheme'),
'type' => 'box',
'attr' => array(
'class' => 'prevent-auto-close'
),
'options' => array(
'top_bar_enabled' => array(
'type' => 'multi-picker',
'label' => false,
'fw-storage' => array(
'type' => 'wp-option',
'wp-option' => 'fw_theme_settings_options:'. fw()->theme->manifest->get_id(),
'key' => 'top_bar_enabled',
),
'picker' => array(
'enabled' => array(
'label' => esc_html__( 'Enable Top Bar', 'mytheme' ),
'type' => 'switch',
'left-choice' => array(
'value' => 'no',
'color' => '#ccc',
'label' => esc_html__( 'No', 'mytheme' )
),
'right-choice' => array(
'value' => 'yes',
'label' => esc_html__( 'Yes', 'mytheme' )
),
'value' => 'no',
)
),
'choices' => array(
'yes' => array(
'top_bar_elements' => array(
'type' => 'addable-popup',
'label' => esc_html__( 'Top Bar Elements', 'mytheme' ),
'popup-title' => esc_html__( 'Add/Edit', 'mytheme' ),
'desc' => esc_html__( 'Add some top bar content', 'mytheme' ),
'template' => '{{if( type.elem_type == "text" ) return type.text.content; if( type.elem_type == "text_icon" ) return type.text_icon.content; if( type.elem_type == "social_icons" ) return type.social_icons.content; }}',
'fw-storage' => array(
'type' => 'wp-option',
'wp-option' => 'fw_theme_settings_options:'. fw()->theme->manifest->get_id(),
'key' => 'top_bar_elements',
),
'popup-options' => array(
'type' => array(
'type' => 'multi-picker',
'label' => false,
'desc' => false,
'value' => array(
'elem_type' => 'text',
),
'picker' => array(
'elem_type' => array(
'label' => esc_html__( 'Element type', 'mytheme' ),
'type' => 'radio',
'choices' => array(
'text' => esc_html__( 'Text', 'mytheme' ),
'text_icon' => esc_html__( 'Text and Icon', 'mytheme' ),
'social_icons' => esc_html__( 'Social icons', 'mytheme' ),
),
)
),
'choices' => array(
'text' => array(
'content' => array(
'type' => 'text',
'label' => esc_html__('Content', 'mytheme'),
'value' => '',
),
'move_right' => array(
'label' => esc_html__( 'Move to the right', 'mytheme' ),
'type' => 'switch',
'left-choice' => array(
'value' => 'no',
'color' => '#ccc',
'label' => esc_html__( 'No', 'mytheme' )
),
'right-choice' => array(
'value' => 'yes',
'label' => esc_html__( 'Yes', 'mytheme' )
),
'value' => 'no',
),
),
'text_icon' => array(
'content' => array(
'type' => 'text',
'label' => esc_html__('Content', 'mytheme'),
'value' => '',
),
'icon' => array(
'type' => 'icon-v2',
),
'move_right' => array(
'label' => esc_html__( 'Move to the right', 'mytheme' ),
'type' => 'switch',
'left-choice' => array(
'value' => 'no',
'color' => '#ccc',
'label' => esc_html__( 'No', 'mytheme' )
),
'right-choice' => array(
'value' => 'yes',
'label' => esc_html__( 'Yes', 'mytheme' )
),
'value' => 'no',
),
),
'social_icons' => array(
'content' => array(
'type' => 'hidden',
'value' => esc_html__('Social Icons', 'mytheme'),
),
'facebook_url' => array(
'type' => 'text',
'label' => esc_html__('Facebook URL', 'mytheme'),
'value' => '',
),
'twitter_url' => array(
'type' => 'text',
'label' => esc_html__('Twitter URL', 'mytheme'),
'value' => '',
),
'linkedin_url' => array(
'type' => 'text',
'label' => esc_html__('LinkedIn URL', 'mytheme'),
'value' => '',
),
'google_plus_url' => array(
'type' => 'text',
'label' => esc_html__('Google Plus URL', 'mytheme'),
'value' => '',
),
'youtube_url' => array(
'type' => 'text',
'label' => esc_html__('YouTube URL', 'mytheme'),
'value' => '',
),
'move_right' => array(
'label' => esc_html__( 'Move to the right', 'mytheme' ),
'type' => 'switch',
'left-choice' => array(
'value' => 'no',
'color' => '#ccc',
'label' => esc_html__( 'No', 'mytheme' )
),
'right-choice' => array(
'value' => 'yes',
'label' => esc_html__( 'Yes', 'mytheme' )
),
'value' => 'no',
),
),
)
),
)
),
)
)
),
)
)
)
),
);
Settings.php code
<?php
if ( ! defined( 'FW' ) ) {
die( 'Forbidden' );
}
$options = array(
array(
'header' => array(
'title' => esc_html__('Header', 'mytheme'),
'type' => 'tab',
'options' => array(
'top_bar' => array(
'title' => esc_html__('Top bar', 'mytheme'),
'type' => 'box',
'attr' => array(
'class' => 'prevent-auto-close'
),
'options' => array(
'top_bar_enabled' => array(
'type' => 'multi-picker',
'label' => false,
'picker' => array(
'enabled' => array(
'label' => esc_html__( 'Enable Top Bar', 'mytheme' ),
'type' => 'switch',
'left-choice' => array(
'value' => 'no',
'color' => '#ccc',
'label' => esc_html__( 'No', 'mytheme' )
),
'right-choice' => array(
'value' => 'yes',
'label' => esc_html__( 'Yes', 'mytheme' )
),
'value' => 'no',
)
),
'choices' => array(
'yes' => array(
'top_bar_elements' => array(
'type' => 'addable-popup',
'label' => esc_html__( 'Top Bar Elements', 'mytheme' ),
'popup-title' => esc_html__( 'Add/Edit', 'mytheme' ),
'desc' => esc_html__( 'Add some top bar content', 'mytheme' ),
'template' => '{{if( type.elem_type == "text" ) return type.text.content; if( type.elem_type == "text_icon" ) return type.text_icon.content; if( type.elem_type == "social_icons" ) return type.social_icons.content; }}',
'popup-options' => array(
'type' => array(
'type' => 'multi-picker',
'label' => false,
'desc' => false,
'value' => array(
'elem_type' => 'text',
),
'picker' => array(
'elem_type' => array(
'label' => esc_html__( 'Element type', 'mytheme' ),
'type' => 'radio',
'choices' => array(
'text' => esc_html__( 'Text', 'mytheme' ),
'text_icon' => esc_html__( 'Text and Icon', 'mytheme' ),
'social_icons' => esc_html__( 'Social icons', 'mytheme' ),
),
)
),
'choices' => array(
'text' => array(
'content' => array(
'type' => 'text',
'label' => esc_html__('Content', 'mytheme'),
'value' => '',
),
'move_right' => array(
'label' => esc_html__( 'Move to the right', 'mytheme' ),
'type' => 'switch',
'left-choice' => array(
'value' => 'no',
'color' => '#ccc',
'label' => esc_html__( 'No', 'mytheme' )
),
'right-choice' => array(
'value' => 'yes',
'label' => esc_html__( 'Yes', 'mytheme' )
),
'value' => 'no',
),
),
'text_icon' => array(
'content' => array(
'type' => 'text',
'label' => esc_html__('Content', 'mytheme'),
'value' => '',
),
'icon' => array(
'type' => 'icon-v2',
),
'move_right' => array(
'label' => esc_html__( 'Move to the right', 'mytheme' ),
'type' => 'switch',
'left-choice' => array(
'value' => 'no',
'color' => '#ccc',
'label' => esc_html__( 'No', 'mytheme' )
),
'right-choice' => array(
'value' => 'yes',
'label' => esc_html__( 'Yes', 'mytheme' )
),
'value' => 'no',
),
),
'social_icons' => array(
'content' => array(
'type' => 'hidden',
'value' => esc_html__('Social Icons', 'mytheme'),
),
'facebook_url' => array(
'type' => 'text',
'label' => esc_html__('Facebook URL', 'mytheme'),
'value' => '',
),
'twitter_url' => array(
'type' => 'text',
'label' => esc_html__('Twitter URL', 'mytheme'),
'value' => '',
),
'linkedin_url' => array(
'type' => 'text',
'label' => esc_html__('LinkedIn URL', 'mytheme'),
'value' => '',
),
'google_plus_url' => array(
'type' => 'text',
'label' => esc_html__('Google Plus URL', 'mytheme'),
'value' => '',
),
'youtube_url' => array(
'type' => 'text',
'label' => esc_html__('YouTube URL', 'mytheme'),
'value' => '',
),
'move_right' => array(
'label' => esc_html__( 'Move to the right', 'mytheme' ),
'type' => 'switch',
'left-choice' => array(
'value' => 'no',
'color' => '#ccc',
'label' => esc_html__( 'No', 'mytheme' )
),
'right-choice' => array(
'value' => 'yes',
'label' => esc_html__( 'Yes', 'mytheme' )
),
'value' => 'no',
),
),
)
),
)
),
)
)
),
)
)
)
),
)
);
p.s. is it not possible to use the same config file for Customizer and Theme Options, if i should specify fw-storate only for customizer?
it would be better to have something like this in Settings.php
$options = array(
fw()->theme->get_options( 'general' ),
'customizer_settings' => array(
'title' => esc_html__('Customizer Options', 'mytheme'),
'type' => 'tab',
'options' => array(
fw()->theme->get_options( 'customizer' ),
)
),
);
and a question regarding nested settings, if i have MultiPicker, how KEY parameter should look for child elements (please see the comment in code)?
'top_bar_enabled' => array(
'type' => 'multi-picker',
'label' => false,
'fw-storage' => array(
'type' => 'wp-option',
'wp-option' => 'fw_theme_settings_options:'. fw()->theme->manifest->get_id(),
'key' => 'top_bar_enabled',
),
'picker' => array(
'enabled' => array(
'label' => esc_html__( 'Enable Top Bar', 'mytheme' ),
'type' => 'switch',
'left-choice' => array(
'value' => 'no',
'color' => '#ccc',
'label' => esc_html__( 'No', 'mytheme' )
),
'right-choice' => array(
'value' => 'yes',
'label' => esc_html__( 'Yes', 'mytheme' )
),
'value' => 'no',
)
),
'choices' => array(
'yes' => array(
'content' => array(
'type' => 'text',
'label' => esc_html__('Content', 'mytheme'),
'value' => '',
/**
* Which key should be specified for child settings?
* "top_bar_enabled/content" or just "content"?
**/
'fw-storage' => array(
'type' => 'wp-option',
'wp-option' => 'fw_theme_settings_options:'. fw()->theme->manifest->get_id(),
'key' => 'top_bar_enabled/content',
),
),
)
)
),
This shows the correct values
add_action('admin_notices', function(){
fw_print(
fw_get_db_settings_option(),
fw_get_db_customizer_option()
);
});
but this returns the db value without processing the fw-storage param.
Fixed, please test.
@moldcraft thank you so much, it works like a charm now!
p.s. please answer on my last question
is it not possible to use the same config file for Customizer and Theme Options, if i should specify fw-storate only for customizer config?
it would be better to have something like this in Settings.php
$options = array(
/** general options config **/
fw()->theme->get_options( 'general' ),
/** settings from customizer **/
'customizer_settings' => array(
'title' => esc_html__('Customizer Options', 'mytheme'),
'type' => 'tab',
'options' => array(
/** the same config for customizer and theme options **/
fw()->theme->get_options( 'customizer' ),
)
),
);
or can i use 'fw-storage' in my code above without problems for options and customizer?
It should work fine in both options.
Thank you! xD

Where did you get my photo?
@moldcraft sorry to disturb you again with the same thing, but when i use fw-storage
'fw-storage' => array(
'type' => 'wp-option',
'wp-option' => 'fw_theme_settings_options:'. fw()->theme->manifest->get_id(),
'key' => 'top_bar_enabled',
),
both in Settings.php and Customizer.php it doesn't work, so currently it is impossible to use one config file both for customizer and settings, could you fix that please?
fw()->theme->get_options() has a second parameter in which you can pass custom variables to be available in the options files that is going to be loaded.
In settings.php you ca do
fw()->theme->get_options( 'customizer', array( 'no_fw_storage' => true ) )
then in customizer.php do
'my_option' => array(
'type' => 'text',
'fw-storage' => isset($no_fw_storage) ? null : array(
'type' => 'wp-option',
'wp-option' => 'fw_theme_settings_options:'. fw()->theme->manifest->get_id(),
'key' => 'top_bar_enabled',
),
),
Works perfectly, thank you so much again and again
@moldcraft Sorry, but after closer testing of this feature i noticed following problem:
now (if i use fw-storage for option) fw_get_db_customizer_option('top_bar_enabled') always returns saved value, and live preview in customizer is not working until i click on "save" button :(
Please test
It works now, thank you!
@moldcraft sorry that's me again
It seems now it works only for one page in Customizer, e.g. if i change header style, it changes, but when i click on any link in preview area, changes doesn't appear like i didn't change this option
also, if you have several panels in customizer:
- change some setting on first panel
- navigate to second panel and change any option, you will lost your settings in preview area from first panel
@WPJobs Please send me again a test theme with test options. I have tried on my side with some options and can't reproduce the problem.