Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If no value set – do not output css property #537

Closed
lumberman opened this issue Dec 22, 2015 · 7 comments
Closed

If no value set – do not output css property #537

lumberman opened this issue Dec 22, 2015 · 7 comments

Comments

@lumberman
Copy link

Is there a possibility to not output css property if the value is empty as shown on the screenshot: https://www.evernote.com/l/AAHOqSbXDWlJzaWorc2eacmzrQefmboArywB/image.png ?

Thanks for your time.

@lumberman
Copy link
Author

Same for colors and any other controls?

@doitmax
Copy link
Contributor

doitmax commented Dec 23, 2015

You can perform a check on the output argument of a control. I expanded the example from the Kirki wiki with an additional check on the output with an is_empty() check.

Kirki::add_config( 'dummy_config' );

Kirki::add_field( 'dummy_config', array(
    'type'     => 'slider',
    'settings' => 'my_setting',
    'label'    => __( 'Border width', 'translation_domain' ),
    'section'  => 'my_section',
    'default'  => 1,
    'priority' => 1,
    'choices'  => array(
        'min'  => 0,
        'max'  => 20,
        'step' => 1,
    ),
    'output' => ( '1' != Kirki::get_option( 'dummy_config', 'my_setting' )  && !empty( Kirki::get_option( 'dummy_config', 'my_setting' ) ) ) ? array(
        array(
            'element'  => '.variable-top-border',
            'property' => 'border-top-width',
            'units'    => 'px',
        ),
        array(
            'element'  => '.variable-bottom-border',
            'property' => 'border-bottom-width',
            'units'    => 'px',
        ),
    ) : null,
) );

This checks against the default value and also checks if the option value is empty.

Hope that helps.

@aristath
Copy link
Contributor

@pavot the solution suggested by @doitmax above is the recommended method.
The only correction I would make is using get_theme_mod() instead of Kirki::get_option():

    'output' => ( ! empty( get_theme_mod( 'my_setting' ) ) ) ? array(
        array(
            'element'  => '.variable-top-border',
            'property' => 'border-top-width',
            'units'    => 'px',
        ),
        array(
            'element'  => '.variable-bottom-border',
            'property' => 'border-bottom-width',
            'units'    => 'px',
        ),
    ) : array(),

@doitmax
Copy link
Contributor

doitmax commented Dec 23, 2015

@aristath Oh sorry, i have used the (still) wrong example from the Wiki. Of course it should be get_theme_mod().

Cheers

@aristath
Copy link
Contributor

yeah, I've completely neglected updating the wiki.... lots of things I must change there. 😟

@pasqualevitiello
Copy link

I get a Fatal error: Can't use function return value in write context in error because prior to PHP 5.5, empty() only supports variables; anything else will result in a parse error.

Probably better:

'output' =>  ( get_theme_mod( 'my_setting' ) != '' ) ? array(
        array(
            'element'  => '.variable-top-border',
            'property' => 'border-top-width',
            'units'    => 'px',
        ),
        array(
            'element'  => '.variable-bottom-border',
            'property' => 'border-bottom-width',
            'units'    => 'px',
        ),
    ) : array(),

Would you please confirm that?

@aristath
Copy link
Contributor

yes, but you can just get the value before creating the control and save it in a var for later use.
Example:

$my_setting_value = get_theme_mod( 'my_setting', 1 );
Kirki::add_field( 'dummy_config', array(
    'type'     => 'slider',
    'settings' => 'my_setting',
    'label'    => __( 'Border width', 'translation_domain' ),
    'section'  => 'my_section',
    'default'  => 1,
    'priority' => 1,
    'output' => ( ! empty( $my_setting_value ) ) ? array(
        array(
            'element'  => '.variable-top-border',
            'property' => 'border-top-width',
            'units'    => 'px',
        ),
    ) : array(),
) );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants