0

How do I prevent the creation of these when creating a new Multisite?

Everytime I create a new multisite this gets created:

  • Hello World
  • Sample Page
  • A sample Comment

I tried adding this to the functions.php

add_action( 'wpmu_new_blog', 'delete_wordpress_defaults', 100, 1 );

function delete_wordpress_defaults(){

// 'Hello World!' post
wp_delete_post( 1, true );

// 'Sample page' page
wp_delete_post( 2, true );
}

It still created Hello world and Sample page. And I don't know how to disable the sample comment.

1

When you create a new site with multisite WP does not automatically change the context. So your action is added to the current site, not the new site. You have to switch first. Like this:

add_action ('wpmu_new_blog', 'wpse296303_delete_wordpress_defaults', 100, 1);

function wpse296303_delete_wordpress_defaults ($blog_id, $user_id, $domain, $path, $site_id, $meta) {
  // Switch to the newly created blog
  switch_to_blog ($blog_id);
  // Delete 'Hello World!' post
  wp_delete_post (1, true);
  // Delete 'Sample page' page
  wp_delete_post (2, true);
  // Delete 'Sample comment' page
  wp_delete_comment (1, true)
  }

I'm not sure if the ID of the default comment is 1, so you'd have to check that.

  • I'm not completely sure I understand. Do I have to do something else than adding this to my functions.php file? And creating a new site will not create this? What do you mean with "You have to switch first"? I tried adding this code (checked that comment id 1 is correct) and added the ending ";" after comment line. It did not work, I got a white screen when creating a new site. The site was created, but the sample post/pages/comments where there! – jockebq Mar 9 '18 at 11:59
  • Code is always executed in the context of the current site. So if you put the snippet in the functions.php of site A, which is active when you create site B, it will be executed on site A and delete the posts on site A. So you have to switch to site B first then and the delete the posts. If you put the snippet in the functions.php of site B it will only be executed after the theme was activated. That might be after the posts have been created. You can use switch_theme after switchting the blog to activate the functions file. – cjbj Mar 11 '18 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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