• I’m trying to return the site_url in a shortcode. In simple PHP I’m verifying with inspecting the following statement and it is fine…

    get_site_url() // echos string(20) "https://mydomain.com

    … but the following strips out the colon : from the url.

    add_shortcode('site_url', 'shortcode_site_url');
    function shortcode_site_url() {
    	return get_site_url();
    }
    // Shortcode returns "https//mydomain.com".  Notice that the : is missing from the shortcode return.
    

    Why does the shortcode function strip out the colon? I searched through the forum and there are a number of other reports all with the same / similar issue, though I have not found any solution or reason.

    To me, this is a bug.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter Norm Sash

    (@normsash)

    To clarify a bit more… I’m adding the shortcode into a nav menu custom menu item similar to this in the url field of the menu item:

    [site_url]my-account/

    … and if you then examine the url of that menu item when it is rendered, it renders something like

    http//mydomain.com/my-account

    … again, missing the : colon

    Hi Norm,

    When I placed the shortcode in a page, it works fine.

    “I’m adding the shortcode into a nav menu custom menu item similar to this in the url field of the menu item:”

    Special characters are being stripped, I would recommend using a filter function to add your custom menu items.

    https://developer.www.ads-software.com/reference/hooks/wp_nav_menu_items/

    Ben

    Here’s an example:

    add_filter( 'wp_nav_menu_items', 'my_custom_menu', 10, 2 );
    
    function my_custom_menu( $items, $args ) {
    
        if( $args->theme_location == 'primary')  {
             
            $items .=  '<li class="menu-item"><a href="link here"'.'</a></li>';
     
        }
        return $items;
    }
    Thread Starter Norm Sash

    (@normsash)

    Thanks Ben, that is helpful. However, I don’t see a way to add the menu items via filter as working very well for me.

    On several of the sites I’m trying to make the custom menu items more resilient to changes in site_url locations.

    For example, I have a WP site at mydomain.com/production. I then clone that to a WP site mydomain.com/stagging.

    In that case, all custom menu items that were entered in the /production site would still be pointing at the /production link. What I want them to do is update to point to the /stagging install (just like the nav links to posts do).

    That’s why I’m trying to use the shortcode [site_url] which returns the WP site url address.

    So in the nav custom link if I just put something like [site_url]my-account/ it would always return the proper url link no matter what site location I’m using.

    And it works great – except for one thing… the stripping of the colon out of the url link. Slashes are retained just fine, it’s just the colon that is missing.

    Is there maybe a filter that would allow colons and not treat them as special characters?

    Hi Norm,

    Give this plugin a try, it worked for me.

    https://www.ads-software.com/plugins/shortcode-in-menus/

    Alternatively, a migration plugin will handle all the url changes between production and your staging environments.

    Cheers,
    Ben

    Moderator bcworkz

    (@bcworkz)

    When you alter a site’s URL, the menu links are not the only place where URLs need to be updated. Site URLs occur all over the DB and need to be sought out and updated. Migration plugins like Ben mentioned will do this for you. If you prefer a more hands-on migration approach (more reliable IME), use the Better Search and Replace plugin to update all site URLs found in the DB.

    Thread Starter Norm Sash

    (@normsash)

    Yes, I’m aware of the migration plugins and do use them, as well as the shortcode in menu plugin.

    This is a specific use case in which I want to return the site_url in the menu item.

    The shortcode that I created [site_url] shown earlier in this thread works fine when placed in a post (e.g returns https://mydomain.com) but when put in a nav menu item returns https//mydomain.com (missing :).

    Maybe there’s no answer to this problem. Closest is what @berksted hinted at: being that WP is stripping “:” out of the nav menu item because it thinks it is a special character.

    Thus, I’m wondering if there is any WP filter that might allow me to modify the list of “special” characters and allow the “:” to pass. I haven’t found one but I could certainly be missing it.

    @normsash

    add_filter('wp_nav_menu_items', 'do_shortcode');
    add_shortcode('site_url', 'shortcode_site_url');
    function shortcode_site_url() {
    	return preg_replace('#^https?://#i', '', get_site_url());
    }

    When I add the shortcode to my custom link, save and look at it again, I get: https://[site_url]/my-account

    That then renders: https://http//my-site.localhost/my-account

    Using the above simply strips the https:// or https:// from the get_site_url() and seems to work for me.

    @normsash did you ever find a solution to this? I can use [site_url] in most places, but I’m having the problem if I try to put a link inside a button, with either the block editor or elementor. I do see what @tugbucket and @bcworkz posted, but I think this is a more general problem than navigation items. I’m wondering if this is the result of security-related “sanitization” of some sort.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Why is colon : striped out of url when returned in shortcode’ is closed to new replies.