web-dev-qa-db-fra.com

Problème WP_INSERT_POST sur Wordpress Multisite

J'ai du mal à régler ce problème.

J'ai un plugin où il crée 6 pages par défaut pour les nouveaux sites multisites wordpress. J'utilise le code suivant.

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); 

Cela fonctionne parfaitement lorsque vous enregistrez un nouveau compte avec un blog. Cependant, le problème réside dans le moment où un utilisateur actuellement connecté a créé un blog pour son compte. Après avoir soumis le formulaire de création de blog, celui-ci passe à une page blanche vierge qui est une chose étrange et le blog a été créé avec succès et les pages ne sont pas .

J'ai essayé de supprimer cette fonction et de créer un exemple de blog. Cela s'est bien passé sans la "page blanche vierge", donc je suppose que le problème était lié à la fonction.

Comment résoudre ce problème?

2
Ken

La déclaration de hook prend 6 paramètres, mais aucun n'est dans la définition de votre fonction. De plus, avant d'appeler le hook wpmu_new_blog, WP effectue une restore_current_blog(); il est donc prudent de supposer que nous devons basculer vers le blog de destination avant d'insérer du contenu.

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 );
}
2
brasofilo