tags:

views:

53

answers:

2

I create lots of blogs that all share the same basic structure of a single "post" and 3 "pages" (about us, contact us, privacy). For the "post" page, I usually just edit the existing "hello world" post once I'm in the newly created blog. For the pages, I start with the "about" page and just edit that for my "about us" page.

I'd like to create a script or plug-in that I can simply add to my site that will, when executed, automatically create those pages with my boilerplate content. For the "about us" page, I can just edit the default "about page" via script. And for the initial "post" the script can just edit the "hello world" post. The other "pages" will need to be created from scratch via the script.

I'm looking for ideas on how best to do it (a plug-in or a script thats uploaded and executed) along with some help with the wordpress API calls used to create and update a post and a page's name and content.

For the default content, the about and contact content will be very short but I will need to use an external file to populate the privacy page. I'm thinking that a preformatted .html file should be used as the basis of this database insert.

Thanks for your help!

+3  A: 

If you set up the blogs from scratch each time, you could manipulate /wp-admin/includes/upgrade.php (function wp_install_defaults()) and put your pages there.

That is, however, if you are comfortable with hacking WP core files.

Another possibility is to do it after installation with a custom SQL script. Just use phpMyAdmin to look what a current page looks like and create your own "INSERT INTO" statement. For convenience, you could write a small WP plugin, that does the insert on activation and afterwards removes itself, or something like this.

Edit: From looking at the above mentioned file, you can do everything from within PHP with this:

$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' => ''
    ));
Boldewyn
Boldewyn, it appears that wp_insert_posts() might be the way to go, but I'm just not sure how to insert multiple posts at the same time. Is this asynchronous?
Scott B
I think, you will have to call it multiple times for each page to insert. What do you mean with 'asynchronous'?
Boldewyn
That's something I'd like to learn, too: inserting multiple pages, possibly looping with php? And it must be possible to insert custom fields, too?
songdogtech
A: 

You could put it in a plugin you just upload, activate and delete. Here's what I use, modified for your use (havent tested it):

<?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');
?>
Greenie