Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using the following code to delete some default pages and to also create some default pages for all new sites that are created. It work as is now but that is only because I purposely have a mistake there that whenever I correct, the functions stop working.

This is the part of the code with the purposeful error ( I was also trying to use some code to choose a default page template but that didnt work )

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

  // not really need, new blogs shouldn't have any content
  if(get_page_by_title('contact')) return;

  // create each page
  $page_id = wp_insert_post(array(
    'post_title'     => 'Attorneys',
    'post_name'      => 'attorneys',
    'post_content'   => 'This is your Attorney page.',
    'post_status'    => 'publish',
    'post_author'    => $user_id, // or "1" (super-admin?)
    'post_type'      => 'page',
    'menu_order'     => 3,
    'comment_status' => 'closed',
    'ping_status'    => 'closed',
     // + see: http://codex.wordpress.org/Function_Reference/wp_insert_post
  ));  
update_post_meta( $id, '_wp_page_template', 'new_template.php' );

  restore_current_blog();
}

Heres the full code

// Create Pages

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

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

  // not really need, new blogs shouldn't have any content
  if(get_page_by_title('About')) return;

  // create each page
  $page_id = wp_insert_post(array(
    'post_title'     => 'About',
    'post_name'      => 'about',
    'post_content'   => 'This is your about page',
    'post_status'    => 'publish',
    'post_author'    => $user_id, // or "1" (super-admin?)
    'post_type'      => 'page',
    'menu_order'     => 1,
    'comment_status' => 'closed',
    'ping_status'    => 'closed',
     // + see: http://codex.wordpress.org/Function_Reference/wp_insert_post
  ));  

  restore_current_blog();
}

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

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

  // not really need, new blogs shouldn't have any content
  if(get_page_by_title('Contact')) return;

  // create each page
  $page_id = wp_insert_post(array(
    'post_title'     => 'Contact',
    'post_name'      => 'contact',
    'post_content'   => 'This is your contact page.',
    'post_status'    => 'publish',
    'post_author'    => $user_id, // or "1" (super-admin?)
    'post_type'      => 'page',
    'menu_order'     => 2,
    'comment_status' => 'closed',
    'ping_status'    => 'closed',
     // + see: http://codex.wordpress.org/Function_Reference/wp_insert_post
  ));  

  restore_current_blog();
}

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

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

  // not really need, new blogs shouldn't have any content
  if(get_page_by_title('Attorneys')) return;

  // create each page
  $page_id = wp_insert_post(array(
    'post_title'     => 'Attorneys',
    'post_name'      => 'attorneys',
    'post_content'   => 'This is your Attorney page.',
    'post_status'    => 'publish',
    'post_author'    => $user_id, // or "1" (super-admin?)
    'post_type'      => 'page',
    'menu_order'     => 5,
    'comment_status' => 'closed',
    'ping_status'    => 'closed',
     // + see: http://codex.wordpress.org/Function_Reference/wp_insert_post
  ));  

  restore_current_blog();
}

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

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

  // not really need, new blogs shouldn't have any content
  if(get_page_by_title('contact')) return;

  // create each page
  $page_id = wp_insert_post(array(
    'post_title'     => 'Attorneys',
    'post_name'      => 'attorneys',
    'post_content'   => 'This is your Attorney page.',
    'post_status'    => 'publish',
    'post_author'    => $user_id, // or "1" (super-admin?)
    'post_type'      => 'page',
    'menu_order'     => 3,
    'comment_status' => 'closed',
    'ping_status'    => 'closed',
     // + see: http://codex.wordpress.org/Function_Reference/wp_insert_post
  ));  
update_post_meta( $id, '_wp_page_template', 'new_template.php' );

  restore_current_blog();
}

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

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

  // not really need, new blogs shouldn't have any content
  if(get_page_by_title('Practice Areas')) return;

  // create each page
  $page_id = wp_insert_post(array(
    'post_title'     => 'Practice Areas',
    'post_name'      => 'practice-areas',
    'post_content'   => 'This is your practice areas page.',
    'post_status'    => 'publish',
    'post_author'    => $user_id, // or "1" (super-admin?)
    'post_type'      => 'page',
    'menu_order'     => 4,
    'comment_status' => 'closed',
    'ping_status'    => 'closed',
     // + see: http://codex.wordpress.org/Function_Reference/wp_insert_post
  ));  

  restore_current_blog();
}

// Delete Pages
add_action('wpmu_new_blog', 'delete_my_pages', 10, 2);
function delete_my_pages(){
$post = get_page_by_path('hello-world',OBJECT,'post');
if ($post)
  wp_delete_post($post->ID,true);
}

add_action('wpmu_new_blog', 'delete_my_pages2', 10, 2);
function delete_my_pages2(){
$page = get_page_by_path('login',OBJECT,'page');
if ($page)
  wp_delete_post($page->ID,true);
}

add_action('wpmu_new_blog', 'delete_my_pages3', 10, 2);
function delete_my_pages3(){
$page = get_page_by_path('logout',OBJECT,'page');
if ($page)
  wp_delete_post($page->ID,true);
}

add_action('wpmu_new_blog', 'delete_my_pages4', 10, 2);
function delete_my_pages4(){
$page = get_page_by_path('lostpassword',OBJECT,'page');
if ($page)
  wp_delete_post($page->ID,true);
}

add_action('wpmu_new_blog', 'delete_my_pages5', 10, 2);
function delete_my_pages5(){
$page = get_page_by_path('register',OBJECT,'page');
if ($page)
  wp_delete_post($page->ID,true);
}

add_action('wpmu_new_blog', 'delete_my_pages6', 10, 2);
function delete_my_pages6(){
$page = get_page_by_path('resetpass',OBJECT,'page');
if ($page)
  wp_delete_post($page->ID,true);
}

add_action('wpmu_new_blog', 'delete_my_pages7', 10, 2);
function delete_my_pages7(){
$page = get_page_by_path('sample-page',OBJECT,'page');
if ($page)
  wp_delete_post($page->ID,true);
}

add_action('wpmu_new_blog', 'delete_my_pages8', 10, 2);
function delete_my_pages8(){
$page = get_page_by_path('store',OBJECT,'page');
if ($page)
  wp_delete_post($page->ID,true);
}

How can I clean this up and make it work without the purposeful error?

EDIT

The error is seen here -

// not really need, new blogs shouldn't have any content
  if(get_page_by_title('contact')) return;

where it says 'contact' it's suppose to match this (attorneys)

// create each page
      $page_id = wp_insert_post(array(
        'post_title'     => 'Attorneys',
        'post_name'      => 'attorneys',

If I change contact to attorneys though, then all the deleted pages show up again.

share|improve this question
1  
What precisely do you mean by "purposeful error" here? –  Rarst Sep 14 '13 at 8:01
    
The only "error" I'm seeing is a bunch of repetitions that could use some loop/arrays. –  brasofilo Sep 14 '13 at 9:26
    
@rarst I updated my question to try and clarify. –  mygm26 Sep 15 '13 at 7:51
    
@brasoflio I updated my question to try and clarify. –  mygm26 Sep 15 '13 at 7:53

1 Answer 1

I'm really not sure where your logic is failing and haven't revised each one of your insert/delete hooks. But the code can be greatly simplified to only call each function once, thus making it easy to make it work.

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

function setup_blog_wpse_114119( $blog_id, $user_id )
{
    create_pages_wpse_114119( $blog_id, $user_id );
    delete_pages_wpse_114119( $blog_id, $user_id );
}

function create_pages_wpse_114119( $blog_id, $user_id )
{
    $defaults = array(
        array(
            'post_title'     => 'About',
            'post_name'      => 'about',
            'post_content'   => 'This is your about page',
            'post_status'    => 'publish',
            'post_author'    => $user_id, // or "1" (super-admin?)
            'post_type'      => 'page',
            'menu_order'     => 1,
            'comment_status' => 'closed',
            'ping_status'    => 'closed',
        ),
        array(
            'post_title'     => 'Contact',
            'post_name'      => 'contact',
            'post_content'   => 'This is your contact page.',
            'post_status'    => 'publish',
            'post_author'    => $user_id, // or "1" (super-admin?)
            'post_type'      => 'page',
            'menu_order'     => 2,
            'comment_status' => 'closed',
            'ping_status'    => 'closed',
        )
    );  
    # Not really necessary, but good practice: http://wordpress.stackexchange.com/q/89113/12615
    $current_blog = get_current_blog_id();
    switch_to_blog( $blog_id );
    foreach( $defaults as $page )
    {
        // not really need, new blogs shouldn't have any content
        if( get_page_by_title( $page['post_title'] ) ) 
            continue;
        wp_insert_post( $page );
    }
    switch_to_blog( $current_blog );
}

function delete_pages_wpse_114119( $blog_id, $user_id )
{
    $defaults = array( 
        'post' => 'hello-world', 
        'page' => 'sample-page' 
    );
    $current_blog = get_current_blog_id();
    switch_to_blog( $blog_id );
    foreach( $defaults as $type => $slug )
    {
        if( $post = get_page_by_path( $slug, OBJECT, $type ) )
            wp_delete_post( $post->ID, true );
    }
    switch_to_blog( $current_blog );
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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