6

As the title states i need an approach to change default settings when a new site is created on my wordpress multi site.
I guess its easiest to keep the code in a mu-plugin but can a go about having specific default options like:

  • frontpage as static page
  • create a default page and set it as blog index page
  • date and time format

1 Answer 1

6

WordPress provides a filter called wpmu_new_blog and passes the parameter $blog_id (the ID of the new blog) and $user_id (the user, that just created that new blog) (and some more) to it. You can hook into this action to create the new pages and edit the options:

<?php
/**
 * Plugin Name: Default Site Structure
 * Description: Crates default pages and set them as front/blog index pages.
 * Network:     true
 * Plugin URL:  http://wordpress.stackexchange.com/a/219504/31323
 * License:     MIT
 * Version:     1.0.0-alpha
 */
namespace WPSE177819;

add_action( 'wp_loaded', __NAMESPACE__ . '\init' );

/**
 * @wp-hook wp_loaded
 */
function init() {

    add_action(
        'wpmu_new_blog',
        function( $blog_id, $user_id ) {

            switch_to_blog( $blog_id );

            $front_page_id = wp_insert_post( front_page_data( $user_id ) );
            $index_page_id = wp_insert_post( index_page_data( $user_id ) );

            if ( ! is_wp_error( $front_page_id ) && 0 !== $front_page_id ) {
                update_option( 'show_on_front', 'page' );
                update_option( 'page_on_front', $front_page_id );
            }
            if ( ! is_wp_error( $index_page_id ) && 0 !== $index_page_id ) {
                update_option( 'page_for_posts', $index_page_id );
            }


            update_option( 'date_format', date_format() );
            update_option( 'time_format', time_format() );

            restore_current_blog();
        },
        10,
        2
    );
}

/**
 * Returns the data of the blog index page
 *
 * @param int $post_author
 *
 * @return array
 */
function index_page_data( $post_author ) {

    return [
        'post_title'   => 'My blog index',
        'post_content' => '',
        'post_type'    => 'page',
        'post_author'  => $post_author,
        'post_status'  => 'publish'
    ];
}

/**
 * Returns the data of the front page
 *
 * @param int $post_author
 *
 * @return array
 */
function front_page_data( $post_author ) {

    return [
        'post_title'   => 'Hello World',
        'post_content' => 'Welcome to my new site!',
        'post_type'    => 'page',
        'post_author'  => $post_author,
        'post_status'  => 'publish'
    ];
}

/**
 * Returns the custom date format
 *
 * @return string
 */
function date_format() {

    return 'd,m,Y';
}

/**
 * Returns the custom time format
 *
 * @return string
 */
function time_format() {

    return 'H/i/s';
}

Use this plugin as mu-plugin and it will affect each new blog. This example edits all option keys directly. You should take some time to figure out, whether WordPress provides API functions to setup these options. (Something like wp_set_front_page()…)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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