themeum / kirki

Extending the customizer
https://kirki.org
MIT License
1.26k stars 328 forks source link

Call to a member function check_capabilities() #458

Closed solarmanawm closed 8 years ago

solarmanawm commented 8 years ago

Hello, thanks for a great work on this toolkit. During my theme development I've met a strange thing. I'm adding fields using Kirki API like this:

    Kirki::add_field( 'config_name', array(
        'type'        => 'radio-buttonset',
        'settings'    => 'theme_options[h4-text-transform]',
        'label'       => __( 'H4 Text Transform', 'textdomain' ),
        'description' => __( 'Choose h4 text transform.', 'textdomain' ),
        'help'        => __( '', 'textdomain' ),
        'section'     => 'body-typography',
        'default'     => 'uppercase',
        'priority'    => 10,
        'choices'     => array(
            'capitalize'   => 'capitalize',
            'lowercase' => 'lowercase',
            'uppercase'  => 'uppercase',
            'none'  => 'none',
        ),
    ) );

And it is ok in general. Except with the 'background' field type. I mean this code:

    Kirki::add_field( 'config_name', array(
        'type'        => 'background',
        'settings'    => 'theme_options[body-background]',
        'label'       => __( 'Background', 'textdomain' ),
        'description' => __( 'Body background with image, color, etc.', 'textdomain' ),
        'help'        => __( '', 'textdomain' ),
        'section'     => 'body-background',
        'default'     => array(
            'color'    => '#f5f5f5',
            'image'    => '',
            'repeat'   => 'no-repeat',
            'size'     => 'cover',
            'attach'   => 'fixed',
            'position' => 'left-top',
            'opacity'  => 100,
        ),
        'priority'    => 10,
        // 'output'      => 'body',
    ) );

will cause an error:

Fatal error: Call to a member function check_capabilities() on a non-object in /home/solarman/public_html/wpnew/wp-includes/class-wp-customize-control.php on line 283

It seems the error occurs when I'm trying to use the 'settings' property as an array with any 'background' field type. When i'm doing this:

'settings'    => 'body-background',

all is fine. What can be the problem? Thanks.

aristath commented 8 years ago

@solarmanawm can you please also post the configuration you're using for config_name ?

solarmanawm commented 8 years ago
    Kirki::add_config( 'config_name', array(
        'capability'    => 'edit_theme_options',
        'option_type'   => 'option',
    ) );
aristath commented 8 years ago

Instead of writing your settings like this: 'settings' => 'theme_options[h4-text-transform]', You could write them like this: 'settings' => 'h4-text-transform', and then have your config look like this:

Kirki::add_config( 'config_name', array(
    'capability'    => 'edit_theme_options',
    'option_type'   => 'option',
    'option_name'   => 'theme_options',
) );

This way, all the fields you create using the config_name id will be saved like theme_options[$field_id]

Let me know if that helps!

solarmanawm commented 8 years ago

Well... No changes. Still have that error.

solarmanawm commented 8 years ago

I have a solution. I can use several field types instead of one complex. It is a bit more code but it works.

aristath commented 8 years ago

@solarmanawm yeah, the background field is a bit buggy and in fact I've wanted to deprecate it for quite some time now. I've removed it from the documentation so hopefully fewer people will be using it in the future. We have plans to add a new background field that will be saving its values as an array in the future instead of creating other settings like it does now. But that's a discussion for another time... :+1: