0

I have a menu that I created in Appearance -> Menus. One of it's items is the company phone number which is set in the theme customizer. I have a shortcode that will return that number:

/* shortcode to display phone number  */
function getPhone()
{
    return get_theme_mod( 'contact_phone_data');;
}

/* Add shortcode */
add_shortcode('contact_phone', 'getPhone');

Is there any way to use this in the Appearance -> Menus UI? when I enter [contact_phone] into the menu item's 'Navigation Label' field, it (as expected) displays the actual string rather than executing the shortcode.

Is there a way to indicate that the value of that field should be executed as a shortcode? If not, is there a standard way that this type of thing is handled in WP?

2
  • the admin page "menu" is more for editing menu elements. then if you want to add an information next to the menu, it's better to add it in a theme file.
    – Kaperto
    Nov 11, 2019 at 16:32
  • .Sorry, I'm not quite sure what you mean by a "theme file"? The phone number itself actually IS the menu item. In my case, it's the last item in the menu, so I guess I can take it out and add it right next to the menu in the template file, but what if it had to go in the middle of the menu? Same with the hyperlink on the menu item... how can one go about accessing that number from the theme settings so that it will update accordingly when the user sets/changes that value in the theme customizer?
    – Daveh0
    Nov 11, 2019 at 16:51

1 Answer 1

0

Someone posted a similar question in the Advanced WordPress facebook group. I suggested adding some token text to the menu item's url and then swapping it out during render.

add_filter( 'nav_menu_link_attributes', 'dcwd_nav_menu_link_attributes', 10, 4 ); 
function dcwd_nav_menu_link_attributes( $atts, $item, $args, $depth ) {
    // If PHONE_NUMBER is found then change it.
    if ( false !== strpos( $atts[ 'href' ], 'PHONE_NUMBER' ) ) {
        $atts[ 'href' ] = getPhone();
    }
    return $atts;
}

I also posted it to pastebin

5
  • looks like it should work, but the link is not changing. I'm not positive the function is ever getting called as the debug statements (included in your pastebin version) are not logging to /wp-content/debug.log. Is it possible that hook doesn't exist in my theme (purchased from Envato)? any other ideas as to what it may be?
    – Daveh0
    Nov 11, 2019 at 22:20
  • It is quite possible that your theme is not using the default Walker class to generate the menu. Look for the wp_nav_menu() call to see if the args passed specify 'walker' (to direct it to use a custom walker class. See: [developer.wordpress.org/reference/functions/wp_nav_menu/] (wp_nav_menu) Nov 12, 2019 at 12:10
  • If the theme is using a custom walker class and the doesn't have useful filters then you might have to use the 'wp_nav_menu_items filter which gives you the menu markup that you could parse. Nov 12, 2019 at 13:43
  • ok this is weird. This (and other filters using the wp_nav_menu_items hook) started working, but each not for several hours after I added them to functions.php. For example, I added a filter that changed the href of 1 menu item at 2pm yesterday and it just started showing up now. Similarly, I applied a filter that appended some text to each menu item early yesterday morning and that extra text didn't start showing up until last night... and now I can't get remove_filter() to take immediate effect either and I'm stuck with the unwanted, appended text. What could this be? Cache-ing issue?
    – Daveh0
    Nov 13, 2019 at 10:17
  • That does sound like caching. If you don't have a caching plugin it might be at server level (hosting support should be able to help you disable it). Developing in such an environment sounds painful and very frustrating. You could echo a string via wp_head action (changing it a few times) to see whether caching is likely the issue. Nov 14, 2019 at 13:53

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.