1

I'm struggling with WordPress Multisite page creation when newly site created. I found some hooks to achieve this but the hook is not responding. Some of the resources I found on stack and other sites are https://stackoverflow.com/questions/40014419/wordpress-multisite-default-page-for-newly-created-site and http://www.wpbeginner.com/wp-tutorials/how-to-add-remove-default-pages-in-wordpress-multisite/

The hook mentioned in these resources not working in my multisite setup.

add_action('wpmu_new_blog', 'wpb_create_my_pages', 10, 2);

function wpb_create_my_pages($blog_id, $user_id){
  switch_to_blog($blog_id);

// create new page
  $page_id = wp_insert_post(array(
    'post_title'     => 'About',
    'post_name'      => 'about',
    'post_content'   => 'This is an about page. Feel free to edit or delete this page.',
    'post_status'    => 'publish',
    'post_author'    => $user_id, // or "1" (super-admin?)
    'post_type'      => 'page',
    'menu_order'     => 1,
    'comment_status' => 'closed',
    'ping_status'    => 'closed',
 ));  

// Find and delete the WP default 'Sample Page'
$defaultPage = get_page_by_title( 'Sample Page' );
wp_delete_post( $defaultPage->ID );

  restore_current_blog();
}

I'm adding this code in my child theme functions.php file. Any suggestion to fix this issue.

4
  • Hi. What do you mean by not working? It doesn't create page? Doesn't delete the sample page? Produces fatal error? etc.
    – Johansson
    Jun 12, 2017 at 16:29
  • @JackJohansson Yes, not creating the page and not deleting the sample page but does not show any error. Jun 12, 2017 at 17:42
  • Hmm, it appears @Nikolay is right. It must be in your main site's functions.php, or a plugin.
    – Johansson
    Jun 12, 2017 at 17:50
  • @JackJohansson no luck, I'm trying the same with functions.php file and also with one plugin. Jun 12, 2017 at 18:14

2 Answers 2

3

The code you provided works great on my multisite. I am putting it in a network activated plugin though. Try the same.

2
  • I'm trying the same, as built one plugin and that plugin is network activated. But no luck. Will you please explain your setup. Jun 12, 2017 at 18:12
  • @webtuts4u It uses subdomains for subsites, WP 4.8, PHP 5.6.29, I am creating the sites from /wp-admin/network/site-new.php and putting me as the admin of the new site.
    – Nikolay
    Jun 12, 2017 at 18:45
2

The "wpmu_new_blog" hook has been deprecated! Instead you should use the "wp_initialize_site" hook. For some reason Per Søderlind recomended using a priority higher than 100 in this post.

The rest is pretty much the same. The only change that has been made (also taking advice from the post that Per Søderlind did) was to change the $blog_id to $new_site->blog_id. It didn't worked for me using only $blog_id.

Also, important detail. I've this piece of code as an mu-plugin.

add_action('wp_initialize_site', 'wpb_create_my_pages', 910, 1);

function wpb_create_my_pages(WP_Site $new_site){
  switch_to_blog($new_site->blog_id);

// create new page
  $page_id = wp_insert_post(array(
    'post_title'     => 'About',
    'post_name'      => 'about',
    'post_content'   => 'Page content goes in here',
    'post_status'    => 'publish',
    'post_author'    => 1,
    'post_type'      => 'page',
    'menu_order'     => 1,
    'comment_status' => 'closed',
    'ping_status'    => 'closed',
 ));  

// Find and delete the WP default 'Sample Page'
$defaultPage = get_page_by_title( 'Sample Page' );
wp_delete_post( $defaultPage->ID );

  restore_current_blog();
}

Hope this works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.