WP_INSERT_POST issue on WordPress Multisite

Im having a hard time on fixing this issue of mine.

I have a plugin where it creates 6 pages at default for new wordpress multisite sites. Im using the following code.

Read More
function create_pages_default() {
 global $user_ID;
// Update Sample Page and turn into About page
  $samplepage = array(
     'ID' => '2',
     'post_name' => 'about',
     'post_title' => 'About',
     'post_type' => 'page',
     'post_author' => $user_ID,
     'post_content' => '<p>Write something about your business. This may include but not limited to what you sell, what are your target audience or market, who you are, your objectives, background history and your location.</p>',
     'menu_order' => '1',
     'post_status' => 'publish',
     'comment_status' => 'closed'
  );

// Create Store Page
  $storepage = array(
     'post_name' => 'store',
     'post_title' => 'Store',
     'menu_order' => '2',
     'post_type' => 'page',
     'post_author' => $user_ID,
     'post_content' => '[WP_online_store]',
     'post_status' => 'publish',
     'comment_status' => 'closed'
  );

// Create Blog Page
  $blogpage = array(
     'post_name' => 'blog',
     'post_title' => 'Blog',
     'post_author' => $user_ID,
     'menu_order' => '3',
     'post_type' => 'page',
     'post_status' => 'publish',
     'comment_status' => 'closed'
  );

// Create Contact Us Page
  $contactus = array(
     'post_name' => 'contact',
     'post_title' => 'Contact Us',
     'post_author' => $user_ID,
     'menu_order' => '4',
     'post_type' => 'page',
     'post_content' => '[support-front]<p>Feel free to contact us using the form below about pre-sale questions, about us, our upcoming products, product/order support and anything with regards to our products and services offered.</p><p>A dedicated ticket page will be opened and the access key will be sent on your email address you have provided or your account registered email address to keep you updated of our reponses.</p>[/support-front]',
     'post_status' => 'publish',
     'comment_status' => 'closed'
  );

// Create Privacy Page
  $privacypage = array(
     'post_name' => 'privacy',
     'post_title' => 'Privacy',
     'menu_order' => '5',
     'post_type' => 'page',
     'post_author' => $user_ID,
     'post_content' => '<p>Edit this Privacy Policy page with regards to the policies concerning the information you get from your customers when they subscribe, comment, register or purchase anything on your blog store.</p>',
     'post_status' => 'publish',
     'comment_status' => 'closed'
  );

// Create Terms Page
  $termspage = array(
     'post_name' => 'termsconditions',
     'post_title' => 'Terms & Conditions',
     'menu_order' => '6',
     'post_author' => $user_ID,
     'post_type' => 'page',
     'post_content' => '<p>Edit this Terms and Conditions page for your customers. This may include but not limited to the policies concerning refunds, damaged products, deliveries, shipping, payment methods, purchasing products and so on.</p>',
     'post_status' => 'publish',
     'comment_status' => 'closed'
  );

       wp_insert_post( $blogpage );
       wp_insert_post( $storepage );
       wp_insert_post( $contactus );
       wp_insert_post( $privacypage );
       wp_insert_post( $termspage );
       wp_insert_post( $samplepage );
}
add_action( 'wpmu_new_blog', 'create_pages_default', 10, 6); 

It works perfectly when you register a new account along with a blog. However, the problem is when a current logged-in user created a blog for his account wherein after he submitted the blog creation form, it goes into a blank white page which is a weird thing and the blog was successfully created and the pages was not.

I tried to remove this function and create a sample blog and it went well without the “blank white page” so i guess the issue was on the function.

How to resolve this issue?

Related posts

1 comment

  1. The hook declaration takes 6 parameters, yet none is in your function’s definition. Also, before calling the hook wpmu_new_blog, WP does a restore_current_blog(), so it’s safe to assume that we have to switch to the destination blog before inserting content.

    add_action( 'wpmu_new_blog', 'new_blog_wpse_115724', 10, 6 );
    
    function new_blog_wpse_115724( $blog_id, $user_id, $domain, $path, $site_id, $meta ) 
    {
        $current_blog_id = get_current_blog_id();
        switch_to_blog( $blog_id );
        $samplepage = array(
           'ID' => '2',
           'post_name' => 'about',
           'post_title' => 'About',
           'post_type' => 'page',
           'post_author' => $user_id,
           'post_content' => '<p>Write something about your business. This may include but not limited to what you sell, what are your target audience or market, who you are, your objectives, background history and your location.</p>',
           'menu_order' => '1',
           'post_status' => 'publish',
           'comment_status' => 'closed'
        );
        wp_insert_post( $samplepage );
        switch_to_blog( $current_blog_id );
    }
    

Comments are closed.