Carbon-fields: Can't change capability for Theme Options

Created on 31 Aug 2017  路  2Comments  路  Source: htmlburger/carbon-fields

Edit: while writing this issue I found the solution.

The current filter for changing the capability for theme_options is a bit counter intuitive.

if ( apply_filters( 'carbon_fields_' . $type . '_container_admin_only_access', true, $title, $this ) ) {
    $this->where( 'current_user_capability', '=', 'manage_options' );
}
add_filter( 'carbon_fields_theme_options_container_admin_only_access', function($bool, $title, $container) {
    $container->where( 'current_user_capability', '=', 'edit_theme_options' );

    return false;
}, 10, 3);



md5-e4c39f4ab93f8711674487a19f80ac94



if ( $theme_options_cap = apply_filters( 'carbon_fields_' . $type . '_container_admin_only_access', true, $title, $this ) ) {
    $this->where( 'current_user_capability', '=', $theme_options_cap );
}



md5-2395b6300fe588cd6942df543478f10d



add_filter( 'carbon_fields_theme_options_container_admin_only_access', function() {
    return 'edit_theme_options';
});
[type] question

Most helpful comment

Hi @erikjoling ,

The filter was added so that you would do this:

// disable for all containers
add_filter( 'carbon_fields_theme_options_container_admin_only_access', '__return_false' );

or this:

add_filter( 'carbon_fields_theme_options_container_admin_only_access', function( $enabled, $title, $container ) {
    if ( $container->get_id() === 'some_container_id' ) {
        $enabled = false; // disable only for a specific container
    }
    return $enabled;
} );

And then add your custom conditions on the container definition as you would with any other container:

Container::make( 'theme_options', ... )
    ->where( 'current_user_capability', '=', 'whatever' );

This way your conditions are not split up in different places.

All 2 comments

Hi @erikjoling ,

The filter was added so that you would do this:

// disable for all containers
add_filter( 'carbon_fields_theme_options_container_admin_only_access', '__return_false' );

or this:

add_filter( 'carbon_fields_theme_options_container_admin_only_access', function( $enabled, $title, $container ) {
    if ( $container->get_id() === 'some_container_id' ) {
        $enabled = false; // disable only for a specific container
    }
    return $enabled;
} );

And then add your custom conditions on the container definition as you would with any other container:

Container::make( 'theme_options', ... )
    ->where( 'current_user_capability', '=', 'whatever' );

This way your conditions are not split up in different places.

Ok, that makes sense. Though it isn't perfect for my use case where I just want to override the capability for carbon fields theme options.

I have a client role which has 'edit_theme_options' capability and no 'manage_options'. So I want to "lower" the cap of all carbon fields theme options to give them access.

But it works, so I'm happy. Thank you for answering my question.

Was this page helpful?
0 / 5 - 0 ratings