Title: Adding attribute to menu items
Last modified: December 18, 2020

---

# Adding attribute to menu items

 *  Resolved [koolmind](https://wordpress.org/support/users/koolmind/)
 * (@koolmind)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/adding-attribute-to-menu-items/)
 * Good evening everybody. I need to add a custom attribute to my menu items.
 * This is what I believe I have to do… but nothing is added.
 *     ```
       add_filter( 'nav_menu_link_attributes', 'add_role_attribute_to_menu' );
       function add_role_attribute_to_menu( $atts ) {
         $atts['role'] = 'button';
         return $atts;
       }
       ```
   
 * I’m using WP 5.6 and Astra free theme.
 * What’s the matter? Maybe I have to include the add_filter inside another call?
 * Thanks

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Support [Team Brainstorm Force](https://wordpress.org/support/users/brainstormteam/)
 * (@brainstormteam)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/adding-attribute-to-menu-items/#post-13822051)
 * Hello [@koolmind](https://wordpress.org/support/users/koolmind/)
 * There is some syntactical mistake in the above filter.
 * Kindly check this below filter:
 *     ```
       add_filter( 'hfe_nav_menu_attrs', 'add_role_attribute_to_menu' );
       function add_role_attribute_to_menu( $atts ) {
           $atts .= ' role=' . 'button';
           return $atts;
       }
       ```
   
 * Kindly refer to the [screenshot](https://a.cl.ly/9Zu0ongK)( at our end )
 * Please let us know in case of any further help.
 * Regards,
    Rahul.
 *  Thread Starter [koolmind](https://wordpress.org/support/users/koolmind/)
 * (@koolmind)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/adding-attribute-to-menu-items/#post-13822403)
 * Thank you very much!
    Is there a way to apply the filter only on first level 
   menu items? Can I only work on $atts variable, or there’s the chance to get also
   the a $item variable to work with?
 * Thanks again.
 *  Plugin Support [Team Brainstorm Force](https://wordpress.org/support/users/brainstormteam/)
 * (@brainstormteam)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/adding-attribute-to-menu-items/#post-13826047)
 * Hello [@koolmind](https://wordpress.org/support/users/koolmind/)
 * It won’t be possible to apply the attributes to the first level item only.
 * Also, only attributes can be changed, not the items.
 * Please let us know in case of any further help.
 * Regards,
    Rahul.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Adding attribute to menu items’ is closed to new replies.

 * ![](https://ps.w.org/header-footer-elementor/assets/icon-256x256.gif?rev=3278750)
 * [Ultimate Addons for Elementor](https://wordpress.org/plugins/header-footer-elementor/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/header-footer-elementor/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/header-footer-elementor/)
 * [Active Topics](https://wordpress.org/support/plugin/header-footer-elementor/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/header-footer-elementor/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/header-footer-elementor/reviews/)

## Tags

 * [attributes](https://wordpress.org/support/topic-tag/attributes/)
 * [menu](https://wordpress.org/support/topic-tag/menu/)

 * 3 replies
 * 2 participants
 * Last reply from: [Team Brainstorm Force](https://wordpress.org/support/users/brainstormteam/)
 * Last activity: [5 years, 5 months ago](https://wordpress.org/support/topic/adding-attribute-to-menu-items/#post-13826047)
 * Status: resolved