Remove Sample Content From New Network Sites
Deletes the "Hello world!" post, its sample comment and the "Sample Page" from each newly created site so site owners start with a clean slate.
Opens WPIDE on
Code
if ( ! is_multisite() ) {
return;
}
add_action( 'wp_initialize_site', function ( $site ) {
switch_to_blog( $site->blog_id );
foreach ( array( 'hello-world' => 'post', 'sample-page' => 'page' ) as $slug => $type ) {
$found = get_posts( array(
'name' => $slug,
'post_type' => $type,
'post_status' => 'any',
'posts_per_page' => 1,
'fields' => 'ids',
'no_found_rows' => true,
) );
if ( $found ) {
wp_delete_post( $found[0], true ); // Also removes its comments.
}
}
restore_current_blog();
}, 200 );
Notes
Only runs once, right after a site is created; it matches the sample items by their default slugs.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.