Skip to content

Instantly share code, notes, and snippets.

@brasofilo
Last active September 3, 2023 20:24
Show Gist options
  • Save brasofilo/4242948 to your computer and use it in GitHub Desktop.
Save brasofilo/4242948 to your computer and use it in GitHub Desktop.
WP Install Defaults - Dropin Plugin
<?php
!defined( 'ABSPATH' ) AND exit;
/*
Plugin Name: Custom Installation Script
Plugin URI: http://wordpress.stackexchange.com/q/75420/12615
Description: Create our own content on WP install
License: GPL
*/
/**
* Plugin auto-activation function
*
* http://wordpress.stackexchange.com/q/4041/12615
*/
function wpse_4041_run_activate_plugin( $plugin )
{
$current = get_option( 'active_plugins' );
$plugin = plugin_basename( trim( $plugin ) );
if( !in_array( $plugin, $current ) )
{
$current[] = $plugin;
sort( $current );
do_action( 'activate_plugin', trim( $plugin ) );
update_option( 'active_plugins', $current );
do_action( 'activate_' . trim( $plugin ) );
do_action( 'activated_plugin', trim( $plugin ) );
}
return null;
}
/**
* Overriding WordPress custom install defaults
* Removed ALL Multisite functionality from the original wp_install_defaults()
*
* http://wordpress.stackexchange.com/a/75436/12615
*/
function wp_install_defaults( $user_id )
{
global $wpdb, $wp_rewrite, $current_site, $table_prefix;
// Default category
$cat_name = __( 'My custom WP install' );
$cat_slug = sanitize_title( _x( 'My custom WP install', 'Default category slug' ) );
if( global_terms_enabled() )
{
$cat_id = $wpdb->get_var(
$wpdb->prepare(
"SELECT cat_ID FROM {$wpdb->sitecategories} WHERE category_nicename = %s",
$cat_slug
)
);
if( $cat_id == null )
{
$wpdb->insert(
$wpdb->sitecategories,
array(
'cat_ID' => 0,
'cat_name' => $cat_name,
'category_nicename' => $cat_slug,
'last_updated' => current_time( 'mysql', true )
)
);
$cat_id = $wpdb->insert_id;
}
update_option( 'default_category', $cat_id );
}
else
{
$cat_id = 1;
}
$wpdb->insert(
$wpdb->terms,
array(
'term_id' => $cat_id,
'name' => $cat_name,
'slug' => $cat_slug,
'term_group' => 0
)
);
$wpdb->insert(
$wpdb->term_taxonomy,
array(
'term_id' => $cat_id,
'taxonomy' => 'category',
'description' => '',
'parent' => 0,
'count' => 1
)
);
$cat_tt_id = $wpdb->insert_id;
// Default link category
$cat_name = __( 'Blogroll' );
/* translators: Default link category slug */
$cat_slug = sanitize_title( _x( 'Blogroll', 'Default link category slug' ) );
if( global_terms_enabled() )
{
$blogroll_id = $wpdb->get_var(
$wpdb->prepare(
"SELECT cat_ID FROM {$wpdb->sitecategories} WHERE category_nicename = %s",
$cat_slug
)
);
if( $blogroll_id == null )
{
$wpdb->insert(
$wpdb->sitecategories,
array(
'cat_ID' => 0,
'cat_name' => $cat_name,
'category_nicename' => $cat_slug,
'last_updated' => current_time( 'mysql', true )
)
);
$blogroll_id = $wpdb->insert_id;
}
update_option( 'default_link_category', $blogroll_id );
}
else
{
$blogroll_id = 2;
}
$wpdb->insert(
$wpdb->terms,
array(
'term_id' => $blogroll_id,
'name' => $cat_name,
'slug' => $cat_slug,
'term_group' => 0
)
);
$wpdb->insert(
$wpdb->term_taxonomy,
array(
'term_id' => $blogroll_id,
'taxonomy' => 'link_category',
'description' => '',
'parent' => 0,
'count' => 7
)
);
$blogroll_tt_id = $wpdb->insert_id;
// Now drop in some default links
$default_links = array( );
$default_links[] = array(
'link_url' => __( 'http://wordpress.stackexchange.com/' ),
'link_name' => __( 'WordPress Answers' ),
'link_rss' => '',
'link_notes' => '' );
foreach( $default_links as $link )
{
$wpdb->insert( $wpdb->links, $link );
$wpdb->insert(
$wpdb->term_relationships,
array(
'term_taxonomy_id' => $blogroll_tt_id,
'object_id' => $wpdb->insert_id
)
);
}
// First post
$now = date( 'Y-m-d H:i:s' );
$now_gmt = gmdate( 'Y-m-d H:i:s' );
$first_post_guid = get_option( 'home' ) . '/?p=1';
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$first_post = __( 'Welcome to <a href="SITE_URL">SITE_NAME</a>. This is your first post. Edit or delete it, then start blogging!' );
$first_post = str_replace( "SITE_URL", esc_url( home_url() ), $first_post );
$first_post = str_replace( "SITE_NAME", $blogname, $first_post );
$wpdb->insert( $wpdb->posts, array(
'post_author' => $user_id,
'post_date' => $now,
'post_date_gmt' => $now_gmt,
'post_content' => $first_post,
'post_excerpt' => '',
'post_title' => __( 'Hello world!' ),
/* translators: Default post slug */
'post_name' => sanitize_title( _x( 'hello-world', 'Default post slug' ) ),
'post_modified' => $now,
'post_modified_gmt' => $now_gmt,
'guid' => $first_post_guid,
'comment_count' => 1,
'to_ping' => '',
'pinged' => '',
'post_content_filtered' => ''
) );
$wpdb->insert(
$wpdb->term_relationships,
array(
'term_taxonomy_id' => $cat_tt_id,
'object_id' => 1
)
);
// Default comment
$first_comment_author = __( 'Visitor' );
$first_comment_url = 'http://example.com/';
$first_comment = __( 'My first modified comment.' );
$wpdb->insert( $wpdb->comments, array(
'comment_post_ID' => 1,
'comment_author' => $first_comment_author,
'comment_author_email' => '',
'comment_author_url' => $first_comment_url,
'comment_date' => $now,
'comment_date_gmt' => $now_gmt,
'comment_content' => $first_comment
) );
// First Page
$first_page = sprintf( __( "Lorem ipsum lorem:
<blockquote>non sequitur</blockquote>
And:
<blockquote>more lorem ipsum.</blockquote>
Go to <a href=\"%s\">your dashboard</a> to delete this page and have fun!" ), admin_url() );
$first_post_guid = get_option( 'home' ) . '/?page_id=2';
$wpdb->insert( $wpdb->posts, array(
'post_author' => $user_id,
'post_date' => $now,
'post_date_gmt' => $now_gmt,
'post_content' => $first_page,
'post_excerpt' => '',
'post_title' => __( 'Sample Page' ),
/* translators: Default page slug */
'post_name' => __( 'sample-page' ),
'post_modified' => $now,
'post_modified_gmt' => $now_gmt,
'guid' => $first_post_guid,
'post_type' => 'page',
'to_ping' => '',
'pinged' => '',
'post_content_filtered' => ''
) );
$wpdb->insert(
$wpdb->postmeta,
array(
'post_id' => 2,
'meta_key' =>
'_wp_page_template',
'meta_value' => 'default'
)
);
// Set up default widgets for default theme.
update_option( 'widget_search',
array(
2 => array(
'title' => ''
),
'_multiwidget' => 1
)
);
// Don't show Welcome panel
update_user_meta( $user_id, 'show_welcome_panel', 0 );
// Activate our theme
update_option( 'template', 'f8-lite' );
update_option( 'stylesheet', 'f8-lite' );
update_option( 'current_theme', 'F8 Lite' );
// Update theme options
$theme_options = array(
"phone" => "0800INSTALLWP",
"email" => "[email protected]",
"selectinput" => "1",
"cats" => "1,2,3"
);
update_option( 'f8_theme_options', $theme_options );
// Activate our plugins
wpse_4041_run_activate_plugin( 'akismet/akismet.php' );
wpse_4041_run_activate_plugin( 'advanced-custom-fields/acf.php' );
// Inserting Akismet API Key
update_option( 'wordpress_api_key', 'YOUR-AKISMET-KEY' );
// Enable the Blogroll (disabled by default in WP 3.5)
update_option( 'link_manager_enabled', true );
}
/**
* Blocking WordPress from sending installation email notice
*
* Empty function
*/
function wp_new_blog_notification( $blog_title, $blog_url, $user_id, $password ) { /* empty function */ }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment