0

Just created a page in my multisite network where the user can input a sitename and be redirected to the site. This works very well, but if there is a typo or if they enter a site that doesn't exist it will still redirect them.

Is it possible to do a check against the sites in the network to see if it exists before redirecting, and if not, output an error?

This is what I have at the moment:

<?php
if ( ! empty( $_POST['sitename'] ) ) {
    $main_site_url = network_site_url();
    $domain        = preg_replace( "/^https?:\/\/(www)?/", '', $main_site_url );
    $domain        = preg_replace( "/\/u\//", '', $domain );

    $path = preg_replace( "/\//", '', trim( $_POST['sitename'] ) );
    $path = preg_replace( "/^(.+)$/", "/u/$1/", $path );

    $sub_site_url = "https://" . $domain . $path;

    // If the sitename does not exist.
    if ( ! $blog_id = get_blog_id_from_url( $domain, $path ) ) {
      wp_die( 'Sitename does not exist.' );
    } 
    else {
      wp_redirect( $sub_site_url );
      exit();
    }
} ?>
<form name="linkform" id="linkform" method="post" action="">
    <p>
        <label for="sitename">Site Name:</label>
        <br><input type="text" name="sitename" id="sitename" value="" size="50"/>
    </p>
    <p class="submit">
        <input id="submit" type="submit" name="Submit" class="submit" value="Go"/>
    </p>
</form>
0

You could perform a validation through get_blog_id_from_url($domain, $path) function, assuming you are using directories and NOT subdomains. Otherwise, $path is not required. See code and comments below.

<?php
if ( ! empty( $_POST['sitename'] ) ) {
    $main_site_url = network_site_url( '/' );
    $domain        = preg_replace( "/^https?:\/\/(www)?/", '', $main_site_url );

    $path = preg_replace( "/\//", '', trim( $_POST['sitename'] ) );
    $path = preg_replace( "/^(.+)$/", "/$1/", $path );

    // If the sitename does not exist.
    if ( ! $blog_id = get_blog_id_from_url( $domain, $path ) ) {
        // Perform whatever you want here...
        wp_die( 'Sitename does not exist.' );
    } else {
        wp_redirect( get_site_url( $blog_id ), 302 );

        exit;
    }
} ?>
<form name="linkform"
      id="linkform"
      method="post"
      action="">
    <p>
        <label for="sitename">Site Name:</label>
        <br><input type="text" name="sitename" id="sitename" value="" size="50"/>
    </p>
    <p class="submit">
        <input id="submit" type="submit" name="Submit" class="submit" value="Go"/>
    </p>
</form>

Put the PHP code plus the form HTML in the same place/page.

| improve this answer | |
  • Just tried it out, and it doesn't have any effect at all? – joq3 Feb 28 '19 at 20:04
  • @joq3 my snippet handles requests that have a wrong sitename. Did you test a wrong sitename and nothing happened? Also, you have to make the request through the form. – filipecsweb Feb 28 '19 at 20:51
  • I tried it together with the code above, it should work when I enter a wrong site name in this input and press submit right? – joq3 Feb 28 '19 at 20:54
  • @joq3 check my answer again, let's try a different approach. – filipecsweb Mar 1 '19 at 0:01
  • It is better, but I get Sitename does not exist. even if I input a correct sitename – joq3 Mar 1 '19 at 6:47

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.