Unyson icon indicating copy to clipboard operation
Unyson copied to clipboard

New option types suggestion

Open onebelarusianguy opened this issue 10 years ago • 22 comments

I wrote two option types about colors and if community wants we can implement it in framework.

This is how it looks:

  1. Just predefined colors 1

  2. Predefined colors with RGBA picker to set custom color (this type depends(technically) from one above) 2

So, what do you think?

UPDATED: Well, I fixed the problem not the best way, but somehow. The problem was that when I render some option type in my Class it doesn't load their dependencies, except if we using this new Option Type for Theme Settings.

Nevertheless I'll post it for you guys. You can use it for Theme Settings/Builder/Customizer/etc. new option types.zip

INSTALL

  1. Copy archive content in {Theme} directory as is
  2. In functions.php add:
function _action_theme_include_custom_option_types() {
    require_once dirname(__FILE__) . '/inc/includes/option-types/predefined-colors/class-fw-option-type-predefined-colors.php';
    require_once dirname(__FILE__) . '/inc/includes/option-types/predefined-colors-color-picker/class-fw-option-type-predefined-colors-color-picker.php';
}
add_action('fw_option_types_init', '_action_theme_include_custom_option_types');

USAGE

  1. Predefined Colors
    array(
        'type' => 'predefined-colors',
        'label' => __('Choose Theme Color', '{domain}'),
        'value' => '', // default value
        'choices' => array(
            '#a8a8a8' => '#a8a8a8',
            'gold' => 'gold',
            'darkseagreen' => 'darkseagreen',
            '#03d9a1' => '#03d9a1',
            'coral' => 'coral',
            'tomato' => 'tomato',
            'darkkhaki' => 'darkkhaki',
            'deepskyblue' => 'deepskyblue',
            'cornflowerblue' => 'cornflowerblue',
            'hotpink' => 'hotpink',
            'crimson' => 'crimson',
            '#8b63b1' => '#8b63b1',
        ),
    ),
  1. Predefined Colors Color Picker
    array(
        'type' => 'predefined-colors-color-picker',
        'label' => __('Section Background Color', '{domain}'),
        'value' => array(
            'predefined' => '#fff', // you can set default value
            'custom' => '' // or default value for picker
        ),
        'colors' => array(
            'predefined' => array(
                'type' =>'predefined',
                'choices' => array(
                    'white' => '#fff',
                    'lightgray' => '#f6f6f6',
                    'dark' => '#0b1216',
                    'darkgray' => '#222',
                ),
            ),
            'custom' => array(
                'type' =>'custom',
                'picker' => 'color-picker', // color-picker|rgba-color-picker
            ),
        )
    ),

onebelarusianguy avatar Jun 17 '16 12:06 onebelarusianguy

@onebelarusianguy how does it work ? what options does it have?

danyj avatar Jun 17 '16 12:06 danyj

@danyj Hm... You just should specify predefined colors set in any CSS format and then just choose color and use it wherever you want through fw_get_db_settings_option('option_id')

for 1st option type it returns selected color value from predefined set: value1 for 2nd option type it returns array with one empty value and one set (depends from which one is choosen): value

Here is how it calls: 1) 11

22

P.S. Actually we can get the same result through color picker with palettes, but if you have 12 predefined colors, for example, it will look ugly

onebelarusianguy avatar Jun 17 '16 12:06 onebelarusianguy

@onebelarusianguy : nice idea, does it work in Customizer?

dylan-ngo11 avatar Jun 18 '16 04:06 dylan-ngo11

@duongancol yes, it should. I wrote it for my Theme Options needs and it works well, BUT when I tried use it in Page Builder or Customizer my 2nd options type not works(to be clear color picker not works). Now I'm trying understand what's wrong with it.

onebelarusianguy avatar Jun 18 '16 08:06 onebelarusianguy

Looks nice. If possible share with us.

Ataurr avatar Jun 18 '16 08:06 Ataurr

@danyj , @duongancol , @Ataurr , check first post, shared.

onebelarusianguy avatar Jun 18 '16 14:06 onebelarusianguy

Thanks @onebelarusianguy

Niloys7 avatar Dec 18 '16 11:12 Niloys7

Thanks @onebelarusianguy! I've added this to my theme.

jonmlas avatar Mar 25 '17 01:03 jonmlas

@onebelarusianguy Tnx for sharing! I used your options in my theme, but not truly understood how to get values from 2-d variant and why i need array instead of only one (chosen by user) value

Now I'm using this:

$background_color = fw_get_db_settings_option('background_type_main/boxed/background_color/predefined').fw_get_db_settings_option('background_type_main/boxed/background_color/custom');

Is there a better way? Or maybe, I don't understand some cases?

One more tnx!

sollerpolt avatar Nov 21 '17 12:11 sollerpolt

Hi @sollerpolt , check this comment https://github.com/ThemeFuse/Unyson/issues/1654#issuecomment-226757353

Niloys7 avatar Nov 21 '17 13:11 Niloys7

@NiloySarker

I read that comment, but can't understand why I need array with 2 items, one of witch always empty.

I think will be better, if fw_get_db_settings_option('option_id') will return selected color value, but not an array. I'm newbie in Unyson, so may not understand developer. But want to know more.

sollerpolt avatar Nov 21 '17 13:11 sollerpolt

Basically, this option has 2 parameters. which is "predefined" & "custom" so once user will select "predefined" then the value will load from "predefined" List and when user will select "custom" then the value will load from "custom" field. That's why this option is in an Array. Also if you don't need "custom" parameter then don't use it. this code has two option type. one has only "predefined" type and the other one have both 2 types "predefined" & "Custom". and for the first option you actually can use fw_get_db_settings_option('option_id') and the 2nd option type is something like unyson "image-picker" option. (Array) Hope it helps to understand.

Niloys7 avatar Nov 21 '17 14:11 Niloys7

@sollerpolt as @NiloySarker said, if you intend to use only predefined colors, then use 1st option type, it will return only one value. 2nd option type should be used when you want to use color picker and want to know which value was set 'predefined' or 'custom'(from color picker). Use case: for example, if it was 'predefined' then I will include external CSS file with this color scheme, if it was set through color picker I will do something else. Hope it was clear.

onebelarusianguy avatar Nov 21 '17 14:11 onebelarusianguy

@NiloySarker

I understand all this. I just don't know why it's designed so? Is it limitation of Unyson? Or author had some reasons to do it so?

Because now I'm getting array with one value instead of value itself. And need to run two fw_get_db_settings_option() instead of one.

In ideal world calling fw_get_db_settings_option('option_id') will return only selected value (no matter is it predefined or custom), not array.

"image-picker" option return useful array (url and id, if I remember correctly).

sollerpolt avatar Nov 21 '17 14:11 sollerpolt

@sollerpolt Why do you need to run fw_get_db_settings_option() twice? You don't need it.

onebelarusianguy avatar Nov 21 '17 14:11 onebelarusianguy

@onebelarusianguy Tnx! Now i understood.

You will help me a lot if show ideal way to get your option value. Now I'm using:

$background_color = fw_get_db_settings_option('background_type_main/boxed/background_color/predefined').fw_get_db_settings_option('background_type_main/boxed/background_color/custom');

This twice call get one empty value and one, selected by user. As a result, i get value, not an array (so I don't need check which one empty and which not). Maybe exist some right way to do this?

And it's look terrible.

sollerpolt avatar Nov 21 '17 14:11 sollerpolt

@sollerpolt Okay, you need this construction:

if (defined('FW')) {

	//get the value. 'background_color' is name of option. But you passed something else
	$background_color = fw_get_db_customizer_option('background_color');
	if ( !empty($background_color['predefined']) ) {
		//do something
	} elseif ( !empty($background_color['custom']) ) {
		//do something else
	}

}

and this option something like:

'options' => array(
	'background_color' => array( //USE THIS OPTION NAME
		'type' => 'predefined-colors-color-picker',
		'label' => __('Choose Theme Color', 'nowadays'),
		'value' => array(
			'predefined' => '',
			'custom' => '',
		),
		'colors' => array(
			'predefined' => array(
				'type' =>'predefined',
				'choices' => array(
					'#a8a8a8' => '#a8a8a8',
					'#ffd700' => '#ffd700',
					'#8fbc8f' => '#8fbc8f',
					'#03d9a1' => '#03d9a1',
					'#ff7f50' => '#ff7f50',
					'#ff6347' => '#ff6347',
					'#bdb76b' => '#bdb76b',
					'#00bfff' => '#00bfff',
					'#6495ed' => '#6495ed',
					'#ff69b4' => '#ff69b4',
				),
			),
			'custom' => array(
				'type' => 'custom',
				'picker' => 'color-picker', // color-picker|rgba-color-picker
			),
		),
	),

),

P.S. I used it in Customizer. That's why I call fw_get_db_customizer_option, but you can use fw_get_db_settings_option

onebelarusianguy avatar Nov 21 '17 14:11 onebelarusianguy

Tnx for your help.

I just want to print this variable. So, as you show, my code can look like this:

$background_color = fw_get_db_customizer_option('background_type_main/boxed/background_color');
	if ( !empty($background_color['predefined']) ) {
		$background_color = $background_color['predefined'];
	} elseif ( !empty($background_color['custom']) ) {
		$background_color = $background_color['custom'];
	}
echo $background_color;

Is calling if/elseif for array faster/more optimized, then one more fw_get_db_customizer_option. How can I check it?

I passing path to my variable. I'm using your option inside "multi-picker" with name background_type_main and option boxed . Maybe, exist some better way to get nested options?

Sorry for multiple entry-level questions.

sollerpolt avatar Nov 21 '17 15:11 sollerpolt

@sollerpolt Definitely it faster!

Your last example is correct - you should specify path just to 'background_color', not to 'background_color/predefinedORcustom'.

onebelarusianguy avatar Nov 21 '17 15:11 onebelarusianguy

@onebelarusianguy I will trust you. Tnx for a lot of help.

Maybe you know some resources, where other developer share some staff for Unyson? Option types, sliders, some useful code examples. I hope, you are not only one.

sollerpolt avatar Nov 21 '17 15:11 sollerpolt