WP_INSERT_POST在WordPress多站点上的问题

时间:2013-09-26 作者:Ken

我很难解决我的这个问题。

我有一个插件,它在默认情况下为新的wordpress多站点站点创建6个页面。Im使用以下代码。

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); 
当你在博客中注册一个新帐户时,它可以完美地工作。然而,问题是,当前登录的用户为其帐户创建了一个博客,在提交了博客创建表单后,它进入了一个空白的白色页面,这是一件奇怪的事情,并且成功创建了博客,但页面没有创建。

我试图删除这个功能并创建一个示例博客,但没有“空白白页”,所以我想问题出在这个功能上。

如何解决此问题?

1 个回复
SO网友:brasofilo

钩子声明包含6个参数,但函数定义中没有任何参数。还有,在调用钩子之前wpmu_new_blog, WP执行restore_current_blog(), 因此,可以安全地假设,在插入内容之前,我们必须切换到目标博客。

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

结束

相关推荐

如何添加将在MultiSite中的所有站点上执行的JavaScript?

我们正在使用MU创建一个站点的内部网络,我们希望在全球所有站点上都有一些小东西,而不必为每个站点单独添加它们。我想添加一个JavaScript和CSS块,将在每个站点上执行。最好的方法是什么?