$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Create form which redirects to site in network?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Create form which redirects to site in network?

matteradmin10PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far