WordPress.org

Make WordPress Core

Opened 21 months ago

Last modified 7 months ago

#43306 new defect (bug)

Creating site using wpmu_create_blog() without / prefixed with $path causes get_id_from_blogname() to fail.

Reported by: aubreypwd Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.9.4
Component: General Keywords:
Focuses: multisite Cc:
PR Number:

Description

The below code will result in NULL:

$id = wpmu_create_blog( 'example.org', 'example', 'neutralized', 1 );
$result = get_id_from_blogname( 'example' );

But the below code will result in an int.

$id = wpmu_create_blog( 'example.org', '/example', 'neutralized', 1 );
$result = get_id_from_blogname( 'example' );

The difference is in the 2nd snippet I prefix $path with a /. Without that being apparent in docs we should strip the need for the / or update docs.

I found this while writing a unit test, and you can see it changing below:

http://aubrey.pw/d/2018/BoaRz4J88N.gif

Attachments (2)

43306.patch (680 bytes) - added by aubreypwd 7 months ago.
This seems to work on either sub-dir or sub-domain setups with get_sites().
43306.2.patch (680 bytes) - added by aubreypwd 7 months ago.
Patched the wrong direction on the original. This one, again, works in either subdir or subdomain setup.

Download all attachments as: .zip

Change History (5)

#1 @johnjamesjacoby
21 months ago

Without looking, there might be a distinction here between subdomain and subdirectory installations.

Was this on a subdirectory network, where it's expecting to see a site as /this?

#2 @aubreypwd
7 months ago

This does seem to be limited to subdomain setup.

When running:


<?php

add_action( 'init', function() {
        if ( wp_doing_ajax() ) {
                return;
        }

        if ( ! isset( $_GET['create' ] ) ) {
                return;
        }

        $id = wpmu_create_blog( $_SERVER['HTTP_HOST'], 'example1', 'example1', 1 );
        $result = get_id_from_blogname( 'example1' );
        error_log( "example1: $id -> $result" );

        $id = wpmu_create_blog( $_SERVER['HTTP_HOST'], '/example2', 'example2', 1 );
        $result = get_id_from_blogname( 'example2' );
        error_log( "example2: $id -> $result " );

        wp_redirect( admin_url( 'network/sites.php' ) );
        exit;

} );

... as a mu-plugin, I get the following results:

[01-Apr-2019 21:54:30 UTC] example1: 15 -> 
[01-Apr-2019 21:54:32 UTC] example2: 16 ->  
[01-Apr-2019 21:54:41 UTC] example1: 8 -> 8
[01-Apr-2019 21:54:42 UTC] example2: 9 -> 9 

The first two are on the sub-domain setup, the next 2 are on a sub-directory setup. For some reason, right after creating a site, I can't call the site get_id_from_blogname on a sub-domain setup.

This is also testing on trunk.

#3 @aubreypwd
7 months ago

It appears it's because the $site_ids = get_sites( part is returning nothing on a subdomain setup when I use get_id_from_blogname. This comes up with ID's on sub-dir setup.

        $site_ids = get_sites(
                array(
                        'number'                 => 1,
                        'fields'                 => 'ids',
                        'domain'                 => $domain,
                        'path'                   => $path,
                        'update_site_meta_cache' => false,
                )
        );

@aubreypwd
7 months ago

This seems to work on either sub-dir or sub-domain setups with get_sites().

@aubreypwd
7 months ago

Patched the wrong direction on the original. This one, again, works in either subdir or subdomain setup.

Note: See TracTickets for help on using tickets.