Skip to main content
2 of 7
Added Deleting the "Hello Dolly" Plugin section
MikeSchinkel
  • 37.5k
  • 14
  • 116
  • 132

As I mentioned I was going to be start working on this need immediately so I'm making headway. Given that I'm knocking these down I figured it's best to start posting them. Still, if someone else can/will post (some of) the parts I haven't done I'll be happy to let you copy whatever I'd done you haven't and select your answer as the best answer. Until then I'm going to start posting the code.

First Thing: Include wp-load.php:

Since we're creating a standalone file in the root of the website to run initialization that will only be used to "bootstrap" a site (I called mine /my-init.php) we start by including /wp-load.php to load the WordPress API functions:

<?php
include "wp-load.php";

Creating Users for the Site

We'll use the wp_insert_user() function located in /wp-includes/registration.php to create our users. This file is not loaded by default so we'll have to load it ourselves with a call to require_once().

We'll also use the get_user_by() function to first see if the user has already been created; no need to run the code twice if they haven't. NOTE: This is a pattern will follow; e.g. our script should not duplicate or overwrite anything if called multiple times, especially after users have added or changed data for any of the items we plan to initialize.

<?php
require_once( ABSPATH . WPINC . '/registration.php');

$user = get_user_by('slug','johnsmith');
if (!is_object($user)) {
  wp_insert_user(array(
    'user_login'        => 'johnsmith',
    'role'              => 'administrator',
    'user_email'        => '[email protected]',
    'user_url'          => 'http://example.com',
    'first_name'        => 'John',
    'last_name'         => 'Smith',
    'comment_shortcuts' => '',
    'use_ssl'           => '0',
    'user_pass'         => '12345',
  ));
}

##Deleting the "Hello Dolly" Plugin To delete the "Hello Dolly" plugin (sorry Matt) we'll use the delete_plugins() function. delete_plugins() expects an array of file paths that are relative to the /wp-content/includes/ directory. For the Hello Dolly plugin the file path is simply hello.php since the Hello Dolly plugin isn't stored in it's own directory but for most plugins it will be in the form of {$subdir}\{$filename}.php; i.e. the file path for Akismet is akismet/akismet.php.

However, delete_plugins() is not available until we've included /wp-admin/includes/plugin.php and there's also a dependency with wp-admin/includes/file.php so we require_once() both of those before we call delete_plugins(), i.e.:

require_once(ABSPATH . 'wp-admin/includes/plugin.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
delete_plugins(array('hello.php'));

Note that sometimes delete_plugins() will fail because of file permissions or maybe the fact a plugin is currently activated or some other reason that you'll first need to resolve but for our use-case Hello Dolly goes away without a fight.

##Next... I'll keep adding to this as I figure it out until I'm done or until someone else helps out.

MikeSchinkel
  • 37.5k
  • 14
  • 116
  • 132