Use "SITEURL" to redirect user to own site

Hi,

I was thinking about if there is a way to use a kind of SITEURL string to put in a link on a single FAQ.
I will use this, if i want to answer a question where it would be helpful to put a link, where user can change that setting or something else.
So if i want to sent them to edit posts page, then i can type in SITEURL/wp-admin/edit.php and then SITEURL will be change to that single users personal site.

Is there a way to do this?

Best regards,

  • Sajid

    Hello Mikkel,
    Hope you are doing good today :slight_smile:

    You can not do this using that constant but instead we can do similar with shortcode. I come up with following custom code snippet to create a simple shortcode for you.

    Add following code in functions.php file of your child theme or use it as mu-plugin (recommended for MS).

    add_shortcode( 'SITEURL', function(){
    	return get_site_url();
    } );

    Now, use the following shortcode in the editor or any shortcode aware area including text widget.
    [SITEURL]

    In case, you want to use in a template file like page.php then use it like below:
    <?php echo do_shortcode('[SITEURL]'); ?>

    If you go with mu-plugin route then, go to your /wp-content/ folder via FTP, and find the folder called "mu-plugins". If there is no folder with that name, then you'll want to create one. Then, create a file inside that folder, and give the file any name you like and paste the code in there.

    You don't need to activate that plugin, as it will be always be automatically activated. Please be sure to add a PHP start tag at the beginning of the code if you insert this code as a mu-plugin, like this:

    <?php

    Take care and have a nice day :slight_smile:

    Best Regards,
    Sajid - WPMU DEV Support

  • Mikkel

    Hi Sajid,

    You just saved my day, again!

    I saved the code in a file which i put in mu-plugins folder, and when using the short code it give me the site url of the current site im looking at. Fantastic!

    Is it possible to have two more short codes?
    One for MAINSITEURL and one for USERSITEURL

    The mainsite url should link to the main site in multisite install.
    The usersite url should work like a personal URL, so if i say go to change shipping info in marketpress settings, which can be done here: [USERSITEURL]/marketpress-shipping-info
    Then the user get redirected to there own admin dashboard where they can change settings.

    Im using a sub site as a support site for the hole multisite install, on that support site i have FAQ and such, so thats why i will need to make a URL like this, to make it easier for site owners to handle their site.

    Oh, then if there is a way to make the url as a link, so user can click on it to get redirected.
    Can this be possible too?

    Best regards,

  • Sajid

    Hello Mikkel,
    Hope you are doing good today :slight_smile:

    I don't think you need another two shortcodes. Instead I come up with modified code that accepts different paramters like site ID, URI, and link text/label.

    Add following code in your functions.php file or use it as mu-plugin instead of the above.

    add_shortcode( 'SITEURL', 'wpmudev_return_url');
    add_shortcode( 'MAINSITEURL', 'wpmudev_return_url');
    
    function wpmudev_return_url( $atts, $content, $tag ){
    
        $atts = shortcode_atts( array(
                                        'ID'        => '',
                                        'uri'       => '',
                                        'label'     => 'Read More',
                                    ),
                                    $atts);
    
        extract( $atts );
    
        if('MAINSITEURL' == $tag){
            global $current_site;
            $main_site_id = $current_site->blog_id;
    
            $url = get_site_url($main_site_id). $uri;
        }else{
    
            $url = get_site_url($ID). $uri;
    
        }
    
        $final_url = '<a href="'.$url.'" title="'.$label.'">'.$label.'</a>';
    
        return $final_url;
    }

    Now you can use the following shortcodes instead:

    [MAINSITEURL uri="/contact-us" label="Contact US"]
    
    [SITEURL ID="4" uri="/contact-us" label="FAQ"]

    These parameters are optional. Also, MAINSITEURL shortcode does not accept site ID, since we force to use for main site only.

    Take care and have a nice day :slight_smile:

    Best Regards,
    Sajid - WPMU DEV Support

  • Sajid

    Hello Mikkel,
    Hope you are doing good today :slight_smile:

    Yes, we can do that too. Please try using the following code instead of above.

    add_shortcode( 'SITEURL', 'wpmudev_return_url');
    add_shortcode( 'MAINSITEURL', 'wpmudev_return_url');
    
    function wpmudev_return_url( $atts, $content, $tag ){
    
        $atts = shortcode_atts( array(
                                        'ID'        => '',
                                        'uri'       => '',
                                        'label'     => 'Read More',
                                        'target'	=> '_blank',
                                    ),
                                    $atts);
    
        extract( $atts );
    
        if('MAINSITEURL' == $tag){
            global $current_site;
            $main_site_id = $current_site->blog_id;
    
            $url = get_site_url($main_site_id). $uri;
        }else{
    
            $url = get_site_url($ID). $uri;
    
        }
    
        $final_url = '<a href="'.$url.'" title="'.$label.'" target="'.$target.'" >'.$label.'</a>';
    
        return $final_url;
    }

    By default it will open in new window but optionally you can set a different target by using the target parameter.

    The shortcode will be like this:

    [SITEURL ID="4" uri="/contact-us" label="FAQ" target="_self"]
    [MAINSITEURL uri="/contact-us" label="Contact US" target="_self"]

    See more details for target attribute here:
    http://www.w3schools.com/TAgs/att_a_target.asp

    Take care and have a nice day :slight_smile:

    Best Regards,
    Sajid - WPMU DEV Support

    • Mikkel

      Thank you so much Sajid.

      I was wondering if that SITEURL can work like it do on The Hub here om WPMU Dev?
      When we open a specific site and click the wp admin button, we got sent to that website admin dashboard.

      In my case i show FAQ on a sub site on frontend, so if a site owner reading this FAQ it would be nice if a link could sent them to their own site and not the current site they looking at, which is the way SITEURL working now.

      Best regards,

  • Sajid

    Hello @Mikkel,
    Hope you are doing good today :slight_smile:

    Oh okay, so you want the link of user's site(s) that is currently logged in. For this purpose, try below code instead.

    add_shortcode( 'USER_SITE_URL', 'wpmudev_return_current_user_site_url');
    
    function wpmudev_return_current_user_site_url( $atts, $content, $tag ){
    
        $atts = shortcode_atts( array(
                                        'uri'       => '',
                                        'label'     => 'Read More',
                                        'target'	=> '_blank',
                                    ),
                                    $atts);
    
        extract( $atts );
    
        if( 'MAINSITEURL' == $tag ){
            global $current_site;
            $main_site_id = $current_site->blog_id;
    
            $url = get_site_url($main_site_id). $uri;
        }else if( 'USER_SITE_URL' == $tag )  {
    
            $url = get_site_url($ID). $uri;
        }
    
        $user_id = get_current_user_id();
    
    	$user_blogs = get_blogs_of_user( $user_id );
    
    	if( $user_blogs ){
    
    		$final_url = '<ul>';
    
    		foreach ( $user_blogs as $user_blog ) {
    
    			$url = get_site_url($user_blog->userblog_id). '/' . $uri;
    
    		    $final_url .= '<li><a href="'.$url.'" title="'.$label.'" target="'.$target.'" >'.$label.'</a></li>';
    
    		}
    
    		$final_url .= '</ul>';
    
    	}
    
        return $final_url;
    }

    Now use [USER_SITE_URL] shortcode with the same parameters of the shortcodes posted above except the ID.

    Take care and have a nice day :slight_smile:

    Best Regards,
    Sajid - WPMU DEV Support

    • Mikkel

      Thank you so much again, Sajid.

      I just facing one problem with this [USER_SITE_URL] shortcode.
      When user have more than one site, it show up the link one time for each site.

      Is it possible to change it, so if user have more than one site, they can see the name of site, beside the link, like: Change my theme link (usersitename)
      Or maybe have a popup that let user choose which site they want to be redirected too.

      Something that could be possible?

      Best regards,

    • Mikkel

      Hi again,

      I just see that i also have a problem with the link when clicking it.
      It try open address http://mysitename.com/“wp-admin/post-new.php”

      I try to attach my file, but i was not able to do, so you can see the code from file here:

      <?php
      
      add_shortcode( 'SITEURL', 'wpmudev_return_url');
      add_shortcode( 'MAINSITEURL', 'wpmudev_return_url');
      
      function wpmudev_return_url( $atts, $content, $tag ){
      
          $atts = shortcode_atts( array(
                                          'ID'        => '',
                                          'uri'       => '',
                                          'label'     => 'Read More',
                                          'target'	=> '_blank',
                                      ),
                                      $atts);
      
          extract( $atts );
      
          if('MAINSITEURL' == $tag){
              global $current_site;
              $main_site_id = $current_site->blog_id;
      
              $url = get_site_url($main_site_id). '/' . $uri;
          }else{
      
              $url = get_site_url($ID). '/' . $uri;
      
          }
      
          $final_url = '<a href="'.$url.'" title="'.$label.'" target="'.$target.'" >'.$label.'</a>';
      
          return $final_url;
      }
      
      add_shortcode( 'USER_SITE_URL', 'wpmudev_return_current_user_site_url');
      
      function wpmudev_return_current_user_site_url( $atts, $content, $tag ){
      
          $atts = shortcode_atts( array(
                                          'uri'       => '',
                                          'label'     => 'Read More',
                                          'target'	=> '_blank',
                                      ),
                                      $atts);
      
          extract( $atts );
      
          if( 'MAINSITEURL' == $tag ){
              global $current_site;
              $main_site_id = $current_site->blog_id;
      
              $url = get_site_url($main_site_id). $uri;
          }else if( 'USER_SITE_URL' == $tag )  {
      
              $url = get_site_url($ID). $uri;
          }
      
          $user_id = get_current_user_id();
      
      	$user_blogs = get_blogs_of_user( $user_id );
      
      	if( $user_blogs ){
      
      		foreach ( $user_blogs as $user_blog ) {
      
      			$url = get_site_url($user_blog->userblog_id). '/' . $uri;
      
      		    $final_url .= '<a href="'.$url.'" title="'.$label.'" target="'.$target.'" >'.$label.'</a>';
      
      		}
      
      	}
      
          return $final_url;
      }

      Best regards,

  • Sajid

    Hello Mikkel :slight_smile:

    Is it possible to change it, so if user have more than one site, they can see the name of site, beside the link, like: Change my theme link (usersitename)
    Or maybe have a popup that let user choose which site they want to be redirected too.

    Something that could be possible?

    Do you want it like below ?

    1. Label (sitename).

    If yes then this can be doable.

    Unfortunately, we can not do the popup thing as it will require a good deal of customization that is beyond the level of support we can provide here (we have already come so far on it).

    Best Regards,
    Sajid - WPMU DEV Support

  • Sajid

    Hello Mikkel,
    Hope you are doing good today :slight_smile:

    Thanks for clarification. Here is the final code to display the site name beside label. Make sure you delete all the above code and just use this one.

    <?php 
    
    add_shortcode( 'SITEURL', 'wpmudev_return_url');
    add_shortcode( 'MAINSITEURL', 'wpmudev_return_url');
    
    function wpmudev_return_url( $atts, $content, $tag ){
    
        $atts = shortcode_atts( array(
                                        'ID'        => '',
                                        'uri'       => '',
                                        'label'     => 'Read More',
                                        'target'    => '_blank',
                                    ),
                                    $atts);
    
        extract( $atts );
    
        if('MAINSITEURL' == $tag){
            global $current_site;
            $main_site_id = $current_site->blog_id;
    
            $url = get_site_url($main_site_id). '/' . $uri;
        }else{
    
            $url = get_site_url($ID). '/' . $uri;
    
        }
    
        $final_url = '<a href="'.$url.'" title="'.$label.'" target="'.$target.'" >'.$label.'</a>';
    
        return $final_url;
    }
    
    add_shortcode( 'USER_SITE_URL', 'wpmudev_return_current_user_site_url');
    
    function wpmudev_return_current_user_site_url( $atts, $content, $tag ){
    
        $atts = shortcode_atts( array(
                                        'uri'       => '',
                                        'label'     => 'Read More',
                                        'target'    => '_blank',
                                    ),
                                    $atts);
    
        extract( $atts );
    
        if( 'MAINSITEURL' == $tag ){
            global $current_site;
            $main_site_id = $current_site->blog_id;
    
            $url = get_site_url($main_site_id). $uri;
        }else if( 'USER_SITE_URL' == $tag )  {
    
            $url = get_site_url($ID). $uri;
        }
    
        $user_id = get_current_user_id();
    
        $user_blogs = get_blogs_of_user( $user_id );
    
        if( $user_blogs ){
    
            $final_url = '<ul>';
    
            foreach ( $user_blogs as $user_blog ) {
    
                $url = get_site_url($user_blog->userblog_id). '/' . $uri;
    
                if(count( $user_blogs) > 1){
                    $final_url .= '<li><a href="'.$url.'" title="'.$label.'" target="'.$target.'" >'.$label.' ('.$user_blog->blogname.')</a></li>';
                }else{
                    $final_url .= '<li><a href="'.$url.'" title="'.$label.'" target="'.$target.'" >'.$label.'</a></li>';
                }
            }
    
            $final_url .= '</ul>';
    
        }
    
        return $final_url;
    }

    Take care and have a nice day :slight_smile:

    Best Regards,
    Sajid - WPMU DEV Support

  • Sajid

    Hello Mikkel,

    Yes, it would not display those links to search engines since its required the current user to be logged and must have some sites.

    The below code will check, if user logged in it will display the sites with link, label and site name. If user is not logged in it will just display the label without link at all.

    <?php 
    
    add_shortcode( 'SITEURL', 'wpmudev_return_url');
    add_shortcode( 'MAINSITEURL', 'wpmudev_return_url');
    
    function wpmudev_return_url( $atts, $content, $tag ){
    
        $atts = shortcode_atts( array(
                                        'ID'        => '',
                                        'uri'       => '',
                                        'label'     => 'Read More',
                                        'target'    => '_blank',
                                    ),
                                    $atts);
    
        extract( $atts );
    
        if('MAINSITEURL' == $tag){
            global $current_site;
            $main_site_id = $current_site->blog_id;
    
            $url = get_site_url($main_site_id). '/' . $uri;
        }else{
    
            $url = get_site_url($ID). '/' . $uri;
    
        }
    
        $final_url = '<a href="'.$url.'" title="'.$label.'" target="'.$target.'" >'.$label.'</a>';
    
        return $final_url;
    }
    
    add_shortcode( 'USER_SITE_URL', 'wpmudev_return_current_user_site_url');
    
    function wpmudev_return_current_user_site_url( $atts, $content, $tag ){
    
        $atts = shortcode_atts( array(
                                        'uri'       => '',
                                        'label'     => 'Read More',
                                        'target'    => '_blank',
                                    ),
                                    $atts);
    
        extract( $atts );
    
        if( 'MAINSITEURL' == $tag ){
            global $current_site;
            $main_site_id = $current_site->blog_id;
    
            $url = get_site_url($main_site_id). $uri;
        }else if( 'USER_SITE_URL' == $tag )  {
    
            $url = get_site_url($ID). $uri;
        }
    
        $user_id = get_current_user_id();
    
        $user_blogs = get_blogs_of_user( $user_id );
    
        if( $user_blogs ){
    
            $final_url = '<ul>';
    
            foreach ( $user_blogs as $user_blog ) {
    
                $url = get_site_url($user_blog->userblog_id). '/' . $uri;
    
                if(count( $user_blogs) > 1){
                    $final_url .= '<li><a href="'.$url.'" title="'.$label.'" target="'.$target.'" >'.$label.' ('.$user_blog->blogname.')</a></li>';
                }else{
                    $final_url .= '<li><a href="'.$url.'" title="'.$label.'" target="'.$target.'" >'.$label.'</a></li>';
                }
            }
    
            $final_url .= '</ul>';
    
        }elseif( !is_user_logged_in() ){
    
            $final_url .= '<a title="'.$label.'" target="'.$target.'" >'.$label.'</a>';
    
        }
    
        return $final_url;
    }

    Take care and have a nice day :slight_smile:

    Best Regards,
    Sajid - WPMU DEV Support

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.