2

I am creating a child site from front-end by using wpmu_create_blog(). The whole code is working fine but the main issue occurs when we visit the new site frontend it gets

404 page not found

But when I create the new child site from wp-admin, the new site works flawlessly with no error.

The code I have used in the page-template for creating the new child site is -

<?php
    # Load WordPress barebones
    define( 'WP_USE_THEMES', false );
    require( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );

    # Multisite domain
    $main_site = get_site_url();

    # Type of Multisite
    $subdomain_install = false;

    # URL param activated
    if( isset( $_POST['new-site'] ) )
    {
        # Create a new user
        $rand_number = rand( 1, 2000 );
        $username = $_POST['username'];
        $password = $_POST['password'];
        // $password = wp_generate_password( 12, false );
        $email = $_POST['emailaddr'];
        $user_id = wpmu_create_user( $username, $password, $email );
        // wp_new_user_notification( $user_id, $password );

        # Create site

            $newdomain = str_replace(array('http://','https://'),'',$main_site);
            $path = "/{$_POST['site_title']}/";

        $title = $_POST['site_title'];
        $blog_id = wpmu_create_blog( $newdomain, $path, $title, $user_id, array( 'public' => 1 ) );
        if ( is_wp_error($return) ) {
       die($blog_id->get_error_message());
      }

     else {
         echo "success";
     }
    }
    ?>
    <form action="" method="POST">
        <label>User Name</label>
        <input type="text" name="username" value="" />
        <label>Password</label>
        <input type="password" name="password" value="" />
        <label>Email</label>
        <input type="email" name="emailaddr" value="" />
        <label>Site Address</label>
        <input type="text" name="siteaddress" value="" />
        <label>Site Title</label>
        <input type="text" name="site_title" value="" />
        <input type="submit" name="new-site" value="new-site" />
    </form>

Please suggest any me, if I am doing something wrong!

Dimple
  • 21
  • 2

1 Answers1

0

I have found the solution of this problem, wpmu_create_blog function does not work on the local host.This function works on live server. Code is perfectly created new site from front end. That it was the url issue where localhost have localhost/wordpress like format and this function only takes the www.example.com like format for creating new website.

Dimple
  • 21
  • 2