Use ACF options page with Carbon Fields options page

Use ACF options page with Carbon Fields options page

So yeah, this is a bit confusing, but it is a special case, because ACF’s repeater & flexible was too visually complex to use, so I decided to use Carbon Fields’s options page instead (I wrote about CF here – in Romanian – a while back).

Anyhow, using them separately? All fine. But putting all in the same menu and have ACF pages first? Well, that’s a totally different story! And apparently I’m the only one that’s crazy enough to use both plugins at once – or the only one that had this probel? – hence this article.

Registering menus§

So you need to do the following (also, read the comments):

add_action('carbon_fields_register_fields', function () {
  // Create a dummy container, in order to have a page to place ACF's fields.
  // The container label is not that important, but the slug is.
  Container::make('theme_options', __('My Settings Page', 'textdomain'))
    ->set_page_file('my-top-page-tmp'); // this is the slug page and it's important!

  // Create the Carbon options page as usual.
  Container::make('theme_options', __('My Carbon Settings', 'textdomain'))
    ->set_page_parent('my-top-page-tmp') // same slug as above. I told you it's important!

  // Register ACF options page.
    'page_title' => __('My Settings Page', 'textdomain'),
    'parent_slug' => 'my-top-page-tmp', // same slug!
}, 99);

This is all good (assuming you’re using PHP 5.6+, btw). But how do we reorder the menu items and having ACF pages first?

Let’s shuffle some arrays a bit!§

add_action('admin_menu', function () {
  global $submenu;

  // We first remove the dummy container.

  // Then we move the first menu item - Carbon's Page - to the end of the array.
  $submenu['my-top-page-tmp'][] = array_shift($submenu['my-top-page-tmp']);
}, 99);

And that’s about all.

Sure enough, you can change and put them in any order, but that’s what I needed. Do you have a better method to achieve this? If so, please leave a comment and let me know!

Adaugă un comentariurăspuns pentru

Poți adăuga bucăți de cod folosind [code]codul tău aici[/code], [js][/js], [php][/php] etc.

Link-urile în context sunt binevenite. Comentariile fără nume/email valid sunt șterse.
PS: Comentariul NU este editabil.

Acest site folosește Akismet pentru a reduce spamul. Află cum sunt procesate datele comentariilor tale.

Site-ul utilizează cookie-uri. Continuarea navigării presupune acceptarea lor. Mai multe informații.

windows apple dropbox facebook twitter