Make WordPress Core

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#48878 closed defect (bug) (invalid)

5.1 wp_insert_site bug

Reported by: bikecrazyy's profile bikecrazyy Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.1
Component: Networks and Sites Keywords:
Focuses: multisite Cc:

Description

The action hook wp_insert_site will not allow wp_insert_post() to be ran.
wp_insert_site() will return Could not insert post into the database: Table "wordpress.wp_9_posts" doesn't exist

The old action 'wpmu_new_blog' works with wp_insert_post() in it.

Working with old action hook wpmu_new_blog

<?php
add_action(
        'wpmu_new_blog',
        function ( $blog_id ) {

                switch_to_blog( $blog_id );
                
                $home_page_id = wp_insert_post(
                        array(
                                'post_title'    => 'Home',
                                'post_name'     => 'home',
                                'post_content'  => '',
                                'post_status'   => 'publish',
                                'post_author'   => 1,
                                'post_type'     => 'page',
                                'page_template' => 'page-templates/location_home.php',
                        )
                        , true);
                echo 'dump:';
                var_dump($home_page_id);

                restore_current_blog();
    }
);

This fails, using new action hook 'wp_insert_site'.

<?php
add_action(
        'wp_insert_site',
        function ( $wp_site ) {

                switch_to_blog( $wp_site->blog_id );

                $home_page_id = wp_insert_post(
                        array(
                                'post_title'    => 'Home',
                                'post_name'     => 'home',
                                'post_content'  => '',
                                'post_status'   => 'publish',
                                'post_author'   => 1,
                                'post_type'     => 'post'
                        )
                        , true);
                echo 'dump:';
                var_dump($home_page_id);

                restore_current_blog();
        }
);

Change History (8)

#1 @SergeyBiryukov
5 years ago

  • Component changed from General to Posts, Post Types
  • Focuses multisite added

#2 @bikecrazyy
5 years ago

  • Component changed from Posts, Post Types to General
  • Focuses multisite removed

Note, the tables do make it to the database once the query is complete but not untill then. If you call var_dump( $wpdb->tables() ); before the insert you will see it show the table names. But when wp_insert_post is called directly after it will show the error above.

#3 @jeremyfelt
5 years ago

  • Component changed from General to Networks and Sites
  • Focuses multisite added
  • Keywords reporter-feedback added

Hi @bikecrazyy, thanks for opening a ticket.

The site's database tables are added in wp_initialize_site(), which is attached to the wp_initialize_site hook at the default priority and run after the site is inserted into the wp_blogs table in wp_insert_site().

I haven't tested this myself, but you should be able to hook into wp_initialize_site at a priority higher than 10 and the database tables will be available.

#4 follow-up: @bikecrazyy
5 years ago

@jeremyfelt

Thanks for responding, I changed add_action priority to 5, 10, 15, and 999 but still same issue.

Version 0, edited 5 years ago by bikecrazyy (next)

#5 in reply to: ↑ 4 @jeremyfelt
5 years ago

Replying to bikecrazyy:

I changed add_action priority to 5, 10, 15, and 999 but still the same issue.

Did you change the action to wp_initialize_site from wp_insert_site?

Also, to clarify: the priority number should be more than 10, which I guess is a "lower" priority.

add_action( 'wp_initialize_site', function( $site_id ) { // do things }, 11, 1 );

#6 @bikecrazyy
5 years ago

@jeremyfelt ah, tricky, yes I forgot to change it to wp_initialize_site, once I did that things started working. Thank you so much for the support, this is resolved.

Do I have to resolve it or is that something you can do?

#7 @bikecrazyy
5 years ago

  • Resolution set to invalid
  • Status changed from new to closed

#8 @desrosj
5 years ago

  • Keywords reporter-feedback removed
  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.