Carbon-fields: custom post types not yet registered on carbon_register_fields

Created on 24 Jan 2017  路  2Comments  路  Source: htmlburger/carbon-fields

In a plugin I let the user decide for which post types he wants to enable the custom meta boxes. For that I use get_post_types and use it to populate the options of the set field.
In previous versions of carbon fields this worked without problems. Now, with the new implementation of carbon_register_fields action, my custom post types are not registered yet and obviously do not show up as an option.
When manually changing the priority in the constructor of Helper and playing with the priority number, the post types show up.

add_action( 'init', array( $this, 'trigger_fields_register' ), 20 );

I see no possibility to manually change the priority without changing it in the plugin core.

Did I do something wrong? Can I use another action hook?

[type] question

Most helpful comment

Thank you very much for the tip. Indeed that solves my problem. I really appreciate your help on this one. Thanks!

All 2 comments

When loading options for use into fields such as Select, Set etc. it is always a good idea to pass a callable instead of the final array. This way the options are only loaded when they are needed and not on every page load. The additional benefit in your case will be that they are not loaded when you register the fields but when they are prepared for output (much later than the init action).

Here's an example:

function get_select_options() {
    $options = ...
    return $options;
}

...

Field::make( 'select', 'select_field' )
    ->set_options( 'get_select_options' )

Thank you very much for the tip. Indeed that solves my problem. I really appreciate your help on this one. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jonwaldstein picture jonwaldstein  路  3Comments

abdusfauzi picture abdusfauzi  路  3Comments

leurdo picture leurdo  路  3Comments

emohamed picture emohamed  路  3Comments

kaleidoscopique picture kaleidoscopique  路  3Comments