php - Create form which redirects to site in network?

admin2025-06-02  3

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>

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>
Share Improve this question edited Mar 12, 2019 at 14:35 joq3 asked Feb 28, 2019 at 19:37 joq3joq3 3813 silver badges21 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748815218a313951.html

最新回复(0)