• artroyer

    (@artroyer)


    I am new to WordPress, and am trying to develop a site for a local non-profit. I need to put a link in a menu. This link goes to another area of the same website. I don’t want to have to go into all my menus and change the links as I move the site from my development to test to production environments. I tried downloading the “URL Shortcodes” plugin, and that didn’t work. The link field didn’t recognize the url_base shortcode, and insisted on putting an https:// on the front. Is there a way I can implement this?

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • JNashHawkins

    (@jnashhawkins)

    The URL Shortcodes is a good idea but that won’t work except in custom coded Menus and you probably don’t need or want to go there.

    The native menu builder in WordPress might build your menu links ‘relative’ to the WordPress location if you drag and drop items from the lefthand panes to the menu itself. It will try to do that by making use of your permalinks.

    Custom menu items are almost always full links.

    I don’t trust either system or my memory completely so I install the Broken Link Checker plugin to double check everything and give me a warning that I can work with to then fix any broken items.

    https://www.ads-software.com/plugins/broken-link-checker/

    Running the Broken Link Checker at its native 72-hour settings should suffice to catch most problems fairly quickly. After three days you can throttle it back to traverse the site in 480-hours (or thereabout) and leave it running at a very small cost in performance. It is a resource hog at 72-hours but worth it for keeping your problems to a minimum.

    The plugin does have a warning that it hasn’t been tested with the last three upgrades but I’ve been running it on most of my sites without a hitch and if you’re worried at all you can disable it once you’ve cured all your potential problems.

    My sites have a lot of dynamic content from ‘elsewhere’ and I need to know when things like links and images break. I just let ‘er run at 480-hours myself.

    I hope this helps!!!

    I’ve run into this same annoyance as well and after too many times clicking a link and ending up in the wrong environment, I wrote a filter to replace the stored link with the current home_url() + the desired path. I use the WP_HOME and WP_SITEURL constants in wp-config.php (which is gitignored) so home_url() is always pointing to the right place.

    
    add_filter('wp_nav_menu_objects', function ($menu_items) {
        foreach ($menu_items as $i => $menu_item) {
            $item_path = parse_url($menu_item->url, PHP_URL_PATH);
            $menu_item->url = home_url() . $item_path;
            $menu_items[$i] = $menu_item;
        }
        return $menu_items;
    });
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Creating dynamic links in menus’ is closed to new replies.