2010-02-10 57 views
1

我創建了大量的博客,所有這些博客共享單個「後」和3「頁面」(關於我們,聯繫我們,隱私)相同的基本結構。對於「發佈」頁面,我通常只需在新創建的博客中編輯現有的「hello world」帖子。對於這些頁面,我從「about」頁面開始,然後編輯「about us」頁面。WordPress的博客設置腳本來創建關於,聯繫,隱私頁面

我想創建一個腳本或插件,我可以簡單地添加到我的網站中,該腳本或插件在執行時會自動使用我的樣板內容創建這些網頁。對於「關於我們」頁面,我可以通過腳本編輯默認的「關於頁面」。對於最初的「發佈」,腳本可以編輯「hello world」發佈。其他「頁面」需要通過腳本從頭開始創建。

我正在尋找關於如何最好地做到這一點的想法(一個插件或腳本上傳和執行),以及一些幫助與wordpress API調用用於創建和更新帖子和頁面的名稱和內容。

對於默認內容,關於和聯繫內容將會很短,但我需要使用外部文件來填充隱私頁面。我在想,應該使用預格式化的.html文件作爲此數據庫插入的基礎。

感謝您的幫助!

回答

3

如果您每次都從頭開始設置博客,則可以操作/wp-admin/includes/upgrade.php(函數wp_install_defaults())並將頁面放在那裏。

然而,如果你對盜版WP核心文件感到滿意的話。

另一種可能性是用自定義SQL腳本安裝後執行此操作。只需使用phpMyAdmin來查看當前頁面的外觀,並創建您自己的「INSERT INTO」語句。爲了方便起見,您可以編寫一個小WP插件,在激活時插入插件,然後刪除它自己,或者類似的東西。

編輯:通過觀察上述文件,你可以在PHP中做的一切與此:

$wpdb->insert($wpdb->posts, array(
    'post_author' => $user_id, 
    'post_date' => $now, 
    'post_date_gmt' => $now_gmt, 
    'post_content' => __('Lorem ipsum...'), 
    'post_excerpt' => '', 
    'post_title' => __('About'), 
    /* translators: Default page slug */ 
    'post_name' => _x('about', 'Default page slug'), 
    'post_modified' => $now, 
    'post_modified_gmt' => $now_gmt, 
    'guid' => $first_post_guid, 
    'post_type' => 'page', 
    'to_ping' => '', 
    'pinged' => '', 
    'post_content_filtered' => '' 
    )); 
+0

Boldewyn,似乎wp_insert_posts()可能是要走的路,但我只是不知道如何在插入多個崗位同一時間。這是異步嗎? – 2010-02-10 15:44:03

+0

我想,您必須多次爲每個要插入的頁面調用它。 「異步」是什麼意思? – Boldewyn 2010-02-10 16:41:00

+0

這也是我想學習的東西:插入多個頁面,可能使用php循環?而且它也必須可以插入自定義字段? – markratledge 2010-02-10 17:19:34

0

你可以把它放在一個插件,你只需上傳,激活和刪除。 這是我使用,修改爲您的使用(還沒有測試它):

<?php 
/* 
Plugin Name: Startup Settings 
Plugin URI: http://someurl/ 
Description: Some description 
Version: 1 
Author: Your Name 
Author URI: http://yoursite.com 
*/ 

function startup_settings() 
{ 

    // Remove "Hello world" post and comment. 
    wp_delete_post(1, TRUE); 
    wp_delete_comment(1); 

    // Insert your pages. 
    global $user_ID; 
    $about_page = array(
     'post_title' => 'About us', 
     'post_content' => 'Your content goes here', 
     'post_status' => 'publish', 
     'post_date' => date('Y-m-d H:i:s'), 
     'post_author' => $user_ID, 
     'post_type' => 'page', 
     'post_category' => array(0) 
    ); 
    $post_id = wp_insert_post($about_page); 

    $contact_page = array(
     'post_title' => 'Contact us', 
     'post_content' => 'Your content goes here', 
     'post_status' => 'publish', 
     'post_date' => date('Y-m-d H:i:s'), 
     'post_author' => $user_ID, 
     'post_type' => 'page', 
     'post_category' => array(0) 
    ); 
    $post_id = wp_insert_post($contact_page); 

    // if you want to load the privacy text from a external file (untested) 
    $myFile = "/path/to/privacy.txt"; 
    $fh = fopen($myFile, 'r'); 
    $theData = fread($fh, filesize($myFile)); 
    fclose($fh); 

    $privacy_page = array(
     'post_title' => 'Privacy', 
     'post_content' => $theData, 
     'post_status' => 'publish', 
     'post_date' => date('Y-m-d H:i:s'), 
     'post_author' => $user_ID, 
     'post_type' => 'page', 
     'post_category' => array(0) 
    ); 
    $post_id = wp_insert_post($privacy_page); 

    // Set some deafault options if you want. 
    $options = array(
     'comment_max_links'   => 0, 
     'comments_per_page'   => 0, 
     'date_format'    => 'd.m.Y', 
     'default_ping_status'  => 'closed', 
     'links_updated_date_format' => 'l, F j, Y', 
     'permalink_structure'  => '/%postname%/', 
     'rss_language'    => 'en', 
     'use_smilies'    => 0 
    ); 

    foreach ($options as $option => $value) 
    { 
     update_option($option, $value); 
    } 

    return; 
} 
register_activation_hook(__FILE__, 'startup_settings'); 
?>