Want to add a custom attribute like target="foobox"
or data-featherlight="#enews-popup"
to a specific nav menu item’s anchor?
nav_menu_link_attributes filter hook in WordPress is your friend.
For example, adding
/**
* Add custom attribute and value to a nav menu item's anchor.
*
* @author Sridhar Katakam
* @link https://sridharkatakam.com/
*/
add_filter( 'nav_menu_link_attributes', function ( $atts, $item, $args ) {
if ( 1161 === $item->ID ) { // change 1161 to the ID of your menu item.
$atts['target'] = 'foobox';
}
return $atts;
}, 10, 3 );
in child theme’s functions.php will add target="foobox"
to the menu item 1161’s a element.
Replace 1161
with the ID of your menu item. This can be obtained by inspecting it using Chrome’s Developer Tools.
Update on March 06, 2017:
Here’s a better version which does not rely on entering the menu item ID:
Simply assign a class to the menu item, say target-foobox
like this:
and use this code:
/**
* Add custom attribute and value to a nav menu item's anchor based on CSS class.
*
* @author Sridhar Katakam
* @link https://sridharkatakam.com/
*/
add_filter( 'nav_menu_link_attributes', function ( $atts, $item, $args ) {
if ( 'target-foobox' === $item->classes[0] ) {
$atts['target'] = 'foobox';
}
return $atts;
}, 10, 3 );
I have a follow on idea – make the code flexible by getting it to search $item->classes for certain classes and adding them to $atts[‘target’] if found.
For example, if one of the classes is ‘target-foobox’ then set $atts[‘target’] = ‘foobox’.
I’ll do up a gist tomorrow.
https://gist.github.com/damiencarbery/d0bd59e34520aa37b87924a04a87847e
`<?php
add_filter( 'nav_menu_link_attributes', function ( $atts, $item, $args ) {
$prefix = 'target';
$target_items = preg_grep( "/^${prefix}-/", $classes );
if ( $target_items ) {
foreach( $target_items as $target_item ) {
$value = substr( $target_item, strlen( $prefix.'-' ) );
$atts[$prefix] = $value;
}
}
return $atts;
}, 10, 3 );`
I think that this could be further extended by creating an array of prefixes and looping through them.
Hey Demien, pardon my intrusion, I know this is an old post…
but I’m looking into how insert data attributes (where none) to specific html elements.
I’d like to append data attributes and classes to a theme with a function in the function.php to facilitate customisations without touching the theme php’s files.
would you know how to achieve this?
Cheers for any help or direction you could give!
Lorenzo – Can you give me some examples of the html elements you may want to change? Are they in the navigation? Are they in a Genesis theme?
Hey Damien, thanks for getting back to me…
I’m using Divi on WordPress and I’m not finding any useful info on how to parallax an img attached to the body element.
So I decided to custom made my parallax implementation… you can find different ways of doing it, but all of them require either change the body classes or data-attributes.
They are all great implementation if you are coding the html or php yourself but if you use a template it means that you have to put your hands in the theme template files…which I rather not do, not for fear but for good practice.
….and I’m not fluent in JS or WP hooks so I’m looking for a solution that keeps the template clean from modifications via function.php which could be implemented regardless the theme I’m using.
Again, thanks for your time!
Ah, Divi. From a quick search it looks like parallax is available for Divi
https://www.elegantthemes.com/blog/divi-resources/ultimate-guide-to-using-parallax-with-divi – this looks most useful.
https://www.elegantthemes.com/blog/divi-resources/how-to-add-parallax-elements-and-a-slide-down-transition-to-the-divi-slider-module
Changing the body class is easy – see the examples at: https://developer.wordpress.org/reference/functions/body_class/
To add a data attribute to an element you could use ‘the_content’ filter: https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content
You would have to use a regular expression to find the element and then add the data attribute.
Thank you for your time Damien.
with Divi you can’t apply parallax effects on the body of the page out of the box… but the last few links you posted look really promising… looks like I’m up for some Divi hacking!
Cheers again!
Is this for a Genesis theme?
No, a later comment mentions Divi theme. :shudder:
I’ve updated the post with a simpler way to implement this idea based on the menu item’s CSS class.
Hello, I tried to use foobox with a menu item but it is not working.
I use in wordpress-menu- css class – target-foobox
and put your code inside code snippet plugin
/**
* Add custom attribute and value to a nav menu item’s anchor based on CSS class.
*
* @author Sridhar Katakam
* @link https://sridharkatakam.com/
*/
add_filter( ‘nav_menu_link_attributes’, function ( $atts, $item, $args ) {
if ( ‘target-foobox’ === $item->classes[0] ) {
$atts[‘target’] = ‘foobox’;
}
return $atts;
}, 10, 3 );
and activate it.
But the menu is not oopen inside foobox.
Do you have a idea why?
Thanks for answering
Oliver – Can you upload a screenshot of your menu item from the admin area (like Sridhar has provided in the post – upload to https://imgur.com/ or similar)?
Hi.
Thanks for the Idea with the menu class!
For my usecase I need to add different targets to different menu items, so I extended your code to check if the first css class contains “target” and then to use the part after “target-” as the target:
add_filter( 'nav_menu_link_attributes', function ( $atts, $item, $args ) {
if ( strpos($item->classes[0], 'target') !== false ) {
$atts['target'] = str_replace('target-','',$item->classes[0]);
}
return $atts;
}, 10, 3 );
Nice. I edited your comment and wrapped your code in three backticks so it appears properly.
It’s funny how I was searching for an hour how to do this and then found it on your site, explained so great and it worked perfectly in my first try – thanks!
Thank you, it works !
Do you know how I can make this work with my custom Walker? It works with the default one, but as soon as I activate my custom walker it stops working 🙁
add_filter( ‘nav_menu_link_attributes’, function ( $atts, $item, $args ) {
$menuItems = $item->classes;
if ( strpos($menuItems[0], ‘toggle’) !== false ) {
foreach( $menuItems as $menuItem ){
if(substr($menuItem , 0, 5) === ‘data-‘){
$prefix = strtok( $menuItem, ‘_’ );
$suffix = strtok( ” );
$atts[$prefix] = $suffix;
}
}
}
return $atts;
}, 10, 3 );
Menu classes to be added ( toggle data-toggle_collapse data-_ ) on wordpress menu
this will add all the data attributes to the menu
Is it possible, to use Oxygen Builder’s Mega Menu element (Composite), and trigger it with a regular WordPress menu element? So I don’t need to use the “oxel_megamenu_parent” classed button? I would use it like this because I would like to use several mega menus, and It is easier to have a simple menu element (for mobile usage reasons).
Thanks for the help
Yes. Replied to your email.