All Projects → motivast → wp-cli-seed-command

motivast / wp-cli-seed-command

Licence: MIT license
WP CLI Seed Command is extension for WP-CLI to seed database with sample data

Programming Languages

PHP
23972 projects - #3 most used programming language
Gherkin
971 projects
Dockerfile
14818 projects
shell
77523 projects
Mustache
554 projects

Projects that are alternatives of or similar to wp-cli-seed-command

server-command
Launches PHP's built-in web server for a specific WordPress installation.
Stars: ✭ 69 (+245%)
Mutual labels:  wp-cli, wp-cli-package
wp-super-cache-cli
A CLI interface for the WP Super Cache plugin
Stars: ✭ 53 (+165%)
Mutual labels:  wp-cli, wp-cli-package
config-command
Generates and reads the wp-config.php file.
Stars: ✭ 32 (+60%)
Mutual labels:  wp-cli, wp-cli-package
import-command
Imports content from a given WXR file.
Stars: ✭ 19 (-5%)
Mutual labels:  wp-cli, wp-cli-package
shell-command
Opens an interactive PHP console for running and testing PHP code.
Stars: ✭ 18 (-10%)
Mutual labels:  wp-cli, wp-cli-package
i18n-command
Provides internationalization tools for WordPress projects.
Stars: ✭ 76 (+280%)
Mutual labels:  wp-cli, wp-cli-package
package-command
Lists, installs, and removes WP-CLI packages.
Stars: ✭ 16 (-20%)
Mutual labels:  wp-cli, wp-cli-package
db-command
Performs basic database operations using credentials stored in wp-config.php.
Stars: ✭ 65 (+225%)
Mutual labels:  wp-cli, wp-cli-package
cron-command
Tests, runs, and deletes WP-Cron events; manages WP-Cron schedules.
Stars: ✭ 23 (+15%)
Mutual labels:  wp-cli, wp-cli-package
cache-command
Manages object and transient caches.
Stars: ✭ 12 (-40%)
Mutual labels:  wp-cli, wp-cli-package
media-command
Imports files as attachments, regenerates thumbnails, or lists registered image sizes.
Stars: ✭ 40 (+100%)
Mutual labels:  wp-cli, wp-cli-package
scaffold-package-command
Scaffolds WP-CLI commands with functional tests, full README.md, and more.
Stars: ✭ 51 (+155%)
Mutual labels:  wp-cli, wp-cli-package
checksum-command
Verifies file integrity by comparing to published checksums.
Stars: ✭ 29 (+45%)
Mutual labels:  wp-cli, wp-cli-package
export-command
Exports WordPress content to a WXR file.
Stars: ✭ 12 (-40%)
Mutual labels:  wp-cli, wp-cli-package
core-command
Downloads, installs, updates, and manages a WordPress installation.
Stars: ✭ 41 (+105%)
Mutual labels:  wp-cli, wp-cli-package
language-command
Installs, activates, and manages language packs.
Stars: ✭ 12 (-40%)
Mutual labels:  wp-cli, wp-cli-package
scaffold-command
Generates code for post types, taxonomies, blocks, plugins, child themes, etc.
Stars: ✭ 149 (+645%)
Mutual labels:  wp-cli, wp-cli-package
Awesome Wp Cli
A curated list of packages and resources for WP-CLI, the command-line interface for WordPress.
Stars: ✭ 129 (+545%)
Mutual labels:  wp-cli
Wp Cli Login Command
Log in to WordPress with secure passwordless magic links.
Stars: ✭ 200 (+900%)
Mutual labels:  wp-cli
Docker Compose Wordpress
An example Docker Compose setup for WordPress plugin or theme development.
Stars: ✭ 127 (+535%)
Mutual labels:  wp-cli

WP CLI Seed Command

CircleCI

WP CLI Seed Command is an extension for WP-CLI to seed database with sample data.

Why?

Working with advanced WordPress project require to provide test data for other developers or testing scripts. Working with MySQL dumps or exported data has a couple of disadvantages. Data from files like import file or MySQL dump are static you can not quickly scale from 10 random to 100 random posts, also you can not import local media files.

This command-line tools aim to solve these problems. You can write your seeds in PHP which gives you unlimited possibilities.

Installation

You can install WP CLI Seed Command like any other WP-CLI extension

wp package install motivast/wp-cli-seed-command

If you want to install WP CLI Seed Command locally you can use composer in your project root directory

composer require motivast/wp-cli-seed-command

Getting started

WP CLI Seed is providing two commands wp seed for seeding database with data and wp scaffold seeder to quickly create new seeders. To create the main seeder the following command.

wp scaffold seeder seeder

This command will create a Seeder.php file in the seeds directory with the following content.

<?php

use Motivast\WP_CLI\Seed\AbstractSeeder;

class Seeder extends AbstractSeeder
{
	/**
	 * Run the database seeds.
	 *
	 * @return void
	 */
	public function run()
	{
		//
	}
}

Everything inside run method will be executed during seeding database. Inside it you can add any PHP code including WordPress functions. Let's change some WordPress options and add basic pages.

<?php

use Motivast\WP_CLI\Seed\AbstractSeeder;

class Seeder extends AbstractSeeder
{
	/**
	 * Run the database seeds.
	 *
	 * @return void
	 */
	public function run()
	{
		update_option('blogname', 'WP CLI Seed Command');
		update_option('blogdescription', '');

		$defaults = [
			'post_type' => 'page',
			'post_status' => 'publish',
		];

		$home_id = wp_insert_post( wp_parse_args( [
			'post_title' => 'Home'
		], $defaults ) );

		$about_id = wp_insert_post( wp_parse_args( [
			'post_title' => 'About'
		], $defaults ) );

		$contact_id = wp_insert_post( wp_parse_args( [
			'post_title' => 'Contact'
		], $defaults ) );

 		// Update homepage
		update_option( 'show_on_front', 'page' );
		update_option( 'page_on_front', $home_id );
	}
}

Now you can import your seeds executing following command.

wp seed

Your WordPress options should change and new pages should be created.

Splitting files

When your project will grow you might want to split seeds into multiple files. Based on the example above you can split this into two files. Create 'Options.php' and 'Pages.php' seeders.

wp scaffold seeder options
wp scaffold seeder pages
<?php

use Motivast\WP_CLI\Seed\AbstractSeeder;

class Options extends AbstractSeeder
{
	/**
	 * Run the database seeds.
	 *
	 * @return void
	 */
	public function run()
	{
		update_option('blogname', 'WP CLI Seed Command');
		update_option('blogdescription', '');
	}
}
<?php

use Motivast\WP_CLI\Seed\AbstractSeeder;

class Pages extends AbstractSeeder
{
	/**
	 * Run the database seeds.
	 *
	 * @return void
	 */
	public function run()
	{
		$defaults = [
			'post_type' => 'page',
			'post_status' => 'publish',
		];

		$home_id = wp_insert_post( wp_parse_args( [
			'post_title' => 'Home'
		], $defaults ) );

		$about_id = wp_insert_post( wp_parse_args( [
			'post_title' => 'About'
		], $defaults ) );

		$contact_id = wp_insert_post( wp_parse_args( [
			'post_title' => 'Contact'
		], $defaults ) );

 		// Update homepage
		update_option( 'show_on_front', 'page' );
		update_option( 'page_on_front', $home_id );
	}
}

Newlly created seeders have to be included and executed in your main Seeder.php file. Change your already existing Seeder.php to handle new seeders.

<?php

use Motivast\WP_CLI\Seed\AbstractSeeder;

require_once __DIR__ . '/Options.php';
require_once __DIR__ . '/Pages.php';

class Seeder extends AbstractSeeder
{
	/**
	 * Run the database seeds.
	 *
	 * @return void
	 */
	public function run()
	{
		$this->call(Options::class);
		$this->call(Pages::class);
	}
}

Contribute

Please make sure to read the Contribution guide before making a pull request.

Thank you to all the people who already contributed to WP-CLI Seed Command!

License

The project is licensed under the MIT.

Copyright (c) 2019-present, Motivast

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].