0

I am working on a theme where I would like to allow users to specify additional options for nav menu items.

Out of the box, Users can just specify css classes that are applied to LI elements in a nav menu. However, I want to allow to specify css classes for links too.

I have looked at different options. I could use the options framework or I could use the customizer API without any plugin dependency. My problem with both of those approaches is that I am required to introduce a new customization menu section that deals with menus. I dont want to do that, because there already is the menu customizer in the backend. All I want to do is to add one more option for link css classes.

So far I have not found a way to hook into wordpress' own customizer without the risk of breaking when WP gets upgdated.

Can anybody point me into the right direction how I could approach this problem?

thanks.

2
  • Why are you afraid of WordPress updates? Hooks will not go with the WordPress update.
    – prosti
    Nov 3, 2016 at 10:52
  • Hey prosti, thats true. With "breaking" i meant editing the original menu editor. I did not mean that hooks are going to break. I should have made myself more clear.
    – DucatiNerd
    Nov 3, 2016 at 17:50

1 Answer 1

0

You can do this two ways.

  1. Add new section to existing nav_menus panel and then add your controls to the section:

$wp_customize->add_section('my_section', array(
    'title'    => 'My options',
    'panel' => 'nav_menus'
));
$wp_customize->add_setting('my_setting', array(
    'type'              => 'theme_mod',
    'settings'          => 'my_section',
    'capability'        => 'edit_theme_options',
    'sanitize_callback' => 'sanitize_my_setting'
));
$wp_customize->add_control('my_control', array(
    'label'   => 'My option',
    'section' => 'my_section',
    'type'    => 'text'
));


2. Add new controls to existing menu_locations section:


wp_customize->add_setting('my_setting', array(
    'settings'          => 'menu_locations',
    'capability'        => 'edit_theme_options',
    'sanitize_callback' => 'sanitize_my_setting'
));

$wp_customize->add_control('my_control', array(
    'label'   => 'My Option',
    'section' => 'menu_locations',
    'type'    => 'text'
));

If I were you I would choose first solution because it makes Customizer more cleaner but it depends on your needs. I hop it will help

1
  • Hi Sebastian, I've already tried to add my options to the 'nav_menus' panel like you suggested. Unfortunately I could not get it to work. Might there be a priority issue? The options wont appear for some reason.
    – DucatiNerd
    Nov 3, 2016 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.