Discussion:
Shortcodes in Custom Menu Items
Jesse Friedman
2013-05-30 15:19:20 UTC
Permalink
Is it possible to put a shortcode like [siteURL]/about-us in a custom menu
link?

I know it isn't out of the box, but it doesn't look as easy as enabling
shortcodes in widgets and I can't find any documentation.
--
thanks

*jesse friedman*
jes.se.com *
*
Book: Web Designers Guide to WordPress -
http://wdgwp.com/onamazon<http://wdgwp.com/onamazon>
Twitter: @professor <http://twitter.com/professor>
Facebook: Like<https://www.facebook.com/pages/Jesse-Friedman/204793299545174>
Nicholas Ciske
2013-05-30 15:28:05 UTC
Permalink
This will do it:

add_filter('wp_nav_menu', 'do_menu_shortcodes');
function do_menu_shortcodes( $menu ){
return do_shortcode( $menu );
}
_________________________
Nick Ciske
http://thoughtrefinery.com/
@nciske
Post by Jesse Friedman
Is it possible to put a shortcode like [siteURL]/about-us in a custom menu
link?
I know it isn't out of the box, but it doesn't look as easy as enabling
shortcodes in widgets and I can't find any documentation.
--
thanks
*jesse friedman*
jes.se.com *
*
Jesse Friedman
2013-05-30 15:44:50 UTC
Permalink
Hi Nick

I appreciate the help, yet it doesn't work. While it may return and do the
shortcode, the actual saving of the custom link strips the [ ] and you're
left with http://siteURL/about-us rather than http://[siteURL]/about-us
Post by Nicholas Ciske
add_filter('wp_nav_menu', 'do_menu_shortcodes');
function do_menu_shortcodes( $menu ){
return do_shortcode( $menu );
}
_________________________
Nick Ciske
http://thoughtrefinery.com/
@nciske
Post by Jesse Friedman
Is it possible to put a shortcode like [siteURL]/about-us in a custom
menu
Post by Jesse Friedman
link?
I know it isn't out of the box, but it doesn't look as easy as enabling
shortcodes in widgets and I can't find any documentation.
--
thanks
*jesse friedman*
jes.se.com *
*
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
--
thanks
*
*
*jesse friedman*
jes.se.com *
*
Book: Web Designers Guide to WordPress -
http://wdgwp.com/onamazon<http://wdgwp.com/onamazon>
Twitter: @professor <http://twitter.com/professor>
Facebook: Like<https://www.facebook.com/pages/Jesse-Friedman/204793299545174>
Nicholas Ciske
2013-05-30 15:49:53 UTC
Permalink
I tested the code and it worked fine for me... [shortcode] showed up (using a custom menu item), then the shortcode output showed up when I wired it to a function.

Something outside core (theme or plugin) is filtering your menus and stripping out the [].

You could also do %SITEURL% or {SITEURL} (or whatever does not get stripped, even just SITEURL) and use str_replace instead of a shortcode solution.

_________________________
Nick Ciske
http://thoughtrefinery.com/
@nciske
Post by Jesse Friedman
Hi Nick
I appreciate the help, yet it doesn't work. While it may return and do the
shortcode, the actual saving of the custom link strips the [ ] and you're
left with http://siteURL/about-us rather than http://[siteURL]/about-us
Jesse Friedman
2013-05-30 21:07:23 UTC
Permalink
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.
Post by Nicholas Ciske
I tested the code and it worked fine for me... [shortcode] showed up
(using a custom menu item), then the shortcode output showed up when I
wired it to a function.
Something outside core (theme or plugin) is filtering your menus and stripping out the [].
You could also do %SITEURL% or {SITEURL} (or whatever does not get
stripped, even just SITEURL) and use str_replace instead of a shortcode
solution.
_________________________
Nick Ciske
http://thoughtrefinery.com/
@nciske
Post by Jesse Friedman
Hi Nick
I appreciate the help, yet it doesn't work. While it may return and do
the
Post by Jesse Friedman
shortcode, the actual saving of the custom link strips the [ ] and
you're
Post by Jesse Friedman
left with http://siteURL/about-us rather than http://[siteURL]/about-us
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
--
thanks
*
*
*jesse friedman*
jes.se.com *
*
Book: Web Designers Guide to WordPress -
http://wdgwp.com/onamazon<http://wdgwp.com/onamazon>
Twitter: @professor <http://twitter.com/professor>
Facebook: Like<https://www.facebook.com/pages/Jesse-Friedman/204793299545174>
Nicholas Ciske
2013-05-30 21:14:46 UTC
Permalink
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
Post by Jesse Friedman
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.
Nicholas Ciske
2013-05-30 21:28:11 UTC
Permalink
Oops, had an extra http:// in there due to the way URLs get sanitized.

Moving the code to a gist:
https://gist.github.com/nciske/5681384

Out of curiosity, why not just use a relative URL like /test/ vs inserting the site url in front?

_________________________
Nick Ciske
http://thoughtrefinery.com/
@nciske
Post by Nicholas Ciske
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.
Jesse Friedman
2013-05-30 21:45:17 UTC
Permalink
Thanks for this Nicholas, that's what i needed.

The reason is I want to be able to do http://subdomain._siteURL_/about-us
so that i can link to subdomain sites that are prelive
Post by Nicholas Ciske
Oops, had an extra http:// in there due to the way URLs get sanitized.
https://gist.github.com/nciske/5681384
Out of curiosity, why not just use a relative URL like /test/ vs inserting
the site url in front?
_________________________
Nick Ciske
http://thoughtrefinery.com/
@nciske
Post by Nicholas Ciske
add_filter('wp_nav_menu', 'menu_shortcodes');
function menu_shortcodes( $menu ){
return str_replace( '_SITEURL_', home_url(), do_shortcode( $menu )
);
Post by Nicholas Ciske
}
This will allow you to have site agnostic URLs and shortcodes in labels.
Tweak as needed.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
--
*Jesse Friedman
**Director of Web Interface and Development | Astonish | **
www.astonish.com
*300 Metro Center Boulevard Warwick, RI 02886
(o) 401-921-6220 | (f) 401-921-6225 | (d) 401-773-7422 |
***@astonish.com
Loading...