[wp-hackers] Shortcodes in Custom Menu Items

Nicholas Ciske nl at thoughtrefinery.com
Thu May 30 21:14:46 UTC 2013


Oh bugger, I put it in the label field -- sorry! The URL must get sanitized on save which would remove [] as they are not valid characters for a domain.

So, SITEURL with str_replace appears to be the way to go as that should be legal and never occur in a real URL. You could use _SITEURL_ to be extra safe.

add_filter('wp_nav_menu', 'menu_shortcodes');
function menu_shortcodes( $menu ){
	return str_replace( '_SITEURL_', home_url(), do_shortcode( $menu ) );
}

This will allow you to have site agnostic URLs and shortcodes in labels. Tweak as needed.

I tested this one and it worked with WP 3.5.1 and a Genesis theme. Can't imagine it won’t work for you.

_________________________
Nick Ciske
http://thoughtrefinery.com/
@nciske



On May 30, 2013, at 4:07 PM, Jesse Friedman wrote:

> To be clear you're able to put the shortcode in the URL Field of a Custom
> Link in the menus section of the admin?
> 
> My shortcodes are being stripped on save and I even tried it with a fresh
> WordPress 3.5.1 install, with no plugins running 2012.



More information about the wp-hackers mailing list